liveBook · Manning

Filterv

As explained in Clojure: The Essential Reference

filterv is a Clojure function that eagerly filters a seqable collection and returns the retained elements as a persistent vector.

filterv eagerly applies a predicate to any seqable collection and returns the retained elements as a persistent vector, preserving input order. Use it when the result should be a vector; choose filter when laziness or output type matters more. section 12.9

Its contract is straightforward:

  • pred is a predicate; each item is retained when (pred item) is truthy. section 12.9
  • coll may be any seqable collection; nil is treated as empty. section 12.9
  • The result is a persistent vector, and eager evaluation loads it into memory. section 12.9
  • Incorrect predicate arity throws ArityException; a non-function predicate throws ClassCastException; a non-seqable collection throws IllegalArgumentException. section 12.9
  • filterv has no transducer arity and runs in O(n) time and space. section 12.9

Filtering a range

(filterv odd? (range 8)) ;; [1 3 5 7] A practical use filters completed asynchronous channels from the remaining channel vector: (filterv #(not= channel %) channels)

How Clojure: The Essential Reference covers it

How deep it goes
Defines inputs, outputs, exceptions, eagerness, transducer limits, and O(n) time and space.
Examples
Uses odd-number selection and removal of completed channels from asynchronous processing.
What it assumes
Assumes predicate functions and seqable collections while defining nil handling and predicate failures.
What you can do after
Apply it via Clojure to produce an eagerly realized persistent vector of predicate-matching elements.
How it connects
Connects filtering with filter, mapv, transducers, seqable collections, and persistent vectors.

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.