liveBook · Manning

Vec

As explained in Clojure: The Essential Reference

vec is a Clojure function that creates a vector from a seqable collection, iterable collection, Java array, or nil.

vec creates a new persistent vector from a seqable or iterable collection, Java array, or nil. Ordered inputs retain their order; sets and maps do not promise one. section 12.5

Use vec when a vector is the useful boundary for collection data:

  • Convert lists, lazy sequences, ranges, sets, maps, Java iterables, and arrays into vector values. section 12.5
  • Treat transients and structs as exceptions; inputs that cannot become sequences throw a RuntimeException. section 12.5
  • Remember that small reference-type Java arrays may be aliased, so later array mutation can change the apparent vector value. section 12.5

Array aliasing

(def a (make-array Long 3)) (def v (vec a)) (aset a 1 99) ;; v becomes [nil 99 nil] Primitive arrays behave differently: (def a (long-array 3)) (def v (vec a)) (aset a 1 99) ;; v remains [0 0 0]

How Clojure: The Essential Reference covers it

How deep it goes
Defines inputs, ordering, exceptions, aliasing, and examples, but does not explain implementation internals.
Examples
Uses collection-type comparisons, reference and primitive Java arrays, cached search results, and vec-versus-into benchmarking.
What it assumes
Assumes basic Clojure collection use while teaching supported inputs, ordering, exceptions, and array-specific behavior.
What you can do after
Apply it via vec to materialize supported collections as persistent vectors and recognize unsafe reference-array aliasing.
How it connects
Connects vectors with sequences, collections, transients, structs, Java iterables, and Java arrays.

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.