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