On this page:
comparator?
compare
1.9.1 Constructing Comparators
comparator-map
make-comparator
comparator-reverse
1.9.2 Predefined Comparators
real<=>
string<=>
1.9.3 Comparison Constants
comparison?
lesser
greater
equivalent
7.4

1.9 Comparators

 (require rebellion/base/comparator) package: rebellion

A comparator is an object that compares two values and determines which (if either) is greater.

procedure

(comparator? v)  boolean?

  v : any/c
A predicate for comparators.

procedure

(compare comparator left right)  comparison?

  comparator : comparator?
  left : any/c
  right : any/c
Returns whether left is less than, greater than, or equivalent to right.

Examples:
> (compare real<=> 5 8)

#<lesser>

> (compare string<=> "foo" "bar")

#<greater>

1.9.1 Constructing Comparators

procedure

(comparator-map comparator f [#:name name])  comparator?

  comparator : comparator?
  f : (-> any/c any/c)
  name : (or/c interned-symbol? #f) = #f
Wraps comparator as a comparator that first calls f on both of its inputs before comparing them.

Examples:
> (define-record-type circle (color radius))
> (define circle<=> (comparator-map real<=> circle-radius))
> (compare circle<=>
           (circle #:color 'green #:radius 5)
           (circle #:color 'blue #:radius 8))

#<lesser>

procedure

(make-comparator function [#:name name])  comparator?

  function : (-> any/c any/c comparison?)
  name : (or/c interned-symbol? #f) = #f
Constructs a comparator named name that compares values by calling function. Most users should use comparator-map to wrap an existing comparator instead of constructing one directly.

Examples:
(define symbol<=>
  (make-comparator
   (λ (left right)
     (cond [(symbol<? left right) lesser]
           [(equal? left right) equivalent]
           [else greater]))
   #:name 'symbol<=>))

 

> (compare symbol<=> 'apple 'banana)

#<lesser>

> (compare symbol<=> 'apple 'aardvark)

#<greater>

procedure

(comparator-reverse comparator)  comparator?

  comparator : comparator?
Reverses comparator, returning a comparator that considers one value greater than another when comparator would consider it lesser and vice-versa.

Examples:
> (compare real<=> 2 5)

#<lesser>

> (compare (comparator-reverse real<=>) 2 5)

#<greater>

1.9.2 Predefined Comparators

A comparator that compares real numbers.

A comparator that lexicographically compares immutable strings. Mutable strings are disallowed, to prevent clients from concurrently mutating a string while it’s being compared.

1.9.3 Comparison Constants

procedure

(comparison? v)  boolean?

  v : any/c
A predicate for comparison constants.

A comparison constant indicating that the left value of a comparison is smaller than the right value.

A comparison constant indicating that the left value of a comparison is greater than the right value.

A comparison constant indicating that the left value of a comparison is equivalent to the right value. Note that equivalent values may not be equal?, depending on the comparator used.