Debian Codegraph @

lib/srfi/235.scm

raw
1 ;;;
2 ;;; SRFI-235 - Combinators
3 ;;;
4 ;;; Copyright (c) 2023-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.235
35 (use gauche.collection) ;group-collection
36 (use util.match)
37 (export constantly ;builtin
38 complement ;builtin
39 flip ;builtin
40 swap ;builtin
41 on-left on-right
42 conjoin disjoin
43 each-of all-of any-of
44 on
45 left-section right-section
46 apply-chain
47 arguments-drop arguments-drop-right
48 arguments-take arguments-take-right
49 group-by
50 begin-procedure
51 if-procedure
52 when-procedure
53 unless-procedure
54 value-procedure
55 case-procedure
56 and-procedure eager-and-procedure
57 or-procedure eager-or-procedure
58 funcall-procedure loop-procedure
59 while-procedure until-procedure
60 always never
61 boolean ;builtin
62 )
63 )
64 (select-module srfi.235)
65
66 (define (on-left proc) (^[x y] (proc x)))
67 (define (on-right proc) (^[x y] (proc y)))
68
69 (define (conjoin . preds)
70 (if (null? preds)
71 (constantly #t)
72 (let1 pred* (apply every-pred preds)
73 (^ args (every pred* args)))))
74
75 (define (disjoin . preds)
76 (if (null? preds)
77 (constantly #f)
78 (let1 pred* (apply any-pred preds)
79 (^ args (if (null? args) #t (any pred* args))))))
80
81 (define (each-of . procs)
82 (^ args
83 (for-each (^p (apply p args)) procs)))
84
85 (define (all-of pred)
86 (^[xs] (every pred xs)))
87
88 (define (any-of pred)
89 (^[xs] (any pred xs)))
90
91 (define (on reducer mapper)
92 (^ xs (apply reducer (map mapper xs))))
93
94 (define (left-section proc . args)
95 (apply pa$ proc args))
96
97 (define (right-section proc . args)
98 (let1 rargs (reverse args)
99 (^ more-args (apply proc (append more-args rargs)))))
100
101 ;; Same as 'compose' except at least one proc is required.
102 (define (apply-chain proc . procs) (apply compose proc procs))
103
104 (define (arguments-drop proc n)
105 (^ args (apply proc (drop args n))))
106 (define (arguments-drop-right proc n)
107 (^ args (apply proc (drop-right args n))))
108 (define (arguments-take proc n)
109 (^ args (apply proc (take args n))))
110 (define (arguments-take-right proc n)
111 (^ args (apply proc (take-right args n))))
112
113 (define (group-by key-proc :optional (= equal?))
114 (^[lis]
115 (assume-type lis <list>)
116 (group-collection lis :key key-proc :test =)))
117
118 (define begin-procedure
119 (case-lambda
120 [() (undefined)]
121 [thunks
122 (let loop ([thunks thunks])
123 (if (null? (cdr thunks))
124 ((car thunks))
125 (begin ((car thunks)) (loop (cdr thunks)))))]))
126
127 (define (if-procedure value then-thunk else-thunk)
128 (if value (then-thunk) (else-thunk)))
129
130 (define (when-procedure value . thunks)
131 (when value
132 (apply begin-procedure thunks) (undefined)))
133
134 (define (unless-procedure value . thunks)
135 (unless value
136 (apply begin-procedure thunks) (undefined)))
137
138 (define (value-procedure value then-proc :optional (else-thunk undefined))
139 (if value (then-proc value) (else-thunk)))
140
141 (define (case-procedure value thunk-alist :optional (else-thunk undefined))
142 (if-let1 p (assv value thunk-alist)
143 ((cdr p))
144 (else-thunk)))
145
146 (define (and-procedure . thunks)
147 (every funcall-procedure thunks))
148
149 (define (eager-and-procedure . thunks)
150 (every identity (map funcall-procedure thunks)))
151
152 (define (or-procedure . thunks)
153 (any funcall-procedure thunks))
154
155 (define (eager-or-procedure . thunks)
156 (any identity (map funcall-procedure thunks)))
157
158 (define (funcall-procedure thunk)
159 (thunk))
160
161 (define (loop-procedure thunk)
162 (let loop () (thunk) (loop)))
163
164 (define (while-procedure thunk)
165 (let loop ()
166 (when (thunk) (loop))))
167
168 (define (until-procedure thunk)
169 (let loop ()
170 (unless (thunk) (loop))))
171
172 (define (always . objs) #t)
173 (define (never . objs) #f)