liveBook · Manning

Vector-of

As explained in Clojure: The Essential Reference

vector-of is a Clojure function that creates a persistent vector constrained to values coercible to a specified primitive type.

vector-of creates a persistent vector whose elements are stored as a selected primitive type, reducing memory use when primitive storage fits the problem. Supply one of :int, :long, :float, :double, :byte, :short, :char, or :boolean; values must be coercible to that type. section 12.7

Listing 12.4. → Persistent Data Type, Primitives, Memory Efficient Storage
(vector-of
  ([t])
  ([t x1])
  ([t x1 x2])
  ([t x1 x2 x3])
  ([t x1 x2 x3 x4])
  ([t x1 x2 x3 x4 & xn]))

The arity listing shows the empty-vector form, fixed argument forms, and the variadic form. section 12.7

Coercion and vector behavior

(vector-of :int 16/5 2.8 1M Double/NaN) ;; [3 2 1 0] ((vector-of :int 1 2 3) 2) ;; 3 (vector-of :int) ;; [] section 12.7

Account for these constraints:

  • Reject nil because primitive vectors cannot contain it, whether during construction or conj. section 12.7
  • Expect failures for invalid type specifications, incompatible coercions, and out-of-range values. section 12.7
  • Use it for dense numerical data such as Mandelbrot-set points containing real, imaginary, and iteration values. section 12.7

How Clojure: The Essential Reference covers it

How deep it goes
Explains the contract, coercion, vector behavior, exceptions, and primitive-storage motivation, but does not detail implementation internals.
Examples
Uses coercion, indexed lookup, invalid inputs, Mandelbrot points, benchmarking, and memory-measurement examples.
What you can do after
Apply vector-of to construct and access primitive-constrained persistent vectors while handling coercion, nil, and range failures.
How it connects
Connects persistent vectors, primitive types, numerical computing, and the Mandelbrot set.

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.