Using interop constructor in map function(Clojure)

后端 未结 2 879
南方客
南方客 2020-12-19 23:09

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

=> (map Integer. [\"1\" \"2\" \"3\"])
CompilerException java.lang.ClassNotF         


        
相关标签:
2条回答
  • 2020-12-19 23:54

    without java interop. if you just need to convert to digits.

    ; nrepl.el 0.2.0 (Clojure 1.5.1, nREPL 0.2.3)
    user> (map read-string ["1" "2"])
    (1 2)
    user> (class (first *1))
    java.lang.Long
    

    Or if you really need Integer class

    user> (map (comp int read-string) ["1" "2"])
    (1 2)
    user> (class (first *1))
    java.lang.Integer
    
    0 讨论(0)
  • 2020-12-19 23:57

    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.

    0 讨论(0)
提交回复
热议问题