liveBook · Manning

Comparison predicate

As explained in Clojure: The Essential Reference

A predicate that evaluates an ordering relationship among one or more values and returns a Boolean result.

A comparison predicate tests an ordering relationship and returns a Boolean. In Clojure, <, >, <=, and >= accept one or more arguments and verify the relationship across the entire sequence. section 6.3

Use the arity and input contract carefully:

  • A single argument of any type returns true. section 6.3
  • Two or more arguments must be numeric; nonnumeric values raise ClassCastException. section 6.3
  • Calling a predicate without arguments raises ArityException. section 6.3
  • < and > require strict increase or decrease, while <= and >= permit equal adjacent values. section 6.3

Checking an ordered sequence

(< 0 (byte 1) 2 2.1 3N 2M 21/2) ;; false (apply < [2.1 4 5.2 8 124 9012 1e4]) ;; true section 6.3

Listing 6.3. → Comparison Operators, Ordering
(<  ([x]) ([x y]) ([x y & more]))
(>  ([x]) ([x y]) ([x y & more]))
(<= ([x]) ([x y]) ([x y & more]))
(>= ([x]) ([x y]) ([x y & more]))

Browse the supported arities for all four ordering predicates. section 6.3

How Clojure: The Essential Reference covers it

How deep it goes
Defines arity, accepted values, Boolean results, ordering semantics, exceptions, and recursive multi-argument implementation.
Examples
Uses heterogeneous numeric sequences, apply, sort, drop-while, disassembly, and alternative predicate implementations.
What you can do after
Apply via Clojure ordering predicates to test numeric sequences and compose checks with apply, sort, or drop-while.
How it connects
Connects ordering predicates with numeric equality, sorting, partial application, and primitive type hints.

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.