liveBook · Manning

Into

As explained in Clojure: The Essential Reference

into is a Clojure function that conjoins the elements of a source collection into a destination collection, optionally applying a transducer.

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.

How Clojure: The Essential Reference covers it

How deep it goes
Defines arities, accepted collections, map-entry errors, transducer use, metadata behavior, and performance comparisons.
Examples
Demonstrates set, vector, map, queue, metadata, transducer, collection-preserving, and merge alternatives.
What it assumes
Assumes familiarity with Clojure collections, conj, sequence functions, and transducer composition.
What you can do after
Apply it via Clojure collection and transducer operations to build results in the required destination type.
How it connects
Connects collections, conj, transducers, map, filter, sequence processing, queues, and metadata.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Clojure: The Essential Reference. The section links point to the parts of the book it draws on.