为什么Clojure中的Symbol可以用作功能? [英] Why Symbol in clojure can be used as Functions?
本文介绍了为什么Clojure中的Symbol可以用作功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我请您参考以下短代码:
I refer you to this short code:
('a 1)
;==> nil
('a 1 2)
;=>2
为什么在这种情况下,符号 a
可以用作函数?
why can the symbol a
be used as a function in this context?
推荐答案
它允许您以一种不错的语法查找以符号为键的地图.
It allows you to look up a map with the symbol as a key, in a nice syntax.
以下等效:
('a my-hash-map)
(get my-hash-map 'a) ; returns value associated with 'a or nil if not present
为进一步方便起见,您还可以提供默认值作为可选的第二个参数:
For further convenience, you can also supply default value as optional second argument:
('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.
As to you example, ('a 1) returns nil because (get 1 'a) returns nil, and ('a 1 2) returned supplied default value, 2.
这篇关于为什么Clojure中的Symbol可以用作功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文