-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.rkt
412 lines (354 loc) · 12.5 KB
/
api.rkt
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
#lang racket/base
(require (only-in racket/function curry)
(except-in data/collection
range
map
filter
reverse
rest
take
drop)
(only-in data/collection
[map d:map]
[filter d:filter]
[reverse d:reverse]
[rest d:rest]
[take d:take]
[drop d:drop])
syntax/parse/define
(for-syntax racket/base)
arguments
version-case
mischief/shorthand
(only-in relation false.)
(prefix-in p: "base.rkt")
"types.rkt")
(version-case
[(version< (version) "7.9.0.22")
(define-alias define-syntax-parse-rule define-simple-macro)])
(module+ test
(require rackunit
rackunit/text-ui
"private/util.rkt"))
(provide range
map
filter
reverse
rest
drop
set-nth
by
take-when
prefix
suffix-at
infix
infix-at
init
zip-with
zip
unzip-with
unzip
choose
suffix
take-while
drop-while
take-until
drop-until
cut-when
cut
cut-at
cut-where
cut-by
cut-with
truncate
rotate-left
rotate-right
rotate
rotations
suffixes
prefixes
infixes
replace-infix
trim-if
trim
trim-by
remove
remove-at
drop-when
intersperse
add-between
wrap-each
interleave
(rename-out [d:take take] ; so scribble can find it in `(for-label seq)`
[p:nth nth]
[p:exists exists]
[p:for-all for-all]
[p:find find]
[p:index-where index-where]
[p:deduplicate deduplicate]
[p:multiples multiples]
[p:powers powers]
[p:iterate iterate]
[p:prefix? prefix?]
[p:starts-with? starts-with?]
[p:suffix? suffix?]
[p:ends-with? ends-with?]
[p:find-infix find-infix]
[p:infix? infix?]
[p:contains? contains?]
[p:index-of index-of]
[p:index index]
[p:join-with join-with]
[p:weave weave]))
(define (annotate-result source result)
(if (and source
(countable? source)
(known-finite? source)
(not (and (countable? result)
(known-finite? result))))
(finite-sequence result)
result))
(define (annotate-result-naively result)
(if (and (countable? result)
(known-finite? result))
result
(finite-sequence result)))
(module+ test
;; the main test module for this is tests/api.rkt
;; but we use a test submodule here to avoid providing
;; the `annotate` macro outside this module since it's an
;; internal implementation detail
(struct opaque-sequence ()
#:transparent
#:methods gen:sequence
[(define (first this)
(void))
(define (rest this)
(opaque-sequence))
(define (empty? this)
#f)]
#:methods gen:countable
[(define (known-finite? this)
#f)
(define (length this)
1)])
(struct known-finite-sequence ()
#:transparent
#:methods gen:sequence
[(define (first this)
(void))
(define (rest this)
(known-finite-sequence))
(define (empty? this)
#f)]
#:methods gen:countable
[(define (known-finite? this)
#t)
(define (length this)
1)])
(define tests
(test-suite
"finiteness annotation"
(test-suite
"annotate conditionally"
(check-false (known-finite?
(annotate-result (opaque-sequence)
(opaque-sequence))))
(check-true (known-finite?
(annotate-result (opaque-sequence)
(known-finite-sequence))))
(check-true (known-finite?
(annotate-result (known-finite-sequence)
(known-finite-sequence))))
(check-true (known-finite?
(annotate-result (known-finite-sequence)
(opaque-sequence)))))
(test-suite
"annotate always"
(check-true (known-finite?
(annotate-result-naively
(opaque-sequence))))
(check-true (known-finite?
(annotate-result-naively
(known-finite-sequence))))))))
;;; built-in or data/collection sequences
(define (range . args)
(finite-sequence (apply in-range args)))
;; `define-by-annotating` is used to annotate interfaces where the
;; finiteness of the result is implied by the finiteness of the input(s)
;; In writing these annotated functions, if we accepted an arbitrary
;; number of arguments, it would obscure the arity of the underlying
;; function, confusing currying attempts and possibly having other
;; unintended consequences. Therefore, we need to write the annotated
;; functions here so that they have identical signatures to the
;; underyling functions being annotated.
(define-syntax-parser define-by-annotating
[(_ fname f 1)
;; function taking exactly one argument
;; which is the sequence itself
#'(define (fname seq)
(let ([result (f seq)])
(annotate-result seq result)))]
[(_ fname f 2 1)
;; function taking two arguments, with the sequence
;; at position 1 (0-indexed)
#'(define (fname arg seq)
(let ([result (f arg seq)])
(annotate-result seq result)))]
[(_ fname f 2 0)
;; function taking two arguments, with the sequence
;; at position 0 (0-indexed)
#'(define (fname seq arg)
(let ([result (f seq arg)])
(annotate-result seq result)))]
[(_ fname f 3 2)
;; function taking three arguments, with the sequence
;; at position 2 (0-indexed)
#'(define (fname arg1 arg2 seq)
(let ([result (f arg1 arg2 seq)])
(annotate-result seq result)))]
[(_ fname f (~datum VARIADIC-INPUT))
;; function taking any number of sequence arguments
#'(define (fname . seqs)
(let ([result (apply f seqs)]
[seq (first seqs)])
(annotate-result seq result)))]
[(_ fname f 1 (~datum VARIADIC-INPUT))
;; function taking an arbitrary argument, followed by
;; any number of sequence arguments
#'(define (fname arg . seqs)
(let ([result (apply f arg seqs)]
[seq (first seqs)])
(annotate-result seq result)))]
[(_ fname f (~datum LIST-INPUT))
;; function taking a list of sequences as its sole argument
#'(define (fname seqs)
(let ([result (f seqs)]
[seq (first seqs)])
(annotate-result seq result)))]
[(_ fname f 1 (~datum LIST-INPUT))
;; function taking an arbitrary argument, followed by
;; a list of sequences as its second argument
#'(define (fname arg seqs)
(let ([result (f arg seqs)]
[seq (first seqs)])
(annotate-result seq result)))]
[(_ fname f 2 1 (~datum TWO-VALUE-RESULT))
;; function taking an arbitrary argument followed by
;; a sequence, returning two values, each a sequence
;; TODO: what about the wrapping sequence in a sequence of sequences?
#'(define (fname arg seq)
(let-values ([(a b) (f arg seq)])
(values (annotate-result seq a)
(annotate-result seq b))))]
[(_ fname f 1 (~datum SEQUENCE-RESULT))
;; function taking a single sequence argument
;; that returns a sequence of sequences
#'(define (fname seq)
(let ([result (f seq)])
(d:map (curry annotate-result seq) result)))]
[(_ fname f 2 1 (~datum SEQUENCE-RESULT))
;; function taking a single sequence argument
;; that returns a sequence of sequences
#'(define (fname arg seq)
(let ([result (f arg seq)])
(d:map (curry annotate-result seq) result)))])
;; `define-by-annotating-naively` is for interfaces where the result is
;; always going to be finite, irrespective of the input. this could
;; support additional patterns like `annotate`, if needed, but this is
;; only used in `choose` at the moment
(define-syntax-parser define-by-annotating-naively
[(_ fname f 1 (~datum VARIADIC-INPUT))
#'(define (fname arg . seqs)
(let ([result (apply f arg seqs)])
(annotate-result-naively result)))])
;; really it's if _any_ of the input sequences are finite
(define-by-annotating map d:map 1 VARIADIC-INPUT)
(define-by-annotating filter d:filter 2 1)
(define-by-annotating reverse d:reverse 1)
(define-by-annotating rest d:rest 1)
(define-by-annotating drop d:drop 2 1)
(define-by-annotating set-nth p:set-nth 3 2)
;;; seq
(define-by-annotating by p:by 2 1)
(define-by-annotating take-when p:take-when 2 1)
(define-by-annotating prefix p:prefix 2 1)
(define-by-annotating suffix-at p:suffix-at 2 1)
(define-by-annotating infix-at p:infix-at 3 2)
(define-by-annotating infix p:infix 3 2)
(define-by-annotating init p:init 1)
;; really it's if _any_ of the input sequences are finite
(define-by-annotating zip-with p:zip-with 1 VARIADIC-INPUT)
(define-by-annotating zip p:zip VARIADIC-INPUT)
(define-by-annotating unzip-with p:unzip-with 1 LIST-INPUT)
(define-by-annotating unzip p:unzip LIST-INPUT)
(define-by-annotating-naively choose p:choose 1 VARIADIC-INPUT)
(define-by-annotating suffix p:suffix 2 1)
(define-by-annotating take-while p:take-while 2 1)
(define-by-annotating drop-while p:drop-while 2 1)
(define-by-annotating take-until p:take-until 2 1)
(define-by-annotating drop-until p:drop-until 2 1)
(define (cut-when pred seq)
(d:map (curry annotate-result seq)
(p:cut-when pred seq)))
(define (cut #:key [key #f]
elem
seq)
(d:map (curry annotate-result seq)
(p:cut #:key key
elem
seq)))
(define-by-annotating cut-at p:cut-at 2 1 TWO-VALUE-RESULT)
(define-by-annotating cut-where p:cut-where 2 1 TWO-VALUE-RESULT)
;; cut-by appears to always return a stream of lists, so it can probably
;; just be passed through here as it's guaranteed to be known finite
;; (as a list)
(define-by-annotating cut-by p:cut-by 2 1 SEQUENCE-RESULT)
(define-by-annotating cut-with p:cut-with 2 1 TWO-VALUE-RESULT)
(define-by-annotating truncate p:truncate 2 0)
(define-by-annotating rotate-left p:rotate-left 2 1)
(define-by-annotating rotate-right p:rotate-right 2 1)
(define-by-annotating rotate p:rotate 1)
(define-by-annotating rotations p:rotations 1 SEQUENCE-RESULT)
(define-by-annotating prefixes p:prefixes 1 SEQUENCE-RESULT)
(define-by-annotating suffixes p:suffixes 1 SEQUENCE-RESULT)
(define-by-annotating infixes p:infixes 2 1 SEQUENCE-RESULT)
(define (replace-infix #:key [key #f]
#:how-many [how-many #f]
orig-subseq
new-subseq
seq)
(annotate-result seq
(p:replace-infix #:key key
#:how-many how-many
orig-subseq
new-subseq
seq)))
(define-by-annotating trim-if p:trim-if 2 1)
(define-by-annotating trim p:trim 2 1)
(define-by-annotating trim-by p:trim-by 3 2)
(define (remove #:key [key #f]
#:how-many [how-many #f]
elem
seq)
(annotate-result seq
(p:remove #:key key
#:how-many how-many
elem
seq)))
(define-by-annotating remove-at p:remove-at 2 1)
(define (drop-when #:how-many [how-many #f]
pred
seq)
(annotate-result seq
(p:drop-when #:how-many how-many
pred
seq)))
(define-by-annotating intersperse p:intersperse 2 1)
(define-by-annotating add-between p:add-between 2 1)
(define-by-annotating wrap-each p:wrap-each 3 2)
;; really it's if _any_ of the input sequences are finite
(define-by-annotating interleave p:interleave VARIADIC-INPUT)
(module+ test
(just-do
(run-tests tests)))