Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using interop constructor in map function(Clojure)

Tags:

clojure

I am getting this complaint when passing Integer constructor to map function :

=> (map Integer. ["1" "2" "3"])
CompilerException java.lang.ClassNotFoundException: Integer., compiling:(NO_SOURCE_PATH:1:1) 

However when I wrap the constructor in a function everything works:

=> (defn str-to-int [str] (Integer. str))
=> (map str-to-int ["1" "2" "3"])
(1 2 3)

Why do I have to wrap Integer in another function to make this work? Is there a better way to make it work without creating additional function?

like image 725
Viktor K. Avatar asked May 04 '26 03:05

Viktor K.


1 Answers

map takes in a function and interop uses a special forms like new . and .. It is fairly easy to wrap these with anonymous function literals

for example

(map #(Integer. %) ["1" "2" "3"])

produces the desired result.

like image 134
KobbyPemson Avatar answered May 06 '26 09:05

KobbyPemson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!