-
Notifications
You must be signed in to change notification settings - Fork 16
/
sequences.sls
289 lines (258 loc) · 9.8 KB
/
sequences.sls
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
#!r6rs
;;; sequences.sls --- Purely Functional Sequences
;; Copyright (C) 2012 Ian Price <ianprice90@googlemail.com>
;; Author: Ian Price <ianprice90@googlemail.com>
;; This program is free software, you can redistribute it and/or
;; modify it under the terms of the new-style BSD license.
;; You should have received a copy of the BSD license along with this
;; program. If not, see <http://www.debian.org/misc/bsd.license>.
;;; Commentary:
;; Sequences are a general-purpose, variable-length collection,
;; similar to lists, however they support efficient addition and
;; removal from both ends, and random-access. Like other Scheme
;; collections, sequences are zero-indexed.
;;
;; make-sequence : () -> sequence
;; returns a new empty sequence
;;
;; sequence any ... -> sequence
;; returns a new sequence containing all of the argument elements, in the
;; same order.
;;
;; sequence? : any -> bool
;; returns #t if the argument is a sequence, #f otherwise.
;;
;; sequence-empty? : sequence -> bool
;; returns #t if the argument sequence contains no elements, #f otherwise.
;;
;; sequence-size : sequence -> non-negative integer
;; returns the number of elements in the sequence
;;
;; sequence-cons : any sequence -> sequence
;; return the new sequence created by adding the element to the front of
;; the sequence.
;;
;; sequence-uncons : sequence -> any sequence
;; returns two values: the first element of the sequence, and a new
;; sequence containing all but the first element. If the sequence is
;; empty, a &sequence-empty condition is raised.
;;
;; sequence-snoc : sequence any -> sequence
;; return the new sequence created by adding the element to the end of
;; the sequence.
;;
;; sequence-unsnoc : sequence -> sequence any
;; returns two values: a new sequence containing all but the last
;; element of the sequence, and the last element itself. If the
;; sequence is empty, a &sequence-empty condition is raised.
;;
;; sequence-append : sequence sequence -> sequence
;; returns a new sequence containing all the elements of the first
;; sequence, followed by all the elements of the second sequence.
;;
;; list->sequence : Listof(Any) -> sequence
;; returns a new sequence containing all the elements of the argument
;; list, in the same order.
;;
;; sequence->list : sequence -> Listof(Any)
;; returns a new list containing all the elements of the sequence, in the
;; same order.
;;
;; sequence-split-at sequence integer -> sequence + sequence
;; returns two new sequences, the first containing the first N elements
;; of the sequence, the second containing the remaining elements. If N is
;; negative, it returns the empty sequence as the first argument, and the
;; original sequence as the second argument. Similarly, if N is greater
;; than the length of the list, it returns the original sequence as the
;; first argument, and the empty sequence as the second argument.
;;
;; Consequently, (let-values (((a b) (sequence-split-at s i)))
;; (sequence-append a b)) is equivalent to s for all sequences s, and
;; integers i.
;;
;; sequence-take sequence integer -> sequence
;; returns a new sequence containing the first N elements of the
;; argument sequence. If N is negative, the empty sequence is
;; returned. If N is larger than the length of the sequence, the whole
;; sequence is returned.
;;
;; sequence-drop sequence integer -> sequence
;; returns a new sequence containing all but the first N elements of the
;; argument sequence. If N is negative, the whole sequence is
;; returned. If N is larger than the length of the sequence, the empty
;; sequence is returned.
;;
;; sequence-ref : sequence non-negative-integer -> any
;; returns the element at the specified index in the sequence. If the
;; index is outside the range 0 <= i < (sequence-size sequence), an
;; assertion violation is raised.
;;
;; sequence-set : sequence non-negative-integer any -> sequence
;; returns the new sequence obtained by replacing the element at the
;; specified index in the sequence with the given value. If the index
;; is outside the range 0 <= i < (sequence-size sequence), an
;; assertion violation is raised.
;;
;; sequence-fold (any -> any -> any) any sequence
;; returns the value obtained by iterating the combiner procedure over
;; the sequence in left-to-right order. The combiner procedure takes two
;; arguments, the value of the position in the sequence, and an
;; accumulator, and its return value is used as the value of the
;; accumulator for the next call. The initial accumulator value is given
;; by the base argument.
;;
;; sequence-fold-right (any -> any -> any) any sequence
;; Like sequence-fold, but the sequence is traversed in right-to-left
;; order, rather than left-to-right.
;;
;; sequence-reverse : sequence -> sequence
;; returns a new sequence containing all the arguments of the argument
;; list, in reverse order.
;;
;; sequence-map : (any -> any) sequence -> sequence
;; returns a new sequence obtained by applying the procedure to each
;; element of the argument sequence in turn.
;;
;; sequence-filter : (any -> bool) sequence -> sequence
;; returns a new sequence containing all the elements of the argument
;; sequence for which the predicate is true.
;;
;; sequence-empty-condition? : any -> bool
;; returns #t if an object is a &sequence-empty condition, #f otherwise.
;;
(library (pfds sequences)
(export make-sequence
sequence?
sequence-empty?
sequence-size
sequence-cons
sequence-uncons
sequence-snoc
sequence-unsnoc
sequence-append
list->sequence
sequence->list
(rename (%sequence sequence))
sequence-split-at
sequence-take
sequence-drop
sequence-ref
sequence-set
sequence-fold
sequence-fold-right
sequence-reverse
sequence-map
sequence-filter
sequence-empty-condition?
)
(import (rnrs)
(pfds fingertrees))
;; Note: as sequences are not a subtype of fingertrees, but rather a
;; particular instantiation of them, &sequence-empty is not a subtype
;; of &fingertree-empty
(define-condition-type &sequence-empty
&assertion
make-sequence-empty-condition
sequence-empty-condition?)
(define-record-type (sequence %make-sequence sequence?)
(fields fingertree))
(define (make-sequence)
(%make-sequence (make-fingertree 0 + (lambda (x) 1))))
(define (sequence-empty? seq)
(fingertree-empty? (sequence-fingertree seq)))
(define (sequence-size seq)
(fingertree-measure (sequence-fingertree seq)))
(define (sequence-cons value seq)
(%make-sequence
(fingertree-cons value (sequence-fingertree seq))))
(define (sequence-snoc seq value)
(%make-sequence
(fingertree-snoc (sequence-fingertree seq) value)))
(define (sequence-uncons seq)
(call-with-values
(lambda ()
(define ft (sequence-fingertree seq))
(when (fingertree-empty? ft)
(raise
(condition
(make-sequence-empty-condition)
(make-who-condition 'sequence-uncons)
(make-message-condition "There are no elements to uncons")
(make-irritants-condition (list seq)))))
(fingertree-uncons ft))
(lambda (head tree)
(values head (%make-sequence tree)))))
(define (sequence-unsnoc seq)
(call-with-values
(lambda ()
(define ft (sequence-fingertree seq))
(when (fingertree-empty? ft)
(raise
(condition
(make-sequence-empty-condition)
(make-who-condition 'sequence-unsnoc)
(make-message-condition "There are no elements to unsnoc")
(make-irritants-condition (list seq)))))
(fingertree-unsnoc ft))
(lambda (tree last)
(values (%make-sequence tree) last))))
(define (sequence-append seq1 seq2)
(%make-sequence
(fingertree-append (sequence-fingertree seq1)
(sequence-fingertree seq2))))
(define (list->sequence list)
(fold-left sequence-snoc
(make-sequence)
list))
(define (sequence->list seq)
(fingertree->list (sequence-fingertree seq)))
(define (%sequence . args)
(list->sequence args))
(define (sequence-split-at seq i)
(let-values (((l r)
(fingertree-split (lambda (x) (< i x))
(sequence-fingertree seq))))
(values (%make-sequence l)
(%make-sequence r))))
(define (sequence-take seq i)
(let-values (((head tail)
(sequence-split-at seq i)))
head))
(define (sequence-drop seq i)
(let-values (((head tail)
(sequence-split-at seq i)))
tail))
(define (sequence-ref seq i)
(define size (sequence-size seq))
(unless (and (<= 0 i) (< i size))
(assertion-violation 'sequence-ref "Index out of range" i))
(let-values (((_l x _r)
(fingertree-split3 (lambda (x) (< i x))
(sequence-fingertree seq))))
x))
(define (sequence-set seq i val)
(define size (sequence-size seq))
(unless (and (<= 0 i) (< i size))
(assertion-violation 'sequence-set "Index out of range" i))
(let-values (((l x r)
(fingertree-split3 (lambda (x) (< i x))
(sequence-fingertree seq))))
(%make-sequence
(fingertree-append l (fingertree-cons val r)))))
(define (sequence-fold proc base seq)
(fingertree-fold proc base (sequence-fingertree seq)))
(define (sequence-fold-right proc base seq)
(fingertree-fold-right proc base (sequence-fingertree seq)))
(define (sequence-reverse seq)
(%make-sequence (fingertree-reverse (sequence-fingertree seq))))
(define (sequence-map proc seq)
(define (combine element seq)
(sequence-cons (proc element) seq))
(sequence-fold-right combine (make-sequence) seq))
(define (sequence-filter pred? seq)
(define (combine element seq)
(if (pred? element)
(sequence-cons element seq)
seq))
(sequence-fold-right combine (make-sequence) seq))
)