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


2.38.8.7 Selecting comparators

Function: make-selecting-comparator comparator0 comparator

Return a comparator whose procedures make use of the comparator arguments as follows:

This procedure is analogous to the expression types select-compare and cond-compare from SRFI-67.

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

(define-constant C
  (make-selecting-comparator boolean-comparator
                             exact-integer-comparator
                             string-comparator))

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

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

;; comparison
(let ((compare (comparator-comparison-procedure C)))
  (compare #t #t)       ⇒ 0
  (compare #t #f)       ⇒ +1
  (compare #f #t)       ⇒ -1

  (compare 1 1)         ⇒ 0
  (compare 2 1)         ⇒ +1
  (compare 1 2)         ⇒ -1

  (compare "1" "1")     ⇒ 0
  (compare "2" "1")     ⇒ +1
  (compare "1" "2")     ⇒ -1

  (try
      (compare #t 1)
    (catch E
      ((&comparator-type-error)
       #t)
      (else E)))        ⇒ #t

  (try
      (compare #t "ciao")
    (catch E
      ((&comparator-type-error)
       #t)
      (else E)))        ⇒ #t

  (try
      (compare 1 "ciao")
    (catch E
      ((&comparator-type-error)
       #t)
      (else E))))       ⇒ #t

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

  (try
      (hash 1+2i)
    (catch E
      ((&comparator-type-error)
       #t)
      (else E))))       ⇒ #t

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