forked from coalton-lang/coalton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iterator.lisp
453 lines (394 loc) · 16.7 KB
/
iterator.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
(coalton-library/utils:defstdlib-package #:coalton-library/iterator
(:shadow #:empty)
(:use
#:coalton
#:coalton-library/classes
#:coalton-library/builtin
#:coalton-library/functions
#:coalton-library/optional
#:coalton-library/tuple
#:coalton-library/char)
(:local-nicknames
(#:list #:coalton-library/list)
(#:string #:coalton-library/string)
(#:cell #:coalton-library/cell)
(#:vector #:coalton-library/vector)
(#:hashtable #:coalton-library/hashtable))
(:import-from
#:coalton-library/vector
#:Vector)
(:import-from
#:coalton-library/hashtable
#:Hashtable)
(:export
#:Iterator
#:new
#:next!
#:fold!
#:empty
#:list-iter
#:vector-iter
#:string-chars
#:recursive-iter
#:range-increasing
#:up-to
#:up-through
#:range-decreasing
#:down-from
#:count-forever
#:repeat-forever
#:repeat-item
#:char-range
#:zip!
#:zipWith!
#:enumerate!
#:filter!
#:take!
#:flatten!
#:chain!
#:concat!
#:remove-duplicates!
#:pair-with!
#:sum!
#:and!
#:or!
#:count!
#:for-each!
#:find!
#:index-of!
#:optimize!
#:max!
#:min!
#:every!
#:any!
#:collect-list!
#:collect-vector-size-hint!
#:collect-vector!
#:collect-hashtable!))
#+coalton-release
(cl:declaim #.coalton-impl:*coalton-optimize-library*)
(cl:in-package #:coalton-library/iterator)
;;; fundamental operators
(coalton-toplevel
(repr :transparent)
(define-type (Iterator :elt)
"A forward-moving pointer into an ordered sequence of :ELTs"
(%Iterator (Unit -> (Optional :elt))))
(declare new ((Unit -> Optional :elt) -> Iterator :elt))
(define new %Iterator)
(declare next! (Iterator :elt -> Optional :elt))
(define (next! iter)
"Advance ITER, returning its next yielded value, or `None` if the iterator is exhausted.
Behavior is undefined if two threads concurrently call `next!` on the same iterator without a lock. Note that
most of the operators defined on iterators call `next!` internally, or create new iterators which will call
`next!` on their inputs."
(match iter
((%Iterator func) (func Unit))))
(declare fold! ((:state -> :elt -> :state) -> :state -> Iterator :elt -> :state))
(define (fold! func init iter)
"Tail recursive in-order fold. Common Lisp calls this operation `reduce`.
If ITER is empty, returns INIT. Otherwise, calls (FUNC STATE ITEM) for each ITEM of ITER to produce a new
STATE, using INIT as the first STATE."
(match (next! iter)
((Some item) (fold! func
(func init item)
iter))
((None) init)))
;;; instances
;; should iterator implement applicative or monad? the only law-abiding applicative instances are
;; pathological, so i doubt it.
(define-instance (Functor Iterator)
(define (map func iter)
(%Iterator (fn () (map func (next! iter))))))
;;; constructors
;; once coalton gets functional dependencies, associated types or type families, much of this will be
;; abstracted into a class `(IntoIterator :collection :item)', with instances like `(IntoIterator (List :elt)
;; :elt)' and `(IntoIterator String Char)'. It's currently not possible for Coalton to do useful type
;; inference on these classes, so we're stuck with monomorphic constructors like `list-iter`.
(declare empty (Iterator :any))
(define empty
"Yields nothing; stops immediately"
(%Iterator (fn () None)))
(declare list-iter ((List :elt) -> (Iterator :elt)))
(define (list-iter lst)
"Yield successive elements of LST.
Behavior is undefined if the iterator is advanced after a destructive modification of LST."
(let ((remaining (cell:new lst)))
(%Iterator (fn () (cell:pop! remaining)))))
(declare vector-iter (Vector :elt -> Iterator :elt))
(define (vector-iter vec)
"Yield successive elements of VEC.
Behavior is undefined if the iterator is advanced after a destructive modification of VEC."
(map ((flip vector:index-unsafe) vec)
(up-to (fromInt (vector:length vec)))))
(declare string-chars (String -> Iterator Char))
(define (string-chars str)
"Yield successive `Char`s from STR.
Behavior is undefined if the iterator is advanced after a destructive modification of STR."
(map (string:ref-unchecked str)
(up-to (fromInt (string:length str)))))
(declare recursive-iter ((:elt -> :elt) -> (:elt -> Boolean) -> :elt -> Iterator :elt))
(define (recursive-iter succ done? start)
"An iterator which yields first START, then (SUCC START), then (SUCC (SUCC START)), and so on, stopping as soon as such a value is `done?`.
Beware off-by-one errors: the first value which is `done?` is not yielded. If `(done? start)' is true, the
iterator is empty."
(let ((next (cell:new start)))
(%Iterator
(fn ()
(let ((this (cell:read next)))
(if (done? this)
None
(Some (cell:update-swap! succ next))))))))
(declare range-increasing ((Num :num) (Ord :num) =>
:num ->
:num ->
:num ->
Iterator :num))
(define (range-increasing step start end)
"An iterator which begins at START and yields successive elements spaced by STEP, stopping before END."
(assert (>= end start)
"END ~a should be greater than or equal to START ~a in RANGE-INCREASING"
end start)
(assert (> step 0)
"STEP ~a should be positive and non-zero in RANGE-INCREASING"
step)
(recursive-iter (+ step) (<= end) start))
(declare up-to ((Num :num) (Ord :num) => :num -> Iterator :num))
(define (up-to limit)
"An iterator which begins at zero and counts up to, but not including, LIMIT."
(range-increasing 1 0 limit))
(declare up-through ((Num :num) (Ord :num) => :num -> Iterator :num))
(define (up-through limit)
"An iterator which begins at zero and counts up through and including LIMIT."
(up-to (+ 1 limit)))
(declare range-decreasing ((Num :num) (Ord :num) =>
:num ->
:num ->
:num ->
(Iterator :num)))
(define (range-decreasing step start end)
"A range which begins below START and counts down through and including END by STEP.
Equivalent to reversing `range-increasing`"
(assert (<= end start)
"END ~a should be less than or equal to START ~a in RANGE-INCREASING"
end start)
(assert (> step 0)
"STEP ~a should be positive and non-zero in RANGE-INCREASING"
step)
;; FIXME: avoid underflow in the DONE? test
(recursive-iter ((flip -) step)
(fn (n) (>= end (+ n step))) ; like (>= (- end step)), but without potential underflow
(- start step) ; begin after START
))
(declare down-from ((Num :num) (Ord :num) => :num -> Iterator :num))
(define (down-from limit)
"An iterator which begins below the provided limit and counts down through and including zero."
(range-decreasing 1 limit 0))
(declare count-forever ((Num :num) (Ord :num) => Unit -> Iterator :num))
(define (count-forever _)
"An infinite iterator which starts at 0 and counts upwards by 1."
(recursive-iter (+ 1)
(const False)
0))
(declare repeat-forever (:item -> Iterator :item))
(define (repeat-forever item)
"Yield ITEM over and over, infinitely."
(%Iterator
(fn ()
(Some item))))
(declare repeat-item (:item -> UFix -> Iterator :item))
(define (repeat-item item count)
"Yield ITEM COUNT times, then stop."
(take! count (repeat-forever item)))
(declare char-range (Char -> Char -> Iterator Char))
(define (char-range start end)
"An inclusive range of characters from START to END by cl:char-code."
(map (compose unwrap code-char)
(range-increasing 1
(char-code start)
(+ 1 (char-code end)))))
;;; combinators
;; these are named with a !, even though they're lazy and therefore do not actually mutate anything on their
;; own, for two reasons:
;; 1. it prevents name conflicts with list operators
;; 2. to emphasize the flow of mutable data
(declare zipWith! ((:left -> :right -> :out) -> Iterator :left -> Iterator :right -> Iterator :out))
(define (zipWith! f left right)
"Return an iterator of elements from LEFT and RIGHT which terminates as soon as either LEFT or RIGHT does."
(%Iterator
(fn ()
(match (Tuple (next! left) (next! right))
((Tuple (Some l) (Some r)) (Some (f l r)))
(_ None)))))
(declare zip! (Iterator :left -> Iterator :right -> Iterator (Tuple :left :right)))
(define zip!
"Return an iterator of tuples contining elements from two iterators."
(zipWith! Tuple))
(declare enumerate! (Iterator :elt -> Iterator (Tuple UFix :elt)))
(define (enumerate! iter)
"Pair successive zero-based incides with elements from ITER"
(zip! (count-forever) iter))
(declare filter! ((:elt -> Boolean) -> Iterator :elt -> Iterator :elt))
(define (filter! keep? iter)
"Return an iterator over the elements from ITER for which KEEP? returns true."
(let ((filter-iter (fn (u)
(match (next! iter)
((None) None)
((Some candidate) (if (keep? candidate)
(Some candidate)
(filter-iter u)))))))
(%Iterator filter-iter)))
(declare take! (UFix -> Iterator :elt -> Iterator :elt))
(define (take! count iter)
"An `Iterator` which yields at most COUNT elements from ITER."
(map fst
(zip! iter
(up-to count))))
(declare chain! (Iterator :elt -> Iterator :elt -> Iterator :elt))
(define (chain! iter1 iter2)
(%iterator
(fn ()
(match (next! iter1)
((None) (next! iter2))
((Some el) (Some el))))))
(declare flatten! (Iterator (Iterator :elt) -> Iterator :elt))
(define (flatten! iters)
"Yield all the elements from each of the ITERS in order."
(match (next! iters)
((None) empty)
((Some first)
(let ((current (cell:new first))
(flatten-iter-inner
(fn ()
(match (next! (cell:read current))
((Some elt) (Some elt))
((None) (match (next! iters)
((Some next-iter) (progn (cell:write! current next-iter)
(flatten-iter-inner)))
((None) None)))))))
(%Iterator flatten-iter-inner)))))
(declare concat! (Iterator :elt -> Iterator :elt -> Iterator :elt))
(define (concat! first second)
"Yield all the elements of FIRST followed by all the elements from SECOND."
(flatten! (list-iter (make-list first second))))
(declare remove-duplicates! (Hash :elt => Iterator :elt -> Iterator :elt))
(define (remove-duplicates! iter)
"Yield unique elements from ITER in order of first appearance."
(let ((already-seen (hashtable:new))
(unique? (fn (elt)
(match (hashtable:get already-seen elt)
((Some (Unit)) False)
((None) (progn (hashtable:set! already-seen elt Unit)
True))))))
(filter! unique? iter)))
(declare pair-with! ((:key -> :value) -> Iterator :key -> Iterator (Tuple :key :value)))
(define (pair-with! func keys)
"Returns an iterator over tuples whose FSTs are elements from KEYS, and whose SNDs are the results of applying FUNC to those KEYS."
(map (fn (key) (Tuple key (func key)))
keys))
;;; consumers
(declare and! (Iterator Boolean -> Boolean))
(define and! (fold! boolean-and True))
(declare or! (Iterator Boolean -> Boolean))
(define or! (fold! boolean-or False))
(declare sum! (Num :num => Iterator :num -> :num))
(define (sum! iter)
"Add together all the elements of ITER."
(fold! + 0 iter))
(declare count! (Iterator :elt -> Integer))
(define (count! iter)
"Return the number of elements in ITER.
This operation could be called `length!`, but `count!` emphasizes the fact that it consumes ITER, and
afterwards, ITER will be exhausted."
(sum! (map (const 1) iter)))
(declare for-each! ((:elt -> :any) -> Iterator :elt -> Unit))
(define (for-each! thunk iter)
"Call THUNK on each element of ITER in order for side effects.
Discard values returned by THUNK."
(fold! (fn (u elt) (thunk elt) u)
Unit
iter))
(declare find! ((:elt -> Boolean) -> Iterator :elt -> Optional :elt))
(define (find! this? iter)
"Return the first element of ITER for which THIS? returns `True`, or `None` if no element matches."
(match (next! iter)
((Some elt) (if (this? elt)
(Some elt)
(find! this? iter)))
((None) None)))
(declare index-of! ((:elt -> Boolean) -> Iterator :elt -> Optional UFix))
(define (index-of! this? iter)
"Return the zero-based index of the first element of ITER for which THIS? is `True`, or `None` if no element matches."
(map fst
(find! (compose this? snd)
(enumerate! iter))))
(declare optimize! ((:elt -> :elt -> Boolean) -> Iterator :elt -> Optional :elt))
(define (optimize! better? iter)
"For an order BETTER? which returns `True` if its first argument is better than its second argument, return the best element of ITER.
Return `None` if ITER is empty."
(match (next! iter)
((None) None)
((Some first)
(Some
(fold! (fn (best new)
(if (better? new best) new best))
first
iter)))))
(declare max! (Ord :num => Iterator :num -> Optional :num))
(define (max! iter)
"Return the most-positive element of ITER, or `None` if ITER is empty."
(optimize! > iter))
(declare min! (Ord :num => Iterator :num -> Optional :num))
(define (min! iter)
"Return the most-negative element of ITER, or `None` if ITER is empty."
(optimize! < iter))
(declare every! ((:elt -> Boolean) -> Iterator :elt -> Boolean))
(define (every! good? iter)
"Return `True` if every element of ITER is GOOD?, or `False` as soon as any element is not GOOD?.
Returns `True` if ITER is empty."
(match (next! iter)
((None) True)
((Some item) (and (good? item) (every! good? iter)))))
(declare any! ((:elt -> Boolean) -> Iterator :elt -> Boolean))
(define (any! good? iter)
"Return `True` as soon as any element of ITER is GOOD?, or `False` if none of them are.
Returns `False` if ITER is empty."
(isSome (find! good? iter)))
;;; collecting
;; as with `IntoIterator`, these will one day be abstracted into a class `(FromIterator :collection :item)'.
(declare collect-list! (Iterator :elt -> List :elt))
(define (collect-list! iter)
"Construct a `List` containing all the elements from ITER in order."
(list:reverse (fold! (flip Cons) Nil iter)))
(declare collect-vector-size-hint! (Integer -> Iterator :elt -> Vector :elt))
(define (collect-vector-size-hint! size iter)
"Construct a `Vector` with initial allocation for SIZE elements, and fill it with all the elements from ITER in order.
The vector will be resized if ITER contains more than SIZE elements."
(let v = (vector:with-capacity size))
(for-each! ((flip vector:push!) v) iter)
v)
(declare collect-vector! (Iterator :elt -> Vector :elt))
(define (collect-vector! iter)
"Construct a `Vector` containing all the elements from ITER in order."
(collect-vector-size-hint! 0 iter))
(declare collect-hashtable-size-hint!
((Hash :key) =>
UFix ->
(Iterator (Tuple :key :value)) ->
(HashTable :key :value)))
(define (collect-hashtable-size-hint! size iter)
"Construct a `HashTable` with initial allocation for SIZE key/value pairs, and fill it with all the key/value pairs from ITER.
If a key appears in ITER multiple times, the resulting table will contain its last corresponding value.
The table will be resized if ITER contains more than SIZE unique keys."
(let ht = (hashtable:with-capacity (into size)))
(for-each! (uncurry (hashtable:set! ht))
iter)
ht)
(declare collect-hashtable!
(Hash :key => Iterator (Tuple :key :value) -> HashTable :key :value))
(define (collect-hashtable! iter)
"Construct a `HashTable` containing all the key/value pairs from ITER.
If a key appears in ITER multiple times, the resulting table will contain its last corresponding value."
(collect-hashtable-size-hint! coalton-library/hashtable::default-hash-table-capacity iter)))
#+sb-package-locks
(sb-ext:lock-package "COALTON-LIBRARY/ITERATOR")