liveBook · Manning

Filter+

As explained in Clojure: The Essential Reference

filter+ is a book-defined filtering function that applies a multi-argument predicate to corresponding elements of multiple collections.

filter+ filters several collections in parallel. Its predicate receives corresponding elements from each collection, and an element position survives when the multi-argument predicate returns a truthy value. section 3.5.3

The function has two useful forms:

  • (filter+ pred coll) delegates directly to filter for one collection. section 3.5.3
  • (filter+ pred c1 c2 & colls) combines corresponding values from the collections, applies pred to each group, and filters those groups. section 3.5.3
  • The result is lazy; the predicate should preferably be free of side effects because lazy evaluation does not guarantee once-only predicate calls. section 3.5.3
  • The implementation walks collections position by position and stops when any collection has no remaining element. section 3.5.3

Matching regular expressions against strings

(filter+ re-seq (map re-pattern (map str (range))) ["234983" "5671" "84987"]) ;; ((#"1" "5671")) Each regular expression is paired with the string at the same position. Only the pair whose predicate result is truthy remains. section 3.5.3

(defn walk-all
  "Returns a lazy-seq of all first elements in coll,
  then all second elements and so on."
  [colls]
  (lazy-seq
    (let [ss (map seq colls)]                                 ; #1
      (when (every? identity ss)                              ; #2
        (cons (map first ss) (walk-all (map rest ss)))))))    ; #3

(defn filter+
  ([pred coll]                                                ; #4
   (filter pred coll))
  ([pred c1 c2 & colls]                                       ; #5
   (filter+ #(apply pred %) (walk-all (conj colls c2 c1)))))

Inspect the implementation to see the single-collection delegation, parallel traversal, and predicate application. section 3.5.3

How Clojure: The Essential Reference covers it

How deep it goes
Defines both arities and shows lazy parallel traversal, but stops short of broader collection-processing internals.
Examples
Demonstrates parallel regular-expression matching against strings produced from a numeric range.
What it assumes
Assumes predicates, sequential collections, filter, and lazy sequences; teaches the parallel multi-collection pattern.
What you can do after
Apply via filter+ a multi-argument predicate across corresponding values in several collections.
How it connects
Connects filter+ to filter, predicates, lazy sequences, sequential collections, and transducer-based collection processing.

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.