liveBook · Manning

Order property

As explained in Clojure: The Essential Reference

A relation that determines the relative ordering of values, such as less than, greater than, or equality.

An order property determines how values relate from smaller to larger, larger to smaller, or equal. Clojure uses this relationship in numeric predicates such as <, >, <=, and >=, and in max and min. section 5.8 section 6.3

Clojure’s ordering predicates check the relationship across every neighboring argument:

  • < requires strict increase; <= permits equal neighboring values; > requires strict decrease; >= permits equality. section 6.3
  • With one argument, every predicate returns true and accepts any type. With two or more arguments, all arguments must be numeric. section 6.3
  • No arguments cause an ArityException; a nonnumeric multi-argument call causes a ClassCastException. section 6.3
  • max and min return the largest or smallest numeric argument, while Infinity dominates in its corresponding direction and NaN propagates through either operation. section 5.8

Chaining an ordering check

(< 0 (byte 1) 2 2.1 3N 4M 21/2) ;; true (< 0 (byte 1) 2 2.1 3N 2M 21/2) ;; false (sort > (range 5)) ;; (4 3 2 1 0) 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 comparison operators’ arities and ordering-oriented signatures. section 6.3

How Clojure: The Essential Reference covers it

How deep it goes
Defines predicates, arities, numeric contracts, exceptions, monotonicity, special values, and implementation details.
Examples
Uses chained numeric comparisons, descending sorting, extrema over ranges, lap-time tracking, Infinity, and NaN.
What it assumes
Assumes basic Clojure function calls and numbers while defining single-argument behavior and numeric ordering from zero.
What you can do after
Apply Clojure comparison predicates to validate numeric ordering and combine them with sorting and extrema operations.
How it connects
Connects comparison predicates with numerical equality, max and min, sorting, statistics, and Java numeric types.

Also explained in

Compare how 8 books explain Order property →

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.