9.1.0.9
2.5 Orders
| (require toolbox/order) | package: toolbox-lib |
procedure
(ordering-reverse ord) → ordering/c
ord : ordering/c
Returns the reverse of ord.
Examples:
> (ordering-reverse '<) '>
> (ordering-reverse '=) '=
> (ordering-reverse '>) '<
procedure
(order-reverse ord) → order?
ord : order?
Examples:
> (define reversed-real/o (order-reverse real/o)) > (reversed-real/o 1.0 1) '=
> (reversed-real/o 5 7) '>
> (reversed-real/o 9.0 3.4) '<
The same bindings as real-order and datum-order reprovided under different names for consistency.
Returns an order object that orders lists lexicographically. The number of elements in the lists must match the number of arguments supplied to list/o, and each element of the list is ordered according to the corresponding order. In other words, list/o is to order objects as list/c is to contracts.
Examples:
> (define up-then-down (list/o real/o (order-reverse real/o))) > (up-then-down '(1 1) '(2 1)) '<
> (up-then-down '(1 1) '(1 2)) '>
> (up-then-down '(1 1) '(2 2)) '<
> (sort (for*/list ([i (in-range 4)] [j (in-range 4)]) (list i j)) (order-<? up-then-down))
'((0 3)
(0 2)
(0 1)
(0 0)
(1 3)
(1 2)
(1 1)
(1 0)
(2 3)
(2 2)
(2 1)
(2 0)
(3 3)
(3 2)
(3 1)
(3 0))
procedure
(property/o accessor ord [ #:name name #:domain domain-ctc]) → order? accessor : (-> any/c any/c) ord : order?
name : symbol? =
(if (symbol? (object-name accessor)) (object-name accessor) 'property) domain-ctc : contract? = any/c
Returns an order object that orders values by applying accessor to both arguments, then ordering the results using ord. In other words, property/o is to order objects as property/c is to contracts.
Examples:
> (define length/o (property/o length real/o #:domain list?)) > (length/o '(1 2) '(3)) '>
> (length/o '(1 2) '(3 4)) '=
> (length/o '(1 2) '(3 4 5)) '<
This is similar to list/o, but all of the given order objects are applied to the same values rather than to successive pairs of list elements.
Returns an order object that orders values lexicographically. The given ord objects are applied from left to right to each pair of values to be compared, returning the first non-'= result, or '= if all ord objects return '=.
Examples:
> (define length-then-alphabetical (lexico/o (property/o string-length real/o) datum/o))
> (sort '("the" "quick" "brown" "fox" "jumps" "over" "the" "lazy" "dog") (order-<? length-then-alphabetical)) '("dog" "fox" "the" "the" "lazy" "over" "brown" "jumps" "quick")