Next: , Previous: , Up: srfi comparators constructors   [Index]


2.38.8.9 Reverse comparators

Function: make-reverse-comparator comparator

Return a comparator that behaves like comparator, except that the compare procedure returns +1, 0, and -1 instead of -1, 0, and +1 respectively. This allows ordering in reverse.

Here a usage example:

#!vicare
(import (vicare) (srfi :114))

(define-constant C
  (make-reverse-comparator exact-integer-comparator))

;; type test
(let ((test-type (comparator-test-type-procedure C)))
  (test-type 1)         ⇒ #t
  (test-type '())       ⇒ #f
  (test-type "ciao"))   ⇒ #f

;; type check
(let ((check-type (comparator-check-type-procedure C)))
  (check-type 1)        ⇒ #t
  (try
      (check-type (void))
    (catch E
      ((&comparator-type-error)
       #t)
      (else E))))       ⇒ #t

;; comparison
(let ((compare (comparator-comparison-procedure C)))
  (compare 1 1)         ⇒ 0
  (compare 1 2)         ⇒ +1
  (compare 2 1)         ⇒ -1)

;; hash
(let ((hash (comparator-hash-function C)))
  (non-negative-exact-integer? (hash 1)))       ⇒ #t