I'm learning Clojure and am confused by the following:
(vector "1")
;; returns ["1"]
(map vector '("1" "2" "3"))
;; returns (["1"] ["2"] ["3"])
However:
(Integer/parseInt "1")
;; returns 1
(map Integer/parseInt '("1" "2" "3"))
;; throws error "Unable to find static field: parseInt in class java.lang.Integer"
Instead, I need:
(map #(Integer/parseInt %) '("1" "2" "3"))
;; returns (1 2 3)
If I can use a function like vector
with map
, why can't I use Integer/parseInt
?
You have to wrap it in #()
or (fn ...)
. This is because Integer/parseInt is a Java method and Java methods can't be passed around. They don't implement the IFn
interface.
Clojure is built on Java and sometimes this leaks through, and this is one of those cases.