[ACCEPTED]-Into or vec: converting sequence back to vector in Clojure-sequence
Actually as of Clojure 1.4.0 the preferred 25 way of doing this is to use mapv
, which is like 24 map
except its return value is a vector. It 23 is by far the most efficient approach, with 22 no unnecessary intermediate allocations 21 at all.
Clojure 1.5.0 will bring a new reducers 20 library which will provide a generic way 19 to map
, filter
, take
, drop
etc. while creating vectors, usable 18 with into []
. You can play with it in the 1.5.0 17 alphas and in the recent tagged releases 16 of ClojureScript.
As for (vec some-seq)
and (into [] some-seq)
, the first 15 ultimately delegates to a Java loop which 14 pours some-seq
into an empty transient vector, while 13 the second does the same thing in very efficient 12 Clojure code. In both cases there are some 11 initial checks involved to determine which 10 approach to take when constructing the final 9 return value.
vec
and into []
are significantly different 8 for Java arrays of small length (up to 32) -- the 7 first will alias the array (use it as the 6 tail of the newly created vector) and demands 5 that the array not be modified subsequently, lest 4 the contents of the vector change (see the 3 docstring); the latter creates a new vector 2 with a new tail and doesn't care about future 1 changes to the array.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.