liveBook · Manning

Predicate

As explained in Clojure: The Essential Reference

A predicate is a function whose result is interpreted as a boolean condition, even when its returned value is not literally boolean.

A predicate is a function used to decide whether a value passes a condition. In Clojure, its result need not be a Boolean: nil and false count as false, while other results count as true. APIs such as filter, every?, and some call a predicate with one collection item at a time. section 2.2.9 section 8.1.5

Choose the operation according to the question:

  • filter keeps items that pass; remove discards them. Both return lazy sequences. Prefer predicates without side effects: lazy processing does not guarantee a once-only call for each item. section 3.5.3
  • every? asks whether all items pass; not-every? asks whether at least one fails. some looks for a passing result; not-any? asks whether none pass. The question-mark functions return Booleans, while some can return the predicate’s result. section 8.1.5
  • every-pred combines checks with AND; some-fn combines them with OR. Each produces a function that can test zero or more values, stopping when the answer is determined. every-pred returns a Boolean; some-fn can return a non-Boolean match. section 2.2.9

Select and combine checks

(filter odd? [1 2 3 4 5]) ;; (1 3 5) (defn symmetric? [xs] (= (seq xs) (reverse xs))) (def palindrome? (every-pred some? string? not-empty symmetric?)) (filter palindrome? ["a" nil :abba 1 "" "racecar"]) ;; ("a" "racecar") The combined predicate excludes nil, non-strings, and empty strings before checking symmetry. Passing (and number? pos? odd?) directly to remove would not combine those functions per item: and evaluates the function values when that expression is formed. Use every-pred, or an anonymous function that calls each check on its item. section 3.5.3 section 2.2.9

A useful predicate can return evidence rather than just true. A set used as a function can return a matching member, and a map used as a function can return an associated value. For example, some can return a prize description found while checking tickets; some-fn can return the unwanted word found while checking email. These results can be used immediately rather than repeating the search. section 8.1.5 section 2.2.9

Predicates also express function contracts. In defn, :pre predicates run before the body and can inspect arguments; :post predicates run afterward and can inspect the return value through %. Failed conditions raise an AssertionError. Keep the distinction clear: a predicate supplies a condition, but the function using it determines whether the result becomes a Boolean, a selected item, or a returned match. section 2.1.1 section 2.2.9

How Clojure: The Essential Reference covers it

How deep it goes
Defines the unary contract and truth-value interpretation, then details return values, short-circuiting, laziness, and assertion failures.
Examples
Demonstrates odd-number filtering, palindrome checks, email spam detection, prize lookup, and validation before and after saving an item.
What it assumes
Assumes basic Clojure function calls and collections while defining unary predicate contracts and how non-Boolean results are interpreted.
What you can do after
Apply it via Clojure's filter, remove, every?, some, every-pred, and some-fn to select values and combine checks.
How it connects
Connects predicates to filter, remove, every?, some, every-pred, some-fn, and defn pre- and post-conditions.

Also explained in

Compare how 5 books explain predicate →

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.