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


2.38.8.3 Pair comparators

Function: make-pair-comparator car-comparator cdr-comparator

Return a comparator that compares pairs first on their cars using car-comparator. If the cars are equal: it compares the cdrs using cdr-comparator. The hash function makes use of both the car and the cdr.

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

(define-constant C
  (make-pair-comparator exact-integer-comparator
                        real-comparator))

;; type test
(comparator-test-type C '(1 . 2.0))     ⇒ #t
(comparator-test-type C '(1 . 2.0))     ⇒ #t
(comparator-test-type C '())            ⇒ #f
(comparator-test-type C '(1 . 2+1i))    ⇒ #f
(comparator-test-type C "ciao")         ⇒ #f

;; type check
(comparator-check-type C '(1 . 2.0))    ⇒ #t
(try
    (comparator-check-type C (void))
  (catch E
    ((&comparator-type-error)
     #t)
    (else E)))
⇒ #t

;; comparison
(comparator-compare C '(1 . 2.0) '(1 . 2.0))   ⇒ 0
(comparator-compare C '(1 . 2.0) '(1 . 3))     ⇒ -1
(comparator-compare C '(1 . 3)   '(1 . 2.0))   ⇒ +1

;; hash
(non-negative-exact-integer?
   (comparator-hash C '(1 . 2.0)))      ⇒ #t
Function: make-car-comparator comparator

Return a comparator that compares pairs on their cars alone using comparator. The hash function makes use of the car only.

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

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

;; type test
(comparator-test-type C '(1 . 2.0))     ⇒ #t
(comparator-test-type C '(1 . 2.0))     ⇒ #t
(comparator-test-type C '(1 . 2+1i))    ⇒ #t
(comparator-test-type C '(2.0 . 1))     ⇒ #f
(comparator-test-type C '())            ⇒ #f
(comparator-test-type C "ciao")         ⇒ #f

;; type check
(comparator-check-type C '(1 . 2.0))    ⇒ #t
(try
    (comparator-check-type C (void))
  (catch E
    ((&comparator-type-error)
     #t)
    (else E)))
⇒ #t

;; comparison
(comparator-compare C '(1 . 2) '(1 . 3))        ⇒ 0
(comparator-compare C '(1 . 2) '(2 . 3))        ⇒ -1
(comparator-compare C '(2 . 2) '(1 . 2))        ⇒ +1

;; hash
(non-negative-exact-integer?
   (comparator-hash C '(1 . 2.0)))      ⇒ #t
Function: make-cdr-comparator comparator

Return a comparator that compares pairs on their cdrs alone using comparator. The hash function makes use of the cdr only.

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

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

;; type test
(comparator-test-type C '(2.0 . 1))     ⇒ #t
(comparator-test-type C '(2.0 . 1))     ⇒ #t
(comparator-test-type C '(2+1i . 1))    ⇒ #t
(comparator-test-type C '(1 . 2.0))     ⇒ #t
(comparator-test-type C '())            ⇒ #f
(comparator-test-type C "ciao")         ⇒ #f

;; type check
(comparator-check-type C '(2.0 . 1))    ⇒ #t
(try
    (comparator-check-type C (void))
  (catch E
    ((&comparator-type-error)
     #t)
    (else E)))
⇒ #t

;; comparison
(comparator-compare C '(2 . 1) '(3 . 1))        ⇒ 0
(comparator-compare C '(2 . 1) '(3 . 2))        ⇒ -1
(comparator-compare C '(2 . 2) '(2 . 1))        ⇒ +1

;; hash
(non-negative-exact-integer?
   (comparator-hash C '(2.0 . 1)))      ⇒ #t

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