4.17.1 Sequences (original) (raw)
4.17.1 Sequences🔗ℹ
Sequence Constructors in The Racket Guide introduces sequences.
A sequence encapsulates an ordered collection of values. The elements of a sequence can be extracted with one of thefor syntactic forms, with the procedures returned bysequence-generate, or by converting the sequence into astream.
The sequence datatype overlaps with many other datatypes. Among built-in datatypes, the sequence datatype includes the following:
- exact nonnegative integers (see below)
- strings (see Strings)
- byte strings (see Byte Strings)
- lists (see Pairs and Lists)
- mutable lists (see Mutable Pairs and Lists)
- vectors (see Vectors)
- flvectors (see Flonum Vectors)
- fxvectors (see Fixnum Vectors)
- hash tables (see Hash Tables)
- dictionaries (see Dictionaries)
- sets (see Sets)
- input ports (see Ports)
- streams (see Streams)
An exact number k that is a non-negativeinteger acts as a sequence similar to (in-range k), except that k by itself is not a stream.
Custom sequences can be defined using structure type properties. The easiest method to define a custom sequence is to use thegen:stream generic interface. Streams are a suitable abstraction for data structures that are directly iterable. For example, a list is directly iterable with first andrest. On the other hand, vectors are not directly iterable: iteration has to go through an index. For data structures that are not directly iterable, the iterator for the data structure can be defined to be a stream (e.g., a structure containing the index of a vector).
For example, unrolled linked lists (represented as a list of vectors) themselves do not fit the stream abstraction, but have index-based iterators that can be represented as streams:
Examples:
The prop:sequence property provides more flexibility in specifying iteration, such as when a pre-processing step is needed to prepare the data for iteration. The make-do-sequencefunction creates a sequence given a thunk that returns procedures to implement a sequence, and the prop:sequence property can be associated with a structure type to implement its implicit conversion to a sequence.
For most sequence types, extracting elements from a sequence has no side-effect on the original sequence value; for example, extracting the sequence of elements from a list does not change the list. For other sequence types, each extraction implies a side effect; for example, extracting the sequence of bytes from a port causes the bytes to be read from the port. A sequence’s state may either span all uses of the sequence, as for a port, or it may be confined to each distinct time that a sequence is initiated by a for form,sequence->stream, sequence-generate, orsequence-generate*. Concretely, the thunk passed tomake-do-sequence is called to initiate the sequence each time the sequence is used. Accordingly, different sequences behave differently when they are initiated multiple times.
> (define (double-initiate s1) ; initiate the sequence twice (define-values (more?.1 next.1) (sequence-generate s1)) (define-values (more?.2 next.2) (sequence-generate s1)) ; alternate fetching from sequence via the two initiations (list (next.1) (next.2) (next.1) (next.2))) > (double-initiate (open-input-string "abcdef")) '(97 98 99 100) > (double-initiate (list 97 98 99 100)) '(97 97 98 98) > (double-initiate (in-naturals 97)) '(97 97 98 98)
Also, subsequent elements in a sequence may be “consumed” just by calling the first result of sequence-generate, even if the second result is never called.
> (define (double-initiate-and-use-more? s1) ; initiate the sequence twice (define-values (more?.1 next.1) (sequence-generate s1)) (define-values (more?.2 next.2) (sequence-generate s1)) ; alternate fetching from sequence via the two initiations ; but this time call `more?` in between (list (next.1) (more?.1) (next.2) (more?.2) (next.1) (more?.1) (next.2) (more?.2))) > (double-initiate-and-use-more? (open-input-string "abcdef")) '(97 #t 99 #t 98 #t 100 #t)
In this example, the state embedded in the first call to sequence-generate“takes” the 98 just by virtue of the invocation of more?.1.
Individual elements of a sequence typically correspond to single values, but an element may also correspond to multiple values. For example, a hash table generates two values—a key and its value—for each element in the sequence.
4.17.1.1 Sequence Predicate and Constructors🔗ℹ
Returns #t if v can be used as a sequence,#f otherwise.
Examples:
> (sequence? 42) #t > (sequence? '(a b c)) #t > (sequence? "word") #t > (sequence? #\x) #f
Returns a sequence (that is also a stream) whose elements are numbers. The single-argument case (in-range end) is equivalent to (in-range 0 end 1). The first number in the sequence is start, and each successive element is generated by adding step to the previous element. The sequence stops before an element that would be greater or equal to end ifstep is non-negative, or less or equal to end ifstep is negative.
An in-range application can provide better performance for number iteration when it appears directly in a for clause.
Example: gaussian sum
Example: sum of even numbers
When given zero as step, in-range returns an infinite sequence. It may also return infinite sequences when step is a very small number, and either step or the sequence elements are floating-point numbers.
Similar to in-range, but the sequence stopping condition is changed so that the last element is allowed to be equal to end.
An in-inclusive-range application can provide better performance for number iteration when it appears directly in a for clause.
Examples:
> (sequence->list (in-inclusive-range 7 11)) '(7 8 9 10 11) > (sequence->list (in-inclusive-range 7 11 2)) '(7 9 11) > (sequence->list (in-inclusive-range 7 10 2)) '(7 9)
Added in version 8.0.0.13 of package base.
Returns an infinite sequence (that is also a stream) of exact integers starting with start, where each element is one more than the preceding element.
An in-naturals application can provide better performance for integer iteration when it appears directly in a for clause.
Example:
'((0 0) (1 1) (2 2) (3 3) (4 4) (5 5) (6 6) (7 7) (8 8) (9 9))
Returns a sequence (that is also a stream) that is equivalent to using lst directly as a sequence.
An in-list application can provide better performance for list iteration when it appears directly in a for clause.
See for for information on the reachability of list elements during an iteration.
Example:
Changed in version 6.7.0.4 of package base: Improved element-reachability guarantee for lists in for.
Returns a sequence equivalent to mlst. Although the expectation is that mlst is mutable list, in-mlistinitially checks only whether mlst is a mutable pair or null, since it could change during iteration.
An in-mlist application can provide better performance for mutable list iteration when it appears directly in a for clause.
Example:
Returns a sequence equivalent to vec when no optional arguments are supplied.
See Vectors for information on using vectors as sequences.
The optional arguments start, stop, andstep are analogous to in-range, except that a#f value for stop is equivalent to(vector-length vec). That is, the first element in the sequence is (vector-ref vec start), and each successive element is generated by adding step to index of the previous element. The sequence stops before an index that would be greater or equal to end if step is non-negative, or less or equal to end if step is negative.
If start is not a valid index, then theexn:fail:contract exception is raised, except when start, stop, and(vector-length vec) are equal, in which case the result is an empty sequence.
Examples:
> (for ([x (in-vector (vector 1) 1)]) x) > (for ([x (in-vector (vector 1) 2)]) x) in-vector: starting index is out of range starting index: 2 valid range: [0, 0] vector: '#(1) > (for ([x (in-vector (vector) 0 0)]) x) > (for ([x (in-vector (vector 1) 1 1)]) x)
If stop is not in [-1, (vector-length vec)], then the exn:fail:contract exception is raised.
If start is less thanstop and step is negative, then theexn:fail:contract exception is raised. Similarly, if startis more than stop and step is positive, then theexn:fail:contract exception is raised.
An in-vector application can provide better performance for vector iteration when it appears directly in a for clause.
Examples:
> (histogram #("hello" "world" "hello" "sunshine")) '#hash(("hello" . 2) ("sunshine" . 1) ("world" . 1))
Returns a sequence equivalent to str when no optional arguments are supplied.
See Strings for information on using strings as sequences.
The optional arguments start, stop, andstep are as in in-vector.
An in-string application can provide better performance for string iteration when it appears directly in a for clause.
Examples:
> (line-count "this string\nhas\nthree \nnewlines") 3
Returns a sequence equivalent to bstr when no optional arguments are supplied.
See Byte Strings for information on using byte strings as sequences.
The optional arguments start, stop, andstep are as in in-vector.
An in-bytes application can provide better performance for byte string iteration when it appears directly in a for clause.
Examples:
> (has-eof? #"this byte string has an \0embedded zero byte") #t > (has-eof? #"this byte string does not") #f
Returns a sequence whose elements are produced by calling ron in until it produces eof.
Returns a sequence equivalent to (in-port read-byte in).
Returns a sequence whose elements are read as characters fromin (equivalent to (in-port read-char in)).
Returns a sequence equivalent to(in-port (lambda (p) (read-line p mode)) in). Note that the default mode is 'any, whereas the default mode ofread-line is 'linefeed.
Returns a sequence equivalent to(in-port (lambda (p) (read-bytes-line p mode)) in). Note that the default mode is 'any, whereas the default mode ofread-bytes-line is 'linefeed.
Returns a sequence equivalent to hash, except when bad-index-vis supplied.
If bad-index-v is supplied, then bad-index-v is returned as both the key and the value in the case that thehash is modified concurrently so that iteration does not have avalid hash index. Providing bad-index-v is particularly useful when iterating through a hash table with weakly held keys, since entries can be removed asynchronously (i.e., after in-hash has committed to another iteration, but before it can access the entry for the next iteration).
Examples:
> (define table (hash 'a 1 'b 2)) > (for ([(key value) (in-hash table)]) (printf "key: ~a value: ~a\n" key value)) key: b value: 2key: a value: 1
See Hash Tables for information on using hash tables as sequences.
Changed in version 7.0.0.10 of package base: Added the optional bad-index-v argument.
Returns a sequence whose elements are the keys of hash, usingbad-index-v in the same way as in-hash.
Examples:
> (define table (hash 'a 1 'b 2))
Changed in version 7.0.0.10 of package base: Added the optional bad-index-v argument.
Returns a sequence whose elements are the values of hash, usingbad-index-v in the same way as in-hash.
Examples:
> (define table (hash 'a 1 'b 2)) > (for ([value (in-hash-values table)]) (printf "value: ~a\n" value))
Changed in version 7.0.0.10 of package base: Added the optional bad-index-v argument.
Returns a sequence whose elements are pairs, each containing a key and its value from hash (as opposed to using hashdirectly as a sequence to get the key and value as separate values for each element).
The bad-index-v argument, if supplied, is used in the same way as by in-hash. When an invalid index is encountered, the pair in the sequence with have bad-index-v as both itscar and cdr.
Examples:
> (define table (hash 'a 1 'b 2)) > (for ([key+value (in-hash-pairs table)]) (printf "key and value: ~a\n" key+value)) key and value: (b . 2)key and value: (a . 1)
Changed in version 7.0.0.10 of package base: Added the optional bad-index-v argument.
Sequence constructors for specific kinds of hash tables. These may perform better than the analogous in-hashforms.
Added in version 6.4.0.6 of package base.
Changed in version 7.0.0.10: Added the optional bad-index-v argument.
Changed in version 8.0.0.10: Added ephemeron variants.
Returns a sequence that produces all of the paths for files, directories, and links within dir, except for the contents of any directory for which use-dir? returns#f. If dir is not#f, then every produced path starts with dir as its prefix. If dir is #f, then paths in and relative to the current directory are produced.
An in-directory sequence traverses nested subdirectories recursively (filtered by use-dir?). To generate a sequence that includes only the immediate content of a directory, use the result of directory-list as a sequence.
The immediate content of each directory is reported as sorted bypath<?, and the content of a subdirectory is reported before subsequent paths within the directory.
Examples:
> (current-directory (path-only (collection-file-path "main.rkt" "info"))) '(#path:compiled #path:compiled/main\_rkt.dep #path:compiled/main\_rkt.zo #path:main.rkt) '(#path:compiled/main\_rkt.dep #path:compiled/main\_rkt.zo) '(#path:compiled #path:main.rkt)
Changed in version 6.0.0.1 of package base: Added use-dir? argument.
Changed in version 6.6.0.4: Added guarantee of sorted results.
Returns a sequence that contains values from sequential calls toproducer, which would usually use some state to do its work.
If a stop value is not given, the sequence goes on infinitely, and therefore it is common to use it with a finite sequence or using #:break etc. If a stop value is given, it is used to identify a value that marks the end of the sequence (and the stop value is not included in the sequence);stop can be a predicate that is applied to the results ofproducer, or it can be a value that is tested against the result of with eq?. (The stop argument must be a predicate if the stop value is itself a function or ifproducer returns multiple values.)
If additional args are specified, they are passed to every call to producer.
Examples:
Returns a sequence that produces a single value: v.
This form is mostly useful for let-like bindings in forms such as for*/list—but a #:do clause form, added more recently, covers many of the same uses.
(in-indexed seq) → sequence? seq : sequence?
Returns a sequence where each element has two values: the value produced by seq, and a non-negative exact integer starting with 0. The elements of seq must be single-valued.
Example:
> (for ([(ch i) (in-indexed "hello")]) (printf "The char at position ~a is: ~a\n" i ch)) The char at position 0 is: hThe char at position 1 is: eThe char at position 2 is: lThe char at position 3 is: lThe char at position 4 is: o
(in-sequences seq ...) → sequence? seq : sequence?
Returns a sequence that is made of all input sequences, one after the other. Each seq is initiated only after the preceding seq is exhausted. If a single seq is provided, then seq is returned; otherwise, the elements of each seq must all have the same number of values.
(in-cycle seq ...) → sequence? seq : sequence?
Similar to in-sequences, but the sequences are repeated in an infinite cycle, where each seq is initiated afresh in each iteration. Beware that if no seqs are provided or if all seqs become empty, then the sequence produced by in-cycle never returns when an element is demanded—or even when the sequence is initiated, if allseqs are initially empty.
(in-parallel seq ...) → sequence? seq : sequence?
Returns a sequence where each element has as many values as the number of supplied seqs; the values, in order, are the values of each seq. The elements of each seq must be single-valued.
Returns a sequence that is like seq, but it combines multiple values for each element from seq as a list of elements.
Returns a sequence that is like seq, but when an element ofseq has multiple values or a single list value, then the values are combined in a list. In other words,in-values*-sequence is like in-values-sequence, except that non-list, single-valued elements are not wrapped in a list.
Returns a sequence that contains the elements of seq (which must be single-valued), but only until the last element for which applying pred to the element produces #t, after which the sequence ends.
Returns a sequence that contains the elements of seq (which must be single-valued), but only until the element (inclusive) for which applying pred to the element produces #t, after which the sequence ends.
Returns a sequence whose elements are generated according to thunk.
The sequence is initiated when thunk is called. The initiated sequence is defined in terms of a position, which is initialized to init-pos, and the element, which may consist of multiple values.
The thunk procedure must return either six or seven values. However, use initiate-sequence to return these multiple values, as opposed to listing the values directly.
If thunk returns six values:
- The first result is a pos->element procedure that takes the current position and returns the value(s) for the current element.
- The second result is a next-pos procedure that takes the current position and returns the next position.
- The third result is a init-pos value, which is the initial position.
- The fourth result is a continue-with-pos? function that takes the current position and returns a true result if the sequence includes the value(s) for the current position, and false if the sequence should end instead of including the value(s). Alternatively, continue-with-pos? can be #f to indicate that the sequence should always include the current value(s). This function is checked on each position beforepos->element is used.
- The fifth result is a continue-with-val? function that is like continue-with-pos?, but it takes the current element value(s) as arguments instead of the current position. Alternatively, continue-with-val?can be #f to indicate that the sequence should always include the value(s) at the current position.
- The sixth result is a continue-after-pos+val?procedure that takes both the current position and the current element value(s) and determines whether the sequence ends after the current element is already included in the sequence. Alternatively, continue-after-pos+val? can be #f to indicate that the sequence can always continue after the current value(s).
If thunk returns seven values, the first result is still the pos->element procedure. However, the second result is now an early-next-pos procedure that is described further below. Alternatively,early-next-pos can be #f, which is equivalent to the identity function. Other results’ positions are shifted by one, so the third result is now next-pos, and the fourth result is now init-pos, etc.
The early-next-pos procedure takes the current position and returns an updated position. This updated position is used for next-pos andcontinue-after-pos+val?, but not withcontinue-with-pos? (which uses the original current position). The intent of early-next-pos is to support a sequence where the position must be incremented to avoid keeping a value reachable while a loop processes the sequence value, soearly-next-pos is applied just afterpos->element. The continue-after-pos+val? function needs to be #f to avoid retaining values to supply to that function.
Each of the procedures listed above is called only once per position. Among the procedures continue-with-pos?, continue-with-val?, and continue-after-pos+val?, as soon as one of the procedures returns #f, the sequence ends, and none are called again. Typically, one of the functions determines the end condition, and #f is used in place of the other two functions.
Changed in version 6.7.0.4 of package base: Added support for the optional second result.
Associates a procedure to a structure type that takes an instance of the structure and returns a sequence. If v is an instance of a structure type with this property, then (sequence? v)produces #t.
Using a pre-existing sequence:
Examples:
> (for/list ([c (make-set 'celeriac 'carrot 'potato)]) c) '(potato celeriac carrot)
Using make-do-sequence:
Examples:
> (require racket/sequence) > (struct train (car next) #:property prop:sequence (lambda (t) (make-do-sequence (lambda () (initiate-sequence #:pos->element train-car #:next-pos train-next #:init-pos t #:continue-with-pos? (lambda (t) t)))))) > (for/list ([c (train 'engine (train 'boxcar (train 'caboose #f)))]) c) '(engine boxcar caboose)
4.17.1.2 Sequence Conversion🔗ℹ
Converts a sequence to a stream, which supports thestream-first and stream-rest operations. Creation of the stream eagerly initiates the sequence, but the stream lazily draws elements from the sequence, caching each element so that stream-first produces the same result each time is applied to a stream.
If extracting an element from seq involves a side-effect, then the effect is performed each time that eitherstream-first or stream-rest is first used to access or skip an element.
Note that a sequence itself can have state, so multiple calls to sequence->stream on the sameseq are not necessarily independent.
Examples:
Initiates a sequence and returns two thunks to extract elements from the sequence. The first returns #t if more values are available for the sequence. The second returns the next element (which may be multiple values) from the sequence; if no more elements are available, the exn:fail:contract exception is raised.
Note that a sequence itself can have state, so multiple calls to sequence-generate on the sameseq are not necessarily independent.
Examples:
Like sequence-generate, but avoids state (aside from any inherent in the sequence) by returning a list of values for the sequence’s first element—or #f if the sequence is empty—and a thunk to continue with the sequence; the result of the thunk is the same as the result of sequence-generate*, but for the second element of the sequence, and so on. If the thunk is called when the element result is #f (indicating no further values in the sequence), the exn:fail:contract exception is raised.
4.17.1.3 Additional Sequence Operations🔗ℹ
The bindings documented in this section are provided by the racket/sequence and racket libraries, but not racket/base.
A sequence with no elements.
Returns a list whose elements are the elements of s, each of which must be a single value. If s is infinite, this function does not terminate.
Returns the number of elements of s by extracting and discarding all of them. If s is infinite, this function does not terminate.
Returns the ith element of s (which may be multiple values).
Returns a sequence equivalent to s, except that the firsti elements are omitted.
In case initiating s involves a side effect, the sequence s is not initiated until the resulting sequence is initiated, at which point the firsti elements are extracted from the sequence.
Returns a sequence that contains all elements of each sequence in the order they appear in the original sequences. The new sequence is constructed lazily.
If all given ss are streams, the result is also astream.
Returns a sequence that contains f applied to each element of s. The new sequence is constructed lazily.
If s is a stream, then the result is also astream.
Returns #t if f returns a true result on every element of s. If s is infinite and fnever returns a false result, this function does not terminate.
Returns #t if f returns a true result on some element of s. If s is infinite and fnever returns a true result, this function does not terminate.
Applies f to each element of s. If s is infinite, this function does not terminate.
Folds f over each element of s with i as the initial accumulator. If s is infinite, this function does not terminate. The f function takes the accumulator as its first argument and the next sequence element as its second.
Returns the number of elements in s for which freturns a true result. If s is infinite, this function does not terminate.
Returns a sequence whose elements are the elements of s for which f returns a true result. Although the new sequence is constructed lazily, if s has an infinite number of elements where f returns a false result in between two elements where f returns a true result, then operations on this sequence will not terminate during the infinite sub-sequence.
If s is a stream, then the result is also astream.
Returns a sequence whose elements are the elements of s, but with e between each pair of elements in s. The new sequence is constructed lazily.
If s is a stream, then the result is also astream.
Examples:
> (let* ([all-reds (in-cycle '("red"))] [red-and-blues (sequence-add-between all-reds "blue")]) (for/list ([n (in-range 10)] [elt red-and-blues]) elt)) '("red" "blue" "red" "blue" "red" "blue" "red" "blue" "red" "blue") > (for ([text (sequence-add-between '("veni" "vidi" "duci") ", ")]) (display text)) veni, vidi, duci
Wraps a sequence, obligating it to produce elements with as many values as there are elem/c contracts, and obligating each value to satisfy the corresponding elem/c. The result is not guaranteed to be the same kind of sequence as the original value; for instance, a wrapped list is not guaranteed to satisfy list?.
If min-count is a number, the stream is required to have at least that many elements in it.
Examples:
> (for ([P predicates]) (printf "~s\n" (P "cat"))) #f predicates: broke its own contract promised: boolean? produced: 'cat in: an element of (sequence/c (-> any/c boolean?)) contract from: (definition predicates) blaming: (definition predicates) (assuming the contract is correct) at: eval:55:0 > (for ([(N S) numbers&strings]) (printf "~s: ~a\n" N S)) numbers&strings: broke its own contract promised: string? produced: 'three in: an element of (sequence/c number? string?) contract from: (definition numbers&strings) blaming: (definition numbers&strings) (assuming the contract is correct) at: eval:57:0 > (for ([x a-sequence] [i (in-naturals)]) (printf "~a is ~a\n" i x)) 0 is x a-sequence: broke its own contract promised: a sequence that contains at least 2 values produced: "x" in: (sequence/c #:min-count 2 char?) contract from: (definition a-sequence) blaming: (definition a-sequence) (assuming the contract is correct) at: eval:59:0
4.17.1.3.1 Additional Sequence Constructors and Functions🔗ℹ
Produces a sequence whose elements are the successive subparts ofstx. Equivalent to (stx->list lst).
An in-syntax application can provide better performance for syntax iteration when it appears directly in a for clause.
Example:
'(#<syntax:eval:61:0 1> #<syntax:eval:61:0 2> #<syntax:eval:61:0 3>)
Added in version 6.3 of package base.
Returns a sequence whose elements are lists with the first lengthelements of seq, then the next length and so on.
Example:
> (for/list ([e (in-slice 3 (in-range 8))]) e) '((0 1 2) (3 4 5) (6 7))
Added in version 6.3 of package base.
(initiate-sequence #:pos->element pos->element [#:early-next-pos early-next-pos] #:next-pos next-pos #:init-pos init-pos [#:continue-with-pos? continue-with-pos? #:continue-with-val? continue-with-val? #:continue-after-pos+val? continue-after-pos+val?]) pos->element : (any/c . -> . any) early-next-pos : (or/c (any/c . -> . any) #f) = #f next-pos : (any/c . -> . any/c) init-pos : any/c continue-with-pos? : (or/c (any/c . -> . any/c) #f) = #f continue-with-val? : (or/c (any/c ... . -> . any/c) #f) = #f continue-after-pos+val? : (or/c (any/c any/c ... . -> . any/c) #f) = #f
Returns values suitable for the thunk argument in make-do-sequence. See make-do-sequence for the meaning of each argument.
Examples:
> (define (in-alt-list xs) (make-do-sequence (λ () (initiate-sequence #:pos->element car #:next-pos (λ (xs) (cdr (cdr xs))) #:init-pos xs #:continue-with-pos? pair? #:continue-after-pos+val? (λ (xs _) (pair? (cdr xs))))))) > (sequence->list (in-alt-list '(1 2 3 4 5 6))) '(1 3 5) > (sequence->list (in-alt-list '(1 2 3 4 5 6 7))) '(1 3 5 7)
Added in version 8.10.0.5 of package base.