In clojure, what function do the characters represent? Why ('+ 2 2) = 2?

While playing with Clojure, I noticed that ('+ 2 2) did not give an error, as I expected - it returns 2. I spent several minutes playing around:

(def f (cast clojure.lang.IFn 'a-symbol))
(f 5)     ;; => nil
(f 5 5)   ;; => 5
(f 5 5 5) ;; ArityException Wrong number of args (3) passed to: Symbol
(f "hey")             ;; => nil
(f "foo" "bar")       ;; => "bar"
(f "foo" "bar" "baz") ;; => ArityException Wrong number of args (3) passed to: Symbol

As far as I can tell, the characters are passed to some function called Symbol, which takes two arguments and returns the second. I assume this has something to do with implementing a character class?

+4
source share
1 answer

When called as a symbol of a function (e.g. keywords), look at yourself in the map passed as the second argument

user> (def my-map '{a 1 b 2 c 3})
#'user/my-map
user> ('a my-map)
1
user> ('a my-map :not-found)
1
user> ('z my-map :not-found)
:not-found

, , , . , , , , 5, :

user> ('z 4 :not-found)
:not-found
user> ('z 'z :not-found)
:not-found 

, nil, .

+10

All Articles