clojure - 为什么 clojure 中的 Symbol 可以用作函数?

标签 clojure symbols

<分区>

我建议你引用这个简短的代码:

('a 1)
;==> nil
('a 1 2)
;=>2

为什么符号 a 可以用作此上下文中的函数?

最佳答案

它允许您以一个很好的语法以符号作为键来查找 map 。

下面是等价的:

('a my-hash-map)
(get my-hash-map 'a)  ; returns value associated with 'a or nil if not present

为了更方便,您还可以提供默认值作为可选的第二个参数:

('a my-hash-map 42)  ; returns 42 if my-hash-map does't have the key 'a

以你为例,('a 1) 返回 nil 因为 (get 1 'a) 返回 nil('a 1 2) 返回提供的默认值 2。

关于clojure - 为什么 clojure 中的 Symbol 可以用作函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21312724/

相关文章:

clojure - yogthos/config 在 CIDER 中获取我的环境,但在 lein with-profile 中运行时则不然

unit-testing - Midje 在 Clojure 中的宏上失败

php - 在 PHP 和 MySQL 中使用图标

ruby - 如何复制符号?

debugging - 如何更改符号文件(pdb)中源文件的位置

string - 在 Elisp 中将符号转换为字符串

Lisp 代码格式

clojure - 为什么答案中有这么多0?

Clojure 在类型推断方面的不足

c++ - 使用 g++ 连接两个具有相同函数签名的强函数符号的结果,为什么?