liveBook · Manning

Mapv

As explained in Clojure: The Essential Reference

mapv is a Clojure function that applies a mapping function to a collection and returns the results in a vector.

mapv applies a function across one or more seqable collections and returns the results in a persistent vector. With multiple collections, it stops at the shortest. section 12.8

Use mapv when eager vector output matters:

  • Unlike map, mapv returns a vector and fully consumes lazy inputs. section 12.8
  • The function’s arity must match the number of collections; nil collections behave as empty collections. section 12.8
  • Incorrect function arity, a non-function, or a non-seqable collection produces an exception. section 12.8
  • mapv has no single-arity transducer form; use transducer-based vector construction for other sequence operations. section 12.8

Element-wise transformation

(mapv inc [0 1 2 3]) ;; [1 2 3 4] (mapv hash-map [:a :b :c] (range)) ;; [{:a 0} {:b 1} {:c 2}]

Composing vector operations

(defn scalar-multiply [c a] (mapv (partial * c) a)) (scalar-multiply 3 [1 2 3]) ;; [3 6 9] (add [1 2] [3 4]) ;; [4 6]

Listing 12.5. → Sequence processing, Higher-order functions
(mapv
  ([f coll])
  ([f c1 c2])
  ([f c1 c2 c3])
  ([f c1 c2 c3 & colls]))

Inspect mapv’s supported arities, from one collection through additional collections. section 12.8

How Clojure: The Essential Reference covers it

How deep it goes
Explains contracts, arities, exceptions, laziness, multi-collection behavior, and transducer limitations without implementation internals.
Examples
Demonstrates incrementing values, pairing keywords with ranges, and composing vector arithmetic.
What it assumes
Assumes familiarity with Clojure functions, collections, sequences, vectors, and higher-order function calls.
What you can do after
Apply it via Clojure to produce eager vectors from unary or parallel collection mappings.
How it connects
Connects mapv with map, filterv, lazy sequences, persistent vectors, and transducers.

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.