-
Notifications
You must be signed in to change notification settings - Fork 0
/
if-letstar.lisp
247 lines (208 loc) · 9.29 KB
/
if-letstar.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
(defpackage #:if-letstar
(:nicknames #:if-let*)
(:use #:cl)
(:export #:if-let*
#:if-let
#:ifn-let
#:when-let
#:when-let*
#:on-fail
#:it))
(in-package :if-letstar)
(defmacro if-let (bindings &body bodies)
"Creates new symbol bindings, and conditionally executes either
either the second-last or last form of BODIES. the last form defaults to NIL.
BINDINGS must be either single binding of the form:
(symbol initial-form)
or a list of bindings of the form:
((symbol-1 initial-form-1)
(symbol-2 initial-form-2)
...
(symbol-n initial-form-n))
All initial-forms are executed sequentially in the specified order. Then all
the symbol are bound to the corresponding values.
Any declarations can come below the bindings form, before the start of any significant code in BODIES.
If all the variables are true, the THEN-FORM is executed with the
bindings in effect, otherwise the ELSE-FORM is executed with the bindings in
effect."
(let* ((on-fail (find 'on-fail bindings :key 'car))
(bindings (remove on-fail bindings))
(binding-list (if (and (consp bindings) (symbolp (car bindings)))
(list bindings)
bindings))
(variables (mapcar #'car binding-list)))
(multiple-value-bind (decls body)
(loop for b in bodies
if (and (listp b)
(or (eq 'declare (car b))
(eq 'locally (car b))))
collect b into decls
else collect b into body
finally (return (values decls body)))
`(let ,binding-list
,@decls
(if (and ,@variables)
,(car body)
,@(if (cdr on-fail)
`((let ((it (remove nil
`(,,@(loop for var in variables
collect `(and (not ,var) ',var))))))
,@(cdr on-fail)
,(cadr body)))
(cdr body)))))))
(defmacro ifn-let (bindings else then)
"IF-LET, but the else clause comes first."
`(if-let ,bindings
,then
,else))
(defmacro when-let (bindings &body body)
"Creates new symbol bindings, and conditionally executes BODY.
([] means docs from alexandria:)
[BINDINGS must be either single binding of the form:
(symbol initial-form)
or a list of bindings of the form:
((symbol-1 initial-form-1)
(symbol-2 initial-form-2)
...
(symbol-n initial-form-n))
All initial-forms are executed sequentially in the specified order. Then all
the symbols are bound to the corresponding values.]
(not from alexandria:)
Any declarations can come below the bindings form, before the start of any significant code in BODIES.
If all the variables are true, then forms in BODY are executed as an
implicit PROGN."
(let* ((on-fail (find 'on-fail bindings :key 'car))
(bindings (remove on-fail bindings))
(binding-list (if (and (consp bindings) (symbolp (car bindings)))
(list bindings)
bindings))
(variables (mapcar #'car binding-list)))
(multiple-value-bind (decls body)
(loop for b in body
if (and (listp b)
(or (eq 'declare (car b))
(eq 'locally (car b))))
collect b into decls
else collect b into body
finally (return (values decls body)))
`(let ,binding-list
,@decls
,(if (not (cdr on-fail))
`(when (and ,@variables)
,@body)
`(if (and ,@variables)
(progn ,@body)
(let ((it (remove nil
`(,,@(loop for var in variables
collect `(and (not ,var) ',var))))))
,@(cdr on-fail)
nil)))))))
(defmacro when-let* (bindings &body body)
"Creates new symbol bindings, and conditionally executes BODY.
([] means docs from alexandria:)
[BINDINGS must be either single binding of the form:
(symbol initial-form)
or a list of bindings of the form:
((symbol-1 initial-form-1)
(symbol-2 initial-form-2)
...
(symbol-n initial-form-n))
Each INITIAL-FORM is executed in turn, and the symbol bound to the
corresponding value. INITIAL-FORM expressions can refer to variables
previously bound by the WHEN-LET*.]
(not from alexandria:)
Additionally, (if-letstar:on-fail (break)) in bindings can be used to splice code on failure.
Any declarations can come below the bindings form, before the start of any significant code in BODIES.
[Execution of WHEN-LET* stops immediately if any INITIAL-FORM evaluates to NIL.
If all INITIAL-FORMs evaluate to true, then BODY is executed as an implicit
PROGN.]"
(let* ((on-fail (find 'on-fail bindings :key 'car))
(bindings (remove on-fail bindings))
(binding-list (if (and (consp bindings) (symbolp (car bindings)))
(list bindings)
bindings))
(values-list (mapcar #'cadr binding-list))
(target-variables (mapcar #'car binding-list))
(fake-vars (loop repeat (length bindings) collect (gensym)))
(fake-to-bindings (loop for f in fake-vars
for (s v2) in binding-list
collect `(,s ,f)))
(fake-variables (loop repeat (length bindings)
for s in fake-vars
for v in values-list
collect `(,s ,v))))
(multiple-value-bind (decls body)
(loop for b in body
if (and (listp b)
(or (eq 'declare (car b))
(eq 'locally (car b))))
collect b into decls
else collect b into body
finally (return (values decls body)))
`(let ,(mapcar 'cadr fake-to-bindings)
,(if binding-list
(labels ((bind (b tgt)
`(if (setq ,@(car b))
,(if (cdr b)
`(let ((,(car tgt) ,(caar b)))
,(bind (cdr b) (cdr tgt)))
`(let* ,fake-to-bindings
,@decls
,@body))
,@(when (cdr on-fail)
`((let ((it ',(caar b)))
,@(cdr on-fail)))))))
(bind fake-variables target-variables)))))))
(defmacro if-let* (bindings &body bodies)
"Creates new symbol bindings, and conditionally executes either the second-last or last form of BODIES.
([] means docs from alexandria:)
[BINDINGS must be either single binding of the form:
(symbol initial-form)
or a list of bindings of the form:
((symbol-1 initial-form-1)
(symbol-2 initial-form-2)
...
(symbol-n initial-form-n))]
(not from alexandria:)
Additionally, (if-letstar:on-fail (break)) in bindings can be used to splice code on failure.
Any declarations can come below the bindings form, before the start of any significant code in BODIES.
Each INITIAL-FORM is executed in turn, and the variable bound to the
corresponding value. INITIAL-FORM expressions can refer to symbols
previously bound by the IF-LET*.
Execution of IF-LET* causes the last form of BODIES to evaluate if any INITIAL-FORM evaluates to NIL.
If all INITIAL-FORMs evaluate to true, then the second-last form of BODIES is executed."
(let* ((on-fail (find 'on-fail bindings :key 'car))
(bindings (remove on-fail bindings))
(binding-list (if (and (consp bindings) (symbolp (car bindings)))
(list bindings)
bindings))
(values-list (mapcar #'cadr binding-list))
(target-variables (mapcar #'car binding-list))
(fake-vars (loop repeat (length bindings) collect (gensym)))
(fake-variables (loop repeat (length bindings)
for s in fake-vars
for v in values-list
collect `(,s ,v)))
(true-to-fake (mapcar 'list target-variables fake-vars)))
(multiple-value-bind (decls body)
(loop for b in bodies
if (and (listp b)
(or (eq 'declare (car b))
(eq 'locally (car b))))
collect b into decls
else collect b into body
finally (return (values decls body)))
`(let ,fake-vars
(if ,(if binding-list
(labels ((bind (b tgt)
`(if (setq ,@(car b))
,(if (cdr b)
`(let ((,(car tgt) ,(caar b)))
,(bind (cdr b) (cdr tgt)))
t)
,@(when (cdr on-fail)
`((let ((it ',(caar b)))
,@(cdr on-fail)))))))
(bind fake-variables target-variables)))
,@(loop for body in body
collect `(let ,true-to-fake ,@decls ,body)))))))