-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloopless.lisp
364 lines (331 loc) · 10 KB
/
loopless.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
(in-package #:cl-user)
(defpackage #:loopless
(:nicknames #:ll)
(:use #:cl)
#-loopless-noconflict
(:export #:with-collectors
#:collect
#:ncollect
#:collecting
#:compose)
(:export #:mapal
#:mapalist
#:mapacon
#:mapal*
#:mapalist*
#:mapacon*
#:mappl
#:mapplist
#:mappcon
#:mapv
#:mapvector
#:mapvcon
#:mapt
#:maptimes
#:maptcon
#:mapt*
#:maptimes*
#:maptcon*
#:for*
#:step*
#:doalist
#:doalist*
#:doplist
#:docons
#:dovector
#:dotimes*
#:while*))
(in-package #:loopless)
;;;; So, I'm sure some will note the "irony" of using LOOP in the
;;;; implementation of a library called Loopless which sole reason for
;;;; existing is using LOOP less, so I thought I'd address this: the
;;;; reason I'm using LOOP at some places here is that it makes the
;;;; implementation that much simpler and I'm also guessing that much
;;;; work has been invested in optimizing LOOP to death for common
;;;; cases so it makes sense to leverage that.
;;; COMPOSE, COLLECTING and WITH-COLLECTORS implementations
;;; originally from cl-utilities (with enhancements).
(defmacro with-collectors ((&rest collectors) &body body)
(multiple-value-bind (names collectors ncollectors tails)
(let (names the-collectors ncollectors tails)
(dolist (collector collectors)
(destructuring-bind (collector ncollector)
(etypecase collector
((and symbol (not null))
(list collector
(if (symbol-package collector)
(intern (format nil "N~A" collector)))))
((cons symbol (cons symbol null)) collector))
(let ((name (or collector
ncollector
(error "collector and ncollector can't both be nil!"))))
(push name names)
(push collector the-collectors)
(push ncollector ncollectors)
(push (gensym (format nil "~A-TAIL" name)) tails))))
(values (nreverse names)
(nreverse the-collectors)
(nreverse ncollectors)
(nreverse tails)))
`(let ,(mapcan #'list names tails)
(labels ,(mapcan
(lambda (name collector ncollector tail)
(nconc (if collector
(list `(,collector
(thing)
(if ,name
(setf (cdr ,tail)
(setf ,tail (list thing)))
(setf ,name
(setf ,tail (list thing))))
thing)))
(if ncollector
(list `(,ncollector
(list)
(if ,name
(setf (cdr ,tail) list)
(setf ,name list))
(if list
(setf ,tail (last list)))
nil)))))
names collectors ncollectors tails)
(declare (ignorable
,@(mapcan (lambda (collector ncollector)
(nconc (if collector
(list `#',collector))
(if ncollector
(list `#',ncollector))))
collectors ncollectors)))
,@body)
(values ,@names))))
;; These should only be called inside of COLLECTING macros, but we
;; define them here to provide informative error messages and to make
;; it easier for SLIME (et al.) to get documentation for the COLLECT
;; and NCOLLECT functions when they're used in the COLLECTING macro.
(defun collect (thing)
(error "Can't collect ~S outside the context of the COLLECTING macro"
thing))
(defun ncollect (list)
(error "Can't ncollect ~S outside the context of the COLLECTING macro"
list))
(defmacro collecting (&body body)
`(with-collectors (collect)
,@body))
(defun compose (&rest functions)
(cond ((not functions) (lambda (first-arg &rest ignored-rest)
(declare (ignore ignored-rest))
first-arg))
((not (cdr functions)) (car functions))
(t (let ((last-function (car functions))
(first-function (car (last functions)))
(butlast-position (1- (length functions))))
(lambda (&rest args)
(funcall last-function
(reduce #'funcall functions
:from-end t
:initial-value (apply first-function args)
:start 1
:end butlast-position)))))))
#+nil
(define-compiler-macro compose (&rest functions)
(labels ((sharp-quoted-p (x)
(and (listp x)
(eql (first x) 'function)
(symbolp (second x)))))
`(lambda (x) ,(reduce #'(lambda (fun arg)
(if (sharp-quoted-p fun)
(list (second fun) arg)
(list 'funcall fun arg)))
functions
:initial-value 'x
:from-end t))))
;; Laughably bad-style unidiomatic implementation.
;; But hey, it works!
(defmacro define-mapcar-like-family ((nil-acc list-acc nconc-acc)
var
contributed-args-per-element
end-test-form
fill-args-form
step-vars-form)
`(progn
,@(mapcar
(let ((more-vars (intern (format nil "MORE-~AS" var)))
(vars (intern (format nil "~AS" var)))
(loop (intern "LOOP")))
(lambda (name accumulation-type)
`(defun ,name (function ,var &rest ,more-vars)
(let* ((,vars (cons ,var ,more-vars))
(args (make-list
,(if (= contributed-args-per-element 1)
`(length ,vars)
`(* (length ,vars)
,contributed-args-per-element)))))
,(let ((core `(prog ()
,loop
(if ,end-test-form
(return-from ,name ,(if accumulation-type
'collect
var)))
,fill-args-form
,(let ((core `(apply function args)))
(if accumulation-type
(list (ecase accumulation-type
(list 'collect)
(nconc 'ncollect))
core)
core))
,step-vars-form
(go ,loop))))
(if accumulation-type
`(collecting ,core)
core))))))
(list nil-acc list-acc nconc-acc)
'(nil list nconc))))
(define-mapcar-like-family (mapal mapalist mapacon) alist
2
(notevery #'identity alists)
(let ((args args))
(dolist (alist alists)
(let ((entry (car alist)))
(setf (car args) (car entry)
(second args) (cdr entry)
args (cddr args)))))
(map-into alists #'cdr alists))
(define-mapcar-like-family (mapal* mapalist* mapacon*) alist
2
(notevery #'identity alists)
(let ((args args))
(dolist (alist alists)
(let ((entry (car alist)))
(setf (car args) (car entry)
(second args) (second entry)
args (cddr args)))))
(map-into alists #'cdr alists))
(define-mapcar-like-family (mappl mapplist mappcon) plist
2
(notevery #'identity plists)
(let ((args args))
(dolist (plist plists)
(setf (car args) (first plist)
(second args) (second plist)
args (cddr args))))
(map-into plists #'cddr plists))
(defun mapv (function vector &rest more-vectors)
(apply #'map nil function vector more-vectors))
(defun mapvector (function vector &rest more-vectors)
(apply #'map 'list function vector more-vectors))
(defun mapvcon (function vector &rest more-vectors)
(collecting
(let ((vectors (cons vector more-vectors)))
(dotimes (i (apply #'max
(mapcar #'length vectors)))
(ncollect (apply function (mapcar (lambda (vector)
(aref vector i))
vectors)))))))
(defun mapt (function count)
(let ((function (coerce function 'function)))
(dotimes (i count)
(funcall function i))))
(defun maptimes (function count)
(collecting
(let ((function (coerce function 'function)))
(dotimes (i count)
(collect (funcall function i))))))
(defun maptcon (function count)
(collecting
(let ((function (coerce function 'function)))
(dotimes (i count)
(ncollect (funcall function i))))))
(defun mapt* (function count)
(let ((function (coerce function 'function)))
(dotimes (ignored count)
(funcall function))))
(defun maptimes* (function count)
(collecting
(let ((function (coerce function 'function)))
(dotimes (ignored count)
(collect (funcall function))))))
(defun maptcon* (function count)
(collecting
(let ((function (coerce function 'function)))
(dotimes (ignored count)
(ncollect (funcall function))))))
(defmacro for* (bindings &body body)
(let ((vars-inits-steps
(flet ((ck (symbol) ; Coerce to Keyword
(if symbol
(if (keywordp symbol)
symbol
(intern (symbol-name symbol) '#:keyword)))))
(mapcar
(lambda (binding)
(let ((var (car binding)))
(cons var
(let ((stepping (cdr binding)))
(flet ((from (function)
(list (second stepping)
`(,function ,var ,(if (eq (ck (third stepping)) :by)
(fourth stepping)
1)))))
(ecase (ck (car stepping))
((:from :upfrom) (from '+))
(:downfrom (from '-))
(:= (list (second stepping)
(if (eq (ck (third stepping)) :then)
(fourth stepping)
(second stepping))))))))))
bindings))))
(let ((vars (mapcar #'first vars-inits-steps))
(inits (mapcar #'second vars-inits-steps))
(steps (mapcar #'third vars-inits-steps)))
(let ((first-time-p (gensym "FIRST-TIME-P")))
`(let* ((,first-time-p t)
,@vars)
(flet ((step* ()
(if ,first-time-p
(setf ,first-time-p nil
,@(mapcan #'list vars inits))
(setf ,@(mapcan #'list vars steps)))
nil))
,@body))))))
(defmacro doalist ((key value alist &optional result) &body body)
(let ((assoc (gensym "ASSOC")))
`(dolist (,assoc ,alist ,result)
(let ((,key (car ,assoc))
(,value (cdr ,assoc)))
(prog () ,@body)))))
(defmacro doalist* ((key value alist &optional result) &body body)
(let ((assoc (gensym "ASSOC")))
`(dolist (,assoc ,alist ,result)
(let ((,key (first ,assoc))
(,value (second ,assoc)))
(prog () ,@body)))))
(defmacro doplist ((key value plist &optional result) &body body)
`(block nil
(mappl (lambda (,key ,value)
(prog () ,@body))
,plist)
,result))
(defmacro docons ((var list &optional result) &body body)
`(block nil
(mapl (lambda (,var)
(prog () ,@body))
,list)
,result))
(defmacro dovector ((var vector &optional result) &body body)
`(block nil
(map 'nil (lambda (,var)
(prog () ,@body))
,vector)
,result))
(defmacro dotimes* ((count &optional result) &body body)
(let ((ignored-var (gensym "IGNORED-VAR")))
`(dotimes (,ignored-var ,count ,@(if result (list result)))
,@body)))
(defmacro while* (test &body body)
`(prog ()
loop-start
(if (not ,test)
(return))
(prog () ,@body)
(go loop-start)))