Clojure - convert list to java array

Is there any idiomatic way of converting a Clojure list to a Java array other than first converting it to a vector and using a in-array (means something other than (into-array (vec my-list)) , as I don't need extra overhead?

+4
source share
2 answers

Your question seems to be based on a false premise. in-array it is not necessary to take a vector, it takes the value of seqable. The documentation ( http://clojuredocs.org/clojure_core/clojure.core/into-array ) contains examples of using in-array in a non-vector sequence:

 user=> (into-array (range 4)) #<Integer[] [Ljava.lang.Integer;@63d6dc46> user=> (type (range 4)) clojure.lang.LazySeq user=> (doc range) ------------------------- clojure.core/range ([] [end] [start end] [start end step]) Returns a lazy seq of nums from start (inclusive) to end (exclusive), by step, where start defaults to 0, step to 1, and end to infinity. 

Calling it on a list works just as well:

 user=> (into-array (list 1 2 3)) #<Long[] [Ljava.lang.Long;@138297fe> 
+9
source

Since I needed to specifically create an integer array, I used the int-array function, which takes a list as a parameter.

+1
source

Source: https://habr.com/ru/post/1481732/


All Articles