Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Weird error when trying to map parseInt in Clojure

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...

like image 789
coco Avatar asked Jun 01 '11 05:06

coco


1 Answers

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.

like image 197
nickik Avatar answered Sep 27 '22 21:09

nickik