liveBook · Manning

Every-pred

As explained in Clojure: The Essential Reference

every-pred is a Clojure function that returns a predicate combining multiple predicates with conjunction across its arguments.

`every-pred` combines one or more predicates into a reusable conjunction. The returned function accepts zero or more values and returns `true` only when every predicate succeeds for every value. section 2.2.9

Its contract has a few useful edges:

  • Each predicate must accept one argument. Predicate results use Clojure truthiness, but the combined result is always `true` or `false`. section 2.2.9
  • Evaluation stops at the first `nil` or `false` result. With no values to test, the combined predicate returns `true`; calling `every-pred` with no predicates raises an `ArityException`. section 2.2.9

Combine checks for collection processing

`(remove (every-pred number? pos? odd?) (range 10))` yields `(0 2 4 6 8)`. Unlike `(and number? pos? odd?)`, which evaluates while forming the argument to `remove` and leaves only `odd?`, `every-pred` runs all three checks on each item. section 2.2.9

(defn symmetric? [xs]
  (= (seq xs) (reverse xs)))

(def palindrome? ; #1
  (every-pred some? string? not-empty symmetric?))

(defn palindromes [coll] ; #2
  (filter palindrome?  coll))

(palindromes ["a" nil :abba 1 "" "racecar" "abba" \a])
;; ("a" "racecar" "abba")

A reusable `palindrome?` combines non-nil, string, nonempty, and symmetry checks before `filter` selects matching values. section 2.2.9

Use `every?` to test one predicate across a collection; use `every-pred` when multiple predicates must all pass. For an alternative that succeeds when a check produces a truthy result, use `some-fn`. section 8.1.5 section 2.2.9

How Clojure: The Essential Reference covers it

How deep it goes
Defines arities, truthiness, boolean output, and short-circuiting without showing the function’s implementation.
Examples
Uses numeric filtering, a reusable palindrome predicate, and predicate-count benchmarks.
What it assumes
Assumes basic Clojure predicates and collection filtering while explaining truthiness and the combined function’s contract.
What you can do after
Apply it via Clojure’s `every-pred` to replace repeated anonymous conjunctions with reusable filtering predicates.
How it connects
Connects predicate composition to `and`, `filter`, `remove`, `every?`, and `some-fn`.

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.