liveBook · Manning

Condp

As explained in Clojure: The Essential Reference

condp is a Clojure macro that evaluates clauses in order using one predicate applied to a selector and an expression, returning the result of the first matching clause or a default.

condp is a Clojure macro for ordered conditional branching: it applies one two-argument predicate to each selector and an expression, returning the first matching choice or default. section 3.3.3

Listing 3.14. Contract
(condp <pred> <expr> [clauses] [<default>])

  clause :=> pair || triplet

  pair    :=> <selector> <choice>
  triplet :=> <selector> :>> <f>

The contract shows the predicate, expression, optional clauses, default, and pair or triplet clause forms. section 3.3.3

Its contract is:

  • pred takes selector and expr; its result is interpreted as boolean. section 3.3.3
  • Pairs contain selector and choice; triplets contain selector, :>>, and a single-argument function. section 3.3.3
  • Clauses run in order, and a missing match without a default throws IllegalArgumentException. section 3.3.3

Matching values and computed conditions

A MIME-type function can match a URL extension with =, while FizzBuzz can test divisibility with #(zero? (mod %2 %1)). For input 15, the first matching selector returns "fizzbuzz". section 3.3.3

Using :>>

With (comp seq filter) as the predicate, :>> passes the predicate result to a function. A card game therefore selects the first matching hand category and applies its ranking function. section 3.3.3

How Clojure: The Essential Reference covers it

How deep it goes
Defines the macro contract, clause forms, outputs, evaluation order, examples, and the missing-match exception.
Examples
Uses operator selection, MIME-type dispatch, FizzBuzz divisibility, and poker-hand classification.
What you can do after
Apply it to build predicate-driven dispatch with pair clauses, defaults, and :>> functions.
How it connects
Connects conditional branching with cond, case, predicates, functions, filter, and higher-order ranking operations.

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.