1 | [[tags: egg]] |
---|
2 | [[toc:]] |
---|
3 | |
---|
4 | == sequences |
---|
5 | |
---|
6 | === Introduction |
---|
7 | |
---|
8 | Operations over generic or user-defined sequences. |
---|
9 | |
---|
10 | ''Note: this is currently under review - the API might still change'' |
---|
11 | |
---|
12 | === Usage |
---|
13 | |
---|
14 | <enscript highlight=scheme> |
---|
15 | (import sequences) |
---|
16 | </enscript> |
---|
17 | |
---|
18 | === Requirements |
---|
19 | |
---|
20 | [[fast-generic]] |
---|
21 | |
---|
22 | === Documentation |
---|
23 | |
---|
24 | A ''sequence'' is a collection of objects and may be either one of the |
---|
25 | built-in types vector, list or string, or the result of the |
---|
26 | sequence-constructors {{make-linear-sequence}} and |
---|
27 | {{make-random-access-sequences}}. A ''linear'' sequence is a sequence |
---|
28 | that only allows element-by-element access (i.e. a list), a ''random |
---|
29 | access'' sequences allows access to arbitrary elements through an index |
---|
30 | (i.e. vectors or strings). |
---|
31 | |
---|
32 | An ''iterator'' is an object that designates a particular position |
---|
33 | in a linear or random-access sequence. |
---|
34 | |
---|
35 | ==== Basic sequence operations |
---|
36 | |
---|
37 | ===== size |
---|
38 | |
---|
39 | <procedure>(size S)</procedure> |
---|
40 | |
---|
41 | Returns the number of elements in the sequence {{S}}. For linear |
---|
42 | sequences, this operation traverses all elements. |
---|
43 | |
---|
44 | ===== elt |
---|
45 | |
---|
46 | <procedure>(elt S I)</procedure> |
---|
47 | |
---|
48 | Returns the {{I}}-th element of {{S}}. {{I}} may be an exact integer or |
---|
49 | an iterator (see below). |
---|
50 | |
---|
51 | A sequence-element can be modified with {{(set! (elt S I) X)}}. |
---|
52 | |
---|
53 | If {{I}} is an iterator, then {{S}} must be the same sequence that had |
---|
54 | been used to construct the iterator. |
---|
55 | |
---|
56 | ===== rev |
---|
57 | |
---|
58 | <procedure>(rev S)</procedure> |
---|
59 | |
---|
60 | Returns a new sequence of the same type with the elements of {{S}} in |
---|
61 | reverse order. |
---|
62 | |
---|
63 | ===== foldl |
---|
64 | |
---|
65 | <procedure>(foldl PROC SEED S)</procedure> |
---|
66 | |
---|
67 | Performs a left "fold" over the sequence {{S}}, where the procedure |
---|
68 | {{PROC}} is applied to its previous result (or {{SEED}} for the first |
---|
69 | element) and each sequence-element. |
---|
70 | |
---|
71 | ===== foldr |
---|
72 | |
---|
73 | <procedure>(foldr PROC SEED S)</procedure> |
---|
74 | |
---|
75 | A right "fold" over sequence {{S}}, {{PROC}} is applied to each |
---|
76 | sequence element and the result of its last invocation (or {{SEED}} |
---|
77 | for the first element). |
---|
78 | |
---|
79 | ===== sub |
---|
80 | |
---|
81 | <procedure>(sub S START [END])</procedure> |
---|
82 | |
---|
83 | Returns a new sequence with the elements of {{S}}, starting at position |
---|
84 | {{START}} up to but not including the element at position {{END}}. |
---|
85 | If {{END}} is not given, all remaining elements are returned. {{START}} |
---|
86 | and {{END}} may be exact integers or iterators. |
---|
87 | |
---|
88 | A range of elements may be modified by executing |
---|
89 | {{(set! (sub S1 START [END]) S2)}}, which assigns the elements of {{S2}} |
---|
90 | to the designated locations of sequence {{S1}}. |
---|
91 | |
---|
92 | ===== pos |
---|
93 | |
---|
94 | <procedure>(pos PRED S)</procedure> |
---|
95 | |
---|
96 | Returns the index of the first element in {{S}} that for which the |
---|
97 | one argument procedure {{PRED}} returns true. If {{PRED}} returns |
---|
98 | false for all arguments, {{#f}} is returned. |
---|
99 | |
---|
100 | ===== take |
---|
101 | |
---|
102 | <procedure>(take PRED S)</procedure> |
---|
103 | |
---|
104 | Returns a new sequence of the same type as {{S}} with the |
---|
105 | elements up to but not including the first element for |
---|
106 | which the one-argument procedure {{PRED}} returns {{#f}}. |
---|
107 | |
---|
108 | ===== drop |
---|
109 | |
---|
110 | <procedure>(drop PRED S)</procedure> |
---|
111 | |
---|
112 | Returns a new sequence of the same type as {{S}} with the |
---|
113 | elements from the first element for |
---|
114 | which the one-argument procedure {{PRED}} returns {{#f}}. |
---|
115 | |
---|
116 | ===== split |
---|
117 | |
---|
118 | <procedure>(split PRED S)</procedure> |
---|
119 | |
---|
120 | Returns two sequences of the same type as {{S}} holding |
---|
121 | the elements split at the first position for which the |
---|
122 | one-argument procedure {{PRED}} returns {{#f}}. |
---|
123 | |
---|
124 | ===== partition |
---|
125 | |
---|
126 | <procedure>(partition PRED S)</procedure> |
---|
127 | |
---|
128 | Returns two sequences of the same type as {{S}} holding those elements |
---|
129 | for which the one-argument procedure {{PRED}} returns true and false, |
---|
130 | respectively. |
---|
131 | |
---|
132 | ===== fill! |
---|
133 | |
---|
134 | <procedure>(fill! PROC S [START [END]])</procedure> |
---|
135 | |
---|
136 | Calls {{PROC}} with the sequence {{S}} and an iterator object over |
---|
137 | the elements in {{S}} starting at position {{START}} up to |
---|
138 | but not including {{END}} and returns the modified sequence. |
---|
139 | |
---|
140 | ===== all? |
---|
141 | |
---|
142 | <procedure>(all? PROC S)</procedure> |
---|
143 | |
---|
144 | Returns true if {{PROC}} returns true for all elements of {{S}}. |
---|
145 | |
---|
146 | ===== thereis? |
---|
147 | |
---|
148 | <procedure>(thereis? PROC S)</procedure> |
---|
149 | |
---|
150 | Returns {{#t}} if {{S}} contains an element for which {{PROC}} returns |
---|
151 | true. |
---|
152 | |
---|
153 | ===== empty? |
---|
154 | |
---|
155 | <procedure>(empty? S)</procedure> |
---|
156 | |
---|
157 | Returns true if {{S}} is of size 0. |
---|
158 | |
---|
159 | ===== peek |
---|
160 | |
---|
161 | <procedure>(peek S)</procedure> |
---|
162 | |
---|
163 | Returns the first element of {{S}}. |
---|
164 | |
---|
165 | ===== pop |
---|
166 | |
---|
167 | <procedure>(pop S)</procedure> |
---|
168 | |
---|
169 | Returns all but the first element of {{S}}. |
---|
170 | |
---|
171 | ===== filter |
---|
172 | |
---|
173 | <procedure>(filter PROTO PROC S)</procedure> |
---|
174 | |
---|
175 | Returns a new sequence of the same type as {{PROTO}} with all elements |
---|
176 | of {{S}} for which {{PROC}} returns true. |
---|
177 | |
---|
178 | |
---|
179 | ==== Set-operations |
---|
180 | |
---|
181 | ===== intersection |
---|
182 | |
---|
183 | <procedure>(intersection PROTO COMPARE S1 ...)</procedure> |
---|
184 | |
---|
185 | Returns the intersection of sequences {{S1 ...}} using the two-argument procedure |
---|
186 | {{COMPARE}} to compare the elements. The returned sequence is of the same type |
---|
187 | as {{PROTO}}. |
---|
188 | |
---|
189 | |
---|
190 | ===== difference |
---|
191 | |
---|
192 | <procedure>(difference PROTO COMPARE S1 S2 ...)</procedure> |
---|
193 | |
---|
194 | Returns the set-difference of sequences {{S2 ...}} taken from {{S1}} |
---|
195 | using the two-argument procedure {{COMPARE}} to compare the |
---|
196 | elements. The returned sequence is of the same type as {{PROTO}}. |
---|
197 | |
---|
198 | |
---|
199 | ===== union |
---|
200 | |
---|
201 | <procedure>(union PROTO COMPARE S1 ...)</procedure> |
---|
202 | |
---|
203 | Returns the union of sequences {{S1 ...}} using the two-argument procedure |
---|
204 | {{COMPARE}} to compare the elements. The returned sequence is of the same type |
---|
205 | as {{PROTO}}. |
---|
206 | |
---|
207 | |
---|
208 | ==== Predicates over sequence types |
---|
209 | |
---|
210 | ===== sequence? |
---|
211 | |
---|
212 | <procedure>(sequence? X)</procedure> |
---|
213 | |
---|
214 | Returns {{#t}} if {{X}} is a sequence or {{#f}} otherwise. |
---|
215 | |
---|
216 | ===== linear-sequence? |
---|
217 | |
---|
218 | <procedure>(linear-sequence? X)</procedure> |
---|
219 | |
---|
220 | Reurns {{#t}} if {{X}} is a list or a sequence created with |
---|
221 | {{make-linear-sequence}} or {{#f}} otherwise. |
---|
222 | |
---|
223 | ===== random-access-sequence? |
---|
224 | |
---|
225 | <procedure>(random-access-sequence? X)</procedure> |
---|
226 | |
---|
227 | Returns {{#t}} if {{X}} is a vector, a string or a sequence created |
---|
228 | with {{make-random-access-sequence}}, or {{#f}} otherwise. |
---|
229 | |
---|
230 | ==== Sequence constructors |
---|
231 | |
---|
232 | ===== make-random-access-sequence |
---|
233 | |
---|
234 | <procedure>(make-random-access-sequence MAKE ELT SIZE)</procedure> |
---|
235 | |
---|
236 | Returns an object representing a sequence that allows random access |
---|
237 | to its elements. {{MAKE}} should be a procedure of two arguments, |
---|
238 | a size count and an initial value and should return a collection |
---|
239 | of elements which will be stored as "data" in the sequence object. |
---|
240 | {{ELT}} should be a procedure of two arguments receiving the |
---|
241 | "data" and an exact integer index and should return the element |
---|
242 | inside the data collection at the given position. {{SIZE}} should |
---|
243 | be a procedure that receives the data and returns the number of |
---|
244 | elements in that collection. |
---|
245 | |
---|
246 | Note that the "data" may be anything - the operators fully |
---|
247 | define how it is interpreted. |
---|
248 | |
---|
249 | ===== make-linear-sequence |
---|
250 | |
---|
251 | <procedure>(make-linear-sequence MAKE ELT NEXT)</procedure> |
---|
252 | |
---|
253 | Returns an object representing a sequence that only allows sequential |
---|
254 | "on-at-a-time" access to its elements. {{MAKE}} should be a procedure |
---|
255 | of two arguments, a size count and an initial value and should return |
---|
256 | a collection of elements which will be stored as "state" in the |
---|
257 | sequence object. {{ELT}} should be a procedure of one argument |
---|
258 | receiving the "state" and should return the element inside the |
---|
259 | collection that is represented by the currently stored state. {{NEXT}} |
---|
260 | should be a procedure that receives the current state and returns a |
---|
261 | new state representing the underlying collection that will make |
---|
262 | the next element accessible via {{ELT}}. If the collection has |
---|
263 | run out of elements, {{NEXT}} should return {{#f}}. |
---|
264 | |
---|
265 | ===== make |
---|
266 | |
---|
267 | <procedure>(make S LENGTH INIT)</procedure> |
---|
268 | |
---|
269 | Creates a sequence of the same type as {{S}} with {{LENGTH}} |
---|
270 | elements that have the initial value {{INIT}}. |
---|
271 | |
---|
272 | ===== sequence |
---|
273 | |
---|
274 | <procedure>(sequence S X1 ...)</procedure> |
---|
275 | |
---|
276 | Creates a sequence of the same type as {{S}} with {{X1, ...}} as |
---|
277 | its initial elements. |
---|
278 | |
---|
279 | ==== Iterators |
---|
280 | |
---|
281 | ===== iterator? |
---|
282 | |
---|
283 | <procedure>(iterator? X)</procedure> |
---|
284 | |
---|
285 | Returns {{#t}} if {{X}} is an iterator object or {{#f}} otherwise. |
---|
286 | |
---|
287 | ===== linear-iterator? |
---|
288 | |
---|
289 | <procedure>(linear-iterator? X)</procedure> |
---|
290 | |
---|
291 | Returns {{#t}} if {{X}} is an iterator on a linear sequence or {{#f}} otherwise. |
---|
292 | |
---|
293 | ===== random-access-iterator? |
---|
294 | |
---|
295 | <procedure>(random-access-iterator? X)</procedure> |
---|
296 | |
---|
297 | Returns {{#t}} if {{X}} is an iterator on a random-access sewuence or {{#f}} otherwise. |
---|
298 | |
---|
299 | ===== iterator |
---|
300 | |
---|
301 | <procedure>(iterator S [INDEX])</procedure> |
---|
302 | |
---|
303 | Returns an iterator object over sequence {{S}}, optionally starting at |
---|
304 | osition {{INDEX}} (an exact integer). |
---|
305 | |
---|
306 | ===== at-end? |
---|
307 | |
---|
308 | <procedure>(at-end? ITERATOR)</procedure> |
---|
309 | |
---|
310 | Returns {{#t}} if {{ITERATOR}} points past the lat element of its |
---|
311 | associated sequence or {{#f}} otherwise. |
---|
312 | |
---|
313 | ===== advance |
---|
314 | ===== advance! |
---|
315 | |
---|
316 | <procedure>(advance ITERATOR [STEPS])</procedure> |
---|
317 | <procedure>(advance! ITERATOR [STEPS])</procedure> |
---|
318 | |
---|
319 | Returns a new iterator (or modifies the given iterator in case of |
---|
320 | {{advance!}}) pointing to the next element of the associated sequence |
---|
321 | or to the element at the position I + {{STEPS}}, where I is the |
---|
322 | current index of {{ITERATOR}}. |
---|
323 | |
---|
324 | ===== index |
---|
325 | |
---|
326 | <procedure>(index ITERATOR)</procedure> |
---|
327 | |
---|
328 | Returns the exact integer index of the position to which {{ITERATOR}} |
---|
329 | points to. |
---|
330 | |
---|
331 | ==== Iteration constructs |
---|
332 | |
---|
333 | ===== for |
---|
334 | ===== for* |
---|
335 | |
---|
336 | <procedure>(for PROC S)</procedure> |
---|
337 | <procedure>(for* PROC S)</procedure> |
---|
338 | |
---|
339 | Invokes {{PROC}} for each element in sequence and returns an undefined |
---|
340 | result. {{for*}} operates as {{for}} but invokes {{PROC}} with the |
---|
341 | sequence {{S}} and an iterator pointing to the current element. |
---|
342 | |
---|
343 | ===== smap |
---|
344 | ===== smap* |
---|
345 | |
---|
346 | <procedure>(smap S1 PROC S2)</procedure> |
---|
347 | <procedure>(smap S1 PROC S2)</procedure> |
---|
348 | |
---|
349 | Applies {{PROC}} to each element in the sequence {{S2}} and returns |
---|
350 | a new sequence of the same type as {{S1}} constructed of the results |
---|
351 | returned by {{PROC}}. |
---|
352 | |
---|
353 | ==== Other operations |
---|
354 | |
---|
355 | ===== coerce |
---|
356 | |
---|
357 | <procedure>(coerce S1 S2)</procedure> |
---|
358 | |
---|
359 | Returns a new sequence of the same type as {{S1}} containing |
---|
360 | the elements of {{S2}}. |
---|
361 | |
---|
362 | ===== copy |
---|
363 | |
---|
364 | <procedure>(copy S)</procedure> |
---|
365 | |
---|
366 | Returns a copy of the sequence {{S}}. |
---|
367 | |
---|
368 | ===== is? |
---|
369 | |
---|
370 | <procedure>(is? X)</procedure> |
---|
371 | |
---|
372 | Returns a single-argument procedure that returns {{#t}} if |
---|
373 | the argument is {{equal?}} to {{X}} or {{#f}} otherwise. |
---|
374 | |
---|
375 | |
---|
376 | ==== SRFI-42 comprehensions |
---|
377 | |
---|
378 | (This code was kindly contributed by Thomas Chust) |
---|
379 | |
---|
380 | SRFI-42 comprehensions for sequences are provided using the {{:seq}} |
---|
381 | generator, here an example: |
---|
382 | |
---|
383 | (string-ec (:seq x "aAbBcC") (if (char-lower-case? x)) x) ==> "abc" |
---|
384 | |
---|
385 | This is mostly useful with user-defined sequences created by |
---|
386 | {{make-linear-sequence}} and {{make-random-access-sequence}}. |
---|
387 | |
---|
388 | To use this feature, execute |
---|
389 | |
---|
390 | (require-extension sequence-comprehensions) |
---|
391 | |
---|
392 | |
---|
393 | === Author |
---|
394 | |
---|
395 | [[/users/felix winkelmann|felix]] |
---|
396 | |
---|
397 | === Repository |
---|
398 | |
---|
399 | This egg is hosted on the CHICKEN Subversion repository: |
---|
400 | |
---|
401 | [[https://anonymous@code.call-cc.org/svn/chicken-eggs/release/5/sequences|https://anonymous@code.call-cc.org/svn/chicken-eggs/release/5/sequences]] |
---|
402 | |
---|
403 | If you want to check out the source code repository of this egg and |
---|
404 | you are not familiar with Subversion, see [[/egg-svn-checkout|this page]]. |
---|
405 | |
---|
406 | === License |
---|
407 | |
---|
408 | Copyright (c) 2010-2011, Felix L. Winkelmann and Thomas Chust |
---|
409 | All rights reserved. |
---|
410 | |
---|
411 | Redistribution and use in source and binary forms, with or without |
---|
412 | modification, are permitted provided that the following conditions |
---|
413 | are met: |
---|
414 | 1. Redistributions of source code must retain the above copyright |
---|
415 | notice, this list of conditions and the following disclaimer. |
---|
416 | 2. Redistributions in binary form must reproduce the above copyright |
---|
417 | notice, this list of conditions and the following disclaimer in the |
---|
418 | documentation and/or other materials provided with the distribution. |
---|
419 | 3. The name of the authors may not be used to endorse or promote products |
---|
420 | derived from this software without specific prior written permission. |
---|
421 | |
---|
422 | THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR |
---|
423 | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
---|
424 | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
---|
425 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
---|
426 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
---|
427 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
---|
428 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
---|
429 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
430 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
---|
431 | THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
432 | |
---|
433 | === Version History |
---|
434 | |
---|
435 | ; 0.5.1 : Fix segfault when calling certain procedures with non-fixnum as indices and step sizes (fixes #1631) |
---|
436 | ; 0.5 : ported to CHICKEN 5 |
---|
437 | ; 0.4 : removed {{replicate}}, renamed {{contains?}} to {{thereis?}}, performance tuning |
---|
438 | ; 0.3 : added {{replicate}}, various bugfixes |
---|
439 | ; 0.2 : added set-operations and some more |
---|
440 | ; 0.1 : initial release |
---|
441 | |
---|