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 the for syntactic forms, with the procedures returned by sequence-generate, or by converting the sequence into a stream.
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-negative integer 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 the gen:stream generic interface. Streams are a suitable abstraction for data structures that are directly iterable. For example, a list is directly iterable with first and rest. 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:
> (define (make-unrolled-list-iterator ul) (unrolled-list-iterator 0 (unrolled-list-lov ul))) > (define ul1 (unrolled-list '(#(cracker biscuit) #(cookie scone)))) > (for/list ([x ul1]) x)'(cracker biscuit cookie scone)
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-sequence function 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, or sequence-generate*. Concretely, the thunk passed to make-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.
> (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
#tif
vcan be used as a
sequence,
#fotherwise.
Examples:
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
stepto the previous element. The sequence stops before an element that would be greater or equal to
endif
stepis non-negative, or less or equal to
endif
stepis negative.
An
in-rangeapplication can provide better performance for number iteration when it appears directly in a
forclause.
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-rangeapplication can provide better performance for number iteration when it appears directly in a
forclause.
Examples:
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-naturalsapplication can provide better performance for integer iteration when it appears directly in a
forclause.
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
lstdirectly as a sequence.
An
in-listapplication can provide better performance for list iteration when it appears directly in a
forclause.
See
forfor 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
mlstis
mutable list,
in-mlistinitially checks only whether
mlstis a
mutable pairor
null, since it could change during iteration.
An
in-mlistapplication can provide better performance for mutable list iteration when it appears directly in a
forclause.
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, and step 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 the exn: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:
If stop is not in [-1, (vector-length vec)], then the exn:fail:contract exception is raised.
If start is less than stop and step is negative, then the exn:fail:contract exception is raised. Similarly, if start is more than stop and step is positive, then the exn: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, and step 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, and step 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
inuntil it produces
eof.
Returns a sequence whose elements are read as characters from
in(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 of
read-lineis
'linefeed.
Returns a sequence equivalent to hash, except when bad-index-v is supplied.
If bad-index-v is supplied, then bad-index-v is returned as both the key and the value in the case that the hash is modified concurrently so that iteration does not have a valid 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: 2
key: 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, using
bad-index-vin 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, using
bad-index-vin 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 pairs, each containing a key and its value from hash (as opposed to using hash directly 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 its car and cdr.
Examples:
> (define table (hash 'a 1 'b 2))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 by path<?, 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 to producer, 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 of producer, 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 if producer 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.
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:
The char at position 0 is: h
The char at position 1 is: e
The char at position 2 is: l
The char at position 3 is: l
The char at position 4 is: o
Returns a sequence that is made of all input sequences, one after the other. Each
seqis
initiated only after the preceding
seqis exhausted. If a single
seqis provided, then
seqis returned; otherwise, the elements of each
seqmust all have the same number of values.
Similar to
in-sequences, but the sequences are repeated in an infinite cycle, where each
seqis
initiated afresh in each iteration. Beware that if no
seqs are provided or if all
seqs become empty, then the sequence produced by
in-cyclenever returns when an element is demanded—
or even when the sequence is
initiated, if all
seqs are initially empty.
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 of
seqhas multiple values or a single list value, then the values are combined in a list. In other words,
in-values*-sequenceis 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 before pos->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 and continue-after-pos+val?, but not with continue-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, so early-next-pos is applied just after pos->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
vis 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) > (for/list ([c (train 'engine (train 'boxcar (train 'caboose #f)))]) c)4.17.1.2 Sequence Conversion🔗ℹ'(engine boxcar caboose)
Converts a sequence to a
stream, which supports the
stream-firstand
stream-restoperations. Creation of the stream eagerly
initiatesthe sequence, but the stream lazily draws elements from the sequence, caching each element so that
stream-firstproduces 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 either stream-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 same seq are not necessarily independent.
Examples:
Initiatesa sequence and returns two thunks to extract elements from the sequence. The first returns
#tif 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:contractexception is raised.
Note that a sequence itself can have state, so multiple calls to sequence-generate on the same seq 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
#fif 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:contractexception is raised.
4.17.1.3 Additional Sequence Operations🔗ℹ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 first i 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 first i 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 a stream.
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 a stream.
Returns #t if f returns a true result on every element of s. If s is infinite and f never 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 f never 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 f returns 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 a stream.
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 a stream.
Examples:
'("red" "blue" "red" "blue" "red" "blue" "red" "blue" "red" "blue")
veni, vidi, duci
Wraps a
sequence, obligating it to produce elements with as many values as there are
elem/ccontracts, 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")))4.17.1.3.1 Additional Sequence Constructors and Functions🔗ℹ#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
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
Produces a sequence whose elements are the successive subparts of
stx. Equivalent to
(stx->list lst).
An
in-syntaxapplication can provide better performance for syntax iteration when it appears directly in a
forclause.
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 length elements of seq, then the next length and so on.
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
Added in version 8.10.0.5 of package base.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4