How can I cast a sequence back to vector after a sequence producing operation (like sort)? Does using (vec..) on a sequence that was a vector is costly?
One (bad?)
Meikel Brandmeyer just posted a solution to this on the Clojure group.
(defn sorted-vec
[coll]
(let [arr (into-array coll)]
(java.util.Arrays/sort arr)
(vec arr)))
Clojure's sort returns a seq across a sorted array; this approach does much the same thing, but returns a vector, not a seq.
If you wish, you can even skip the conversion back into a Clojure persistent data structure:
(defn sorted-arr
"Returns a *mutable* array!"
[coll]
(doto (into-array coll)]
(java.util.Arrays/sort))
but the resulting Java array (which you can treat as a Clojure collection in most cases) will be mutable. That's fine if you're not handing it off to other code, but be careful.