into conjoins every element from a source collection into a destination collection, optionally transforming the elements with a transducer. The result keeps the destination’s collection type and follows conj semantics. section 8.1.1 section 7.2
Use these arities and input rules:
- Call (into to from) to copy elements, or (into to xform from) to apply a transducer chain while accumulating them. section 8.1.1
- Provide a seqable source or nil; use a list, vector, set, queue, map, or nil destination. A nil destination defaults to a list. section 8.1.1
- Supply map entries as pairs or maps; otherwise a map destination can throw ClassCastException when conj cannot process the source element. section 8.1.1
Changing the destination type
(into #{} (range 10)) ;; #{0 7 1 4 6 3 2 9 5 8} (into [:g :x :d] [1 5 9]) ;; [:g :x :d 1 9 5] The source and destination may use different collection types; the destination determines the result. section 8.1.1
Transforming while accumulating
(into (vector-of :int) (comp (map inc) (filter even?)) (range 10)) ;; [2 4 6 8 10] A transducer chain performs the mapping and filtering during accumulation. section 7.2
Preserving a collection’s shape
(defn maintain [fx f coll] (into (empty coll) (fx f coll))) (->> #{1 2 3 4 5} (maintain map inc) (maintain filter odd?)) ;; #{3 5} Creating an empty destination from the input lets sequence operations return the original collection kind.
