Debian Codegraph @

lib/srfi/228.scm

raw
1 ;;;
2 ;;; SRFI-228 - Composing comparators
3 ;;;
4 ;;; Copyright (c) 2022-2024 Shiro Kawai <[email protected]>
5 ;;;
6 ;;; Redistribution and use in source and binary forms, with or without
7 ;;; modification, are permitted provided that the following conditions
8 ;;; are met:
9 ;;;
10 ;;; 1. Redistributions of source code must retain the above copyright
11 ;;; notice, this list of conditions and the following disclaimer.
12 ;;;
13 ;;; 2. Redistributions in binary form must reproduce the above copyright
14 ;;; notice, this list of conditions and the following disclaimer in the
15 ;;; documentation and/or other materials provided with the distribution.
16 ;;;
17 ;;; 3. Neither the name of the authors nor the names of its contributors
18 ;;; may be used to endorse or promote products derived from this
19 ;;; software without specific prior written permission.
20 ;;;
21 ;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 ;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 ;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 ;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 ;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 ;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27 ;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 ;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 ;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 ;;;
33
34 (define-module srfi.228
35 (export make-wrapper-comparator
36 make-product-comparator
37 make-sum-comparator
38 comparator-one
39 comparator-zero))
40 (select-module srfi.228)
41
42 (define (make-wrapper-comparator type-test unwrap contents-comparator)
43 (make-comparator
44 type-test
45 (^[a b] (=? contents-comparator (unwrap a) (unwrap b)))
46 (and (comparator-ordered? contents-comparator)
47 (^[a b] (<? contents-comparator (unwrap a) (unwrap b))))
48 (and (comparator-hashable? contents-comparator)
49 (^[a] (comparator-hash contents-comparator (unwrap a))))))
50
51 (define-constant comparator-one
52 (make-comparator (constantly #t)
53 (constantly #t)
54 (constantly #f)
55 (constantly 0)))
56
57 (define-constant comparator-zero
58 (make-comparator (constantly #f)
59 (^[a b] (error "Can't test equality with this comparator"))
60 #f #f))
61
62 (define (make-product-comparator . cmps)
63 (if (null? cmps)
64 comparator-one
65 (make-comparator
66 (^[a] (every (cut comparator-test-type <> a) cmps))
67 (^[a b] (let loop ([cmps cmps])
68 (cond [(null? cmps) #t]
69 [(=? (car cmps) a b) (loop (cdr cmps))]
70 [else #f])))
71 (and (every comparator-ordered? cmps)
72 (^[a b] (let loop ([cmps cmps])
73 (cond [(null? cmps) #f]
74 [(<? (car cmps) a b) #t]
75 [(=? (car cmps) a b) (loop (cdr cmps))]
76 [else #f]))))
77 (and (every comparator-hashable? cmps)
78 (^[a]
79 (fold (^[cmp h] (combine-hash-value h (comparator-hash cmp a)))
80 (hash-salt)
81 cmps))))))
82
83 (define (select-cmp cmps obj)
84 (let loop ((cmps cmps) (index 0))
85 (cond [(null? cmps) (values #f #f)]
86 [(comparator-test-type (car cmps) obj) (values (car cmps) index)]
87 [else (loop (cdr cmps) (+ index 1))])))
88
89 (define (make-sum-comparator . cmps)
90 (if (null? cmps)
91 comparator-zero
92 (make-comparator
93 (^[a] (any (cut comparator-test-type <> a) cmps))
94 (^[a b] (let loop ([cmps cmps])
95 (cond [(null? cmps) #f]
96 [(comparator-test-type (car cmps) a)
97 (and (comparator-test-type (car cmps) b)
98 (=? (car cmps) a b))]
99 [else (loop (cdr cmps))])))
100 (and (every comparator-ordered? cmps)
101 (^[a b]
102 (let loop ([cmps cmps])
103 (cond [(null? cmps) #f]
104 [(comparator-test-type (car cmps) a)
105 (if (comparator-test-type (car cmps) b)
106 (<? (car cmps) a b)
107 #t)]
108 [(comparator-test-type (car cmps) b) #f]
109 [else (loop (cdr cmps))]))))
110 (and (every comparator-hashable? cmps)
111 (^[a] (if-let1 cmp (find (cut comparator-test-type <> a) cmps)
112 (comparator-hash cmp a)
113 0))))))