I'm learning Clojure and I've a doubt:
Why when I type
(map vector '("1" "2" "3"))
I get (["1"] ["2"] ["3"])
It's OK because vector is a function (or almost I think it), and I can do
(vector "3") and get ["3"].
So far so good, but when I try
(map Integer/parseInt '("1" "2" "3"))
I get an error. Shouldn't parseInt behave like a function?
Then I need to type
(map #(Integer/parseInt %) '(......
Why can't I use parseInt like a function? For me it's a function, and I can use it like
(Integer/parseInt "3")
I feel it a bit incoherent, but I'm sure I'm making some mistake and is for this I ask it...
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With