Skip to content

clojure.core/condp

View this page on ClojureDocs

Type: macro Added: Clojure 1.0 Examples: 13 Runnable: 10
([pred expr & clauses])

Takes a binary predicate, an expression, and a set of clauses. Each clause can take the form of either:

test-expr result-expr

test-expr :>> result-fn

Note :>> is an ordinary keyword.

For each clause, (pred test-expr expr) is evaluated. If it returns logical true, the clause is a match. If a binary clause matches, the result-expr is returned, if a ternary clause matches, its result-fn, which must be a unary function, is called with the result of the predicate as its argument, the result of that call being the return value of condp. A single default expression can follow the clauses, and its value will be returned if no clause matches. If no default expression is provided and no clause matches, an IllegalArgumentException is thrown.

Examples

by jneira on . May have evaluation errors.
by kotarak on
by kotarak on . May have evaluation errors.
by kotarak on
by jafingerhut on
by boxie on . May have evaluation errors.
by leesper on
by vee on
by dfletcher on
by bfontaine on
by ernstroux on
by berczeck on
by Rovanion on

Note by bsima

A lot of these examples show the pattern:

(condp = value
  :a "a"
  :b "b"
  ...)

Which is the same as:

(case value
  :a "a"
  :b "b"
  ...)

See also


Content from the matching ClojureDocs page, with authors credited on each contribution.