Page
Library
Module
Module type
Parameter
Class
Class type
Source
CFStream.Stream
SourceStreams.
In general, functions that return a stream return a "fresh" stream, meaning that their count is set to 0.
Return a stream containing all elements of given data structure. Exact semantics depend on implementation. For example, elements in stream may or may not be ordered.
Return a data structure containing all elements in given stream, fully consuming the stream. Exact semantics depend on implementation. For example, duplicate elements in input may be ignored if the data structure is a set.
Raised when asking for an element of an empty stream, and by Genlex
parsers when none of the first components of the stream patterns is accepted.
Raised by Genlex
parsers when the first component of a stream pattern is accepted, but one of the following components is rejected.
Raised by operations working on more than one stream where all streams are expected to be of the same length.
Raised when an operation needs more elements from a stream than available.
Return first element in given stream if any and remove it from the stream.
Return first element of given stream without removing it from the stream, or None
if the stream is empty.
npeek s n
returns a list of the first n
elements in stream s
, or all of its remaining elements if less than n
elements are available. The elements are not removed from the stream.
Discard first element of given stream or do nothing if the stream is empty.
from f
returns a stream whose n
th element is determined by calling f n
, which should return Some x
to indicate value x
or None
to indicate the end of the stream. The stream is infinite if f
never returns None.
Return a stream of characters by reading from the input channel. WARNING: Semantics unclear if the channel is closed before the stream reads all of its input. For example, the stream appears to return values although the channel has been closed.
Return a stream of strings from the input. Each string has length at most buffer_size
.
range p until:q
creates a stream of integers [p, p+1, ..., q]
. If until
is omitted, the enumeration is not bounded. Behaviour is not-specified once max_int
has been reached.
unfold a0 f
returns the stream b0; b1; ...; bn
, where
Some (b0, a1) = f a0
,Some (b1, a2) = f a1
,Some (bn, a(n+1)) = f an
,None = f a(n+1)
The stream is infinite if f
never returns None.
Unless otherwise stated, functions in this section normally consume the entire stream. The exception is if a caller supplied function raises an exception, but that is not the normal intention of supplied functions.
Like iter
but operates on two streams. Stops when either stream becomes empty.
Like iter2
except streams required to be of equal length.
fold xs ~init ~f
returns f (...(f (f init x0) x1)...) xn
, that is for the stream a0; a1; ...; an
does the following calculations:
and returns bn
Like fold
but operates on two streams. Processing continues until either stream becomes empty.
Like fold2
except streams required to be of equal length.
Like fold
but all intermediate values are returned, not just the final value. If given stream s
is a0; a1; ...
, then scanl f init s
is the stream containing
scan
is similar to scanl
but without the init
value: if s
contains x0
, x1
, x2
..., scan s ~f
contains
For instance, scan (1 -- 10) ~f:( * )
will produce an enumeration containing the successive values of the factorial function. Returns an empty stream if the input stream is empty as well.
Indexed variants of the previous higher-order functions. The index provided to the ~f
argument is the count of the stream, that is the number of discarded elements before the reaching the current one. For functions iterating on two streams, the ~f
is thus provided two indices, since the current count may differ from one stream to another.
reduce xs ~f
returns f (...(f (f x1 x2) x3)...) xn
Operations that scan a stream for various purposes. Unlike iterators, these operations are not inherently meant to consume streams, although they do partially or fully, due to the nature of streams.
exists s ~f
returns true
if there is some x
in s
such that f x
is true. The stream is consumed through and including x
.
for_all s ~f
returns true
if f x
is true for every x
in s
.
find e ~f
returns either Some x
where x
is the first element of e
such that f x
returns true
, consuming the stream up to and including the found element, or None
if no such element exists in the stream, consuming the whole stream in the search.
Since find
(eagerly) consumes a prefix of the stream, it can be used several times on the same stream to find the next element.
Same as find
except that it raises an exception Not_found
instead of returning None
.
Extract a subset of a stream or map a stream into another type of stream.
take xs ~n
builds a fresh stream from xs
containing the d
first elements of xs
where d = min n l
and l
is the length of xs
. As it is fresh, the count of the resulting stream starts from 0
whatever the count of xs
is.
Same as take
but takes elements from the input enum as long as f
evaluates to true
.
Similar to drop
: drop_while xs ~f
removes elements from xs
and stops when f
evals to false on the head element.
Similar to drop
but returns the stream in input (useful in chained composition).
Similar to skip
: skip_while xs ~f
removes elements from xs
and stops when f
evals to false on the head element.
Indexed variants of the previous prefix/suffix constructors
span test e
produces two streams (hd, tl)
, such that hd
is the same as take_while test e
and tl
is the same as skip_while test e
.
group xs f
applies f
to the elements of xs
and distribute them according to the return value of f
. Let ys
= group xs f
, then xs
= concat ys
and in each stream s
of ys
, all values give the same value with f
.
Same as group
but with a comparison function instead of a mapping.
Given a stream with items x0, x1, x2, x3,...
, the returned stream will be pairs of items (x0,x1), (x2,x3), ...
.
combine
transforms a pair of streams into a stream of pairs of corresponding elements. If one stream is short, excess elements of the longer stream are ignored.
merge test (a, b)
merge the elements from a
and b
into a single stream. At each step, test
is applied to the first element of a
and the first element of b
to determine which should get first into the resulting stream. If a
or b
runs out of elements, the process will append all elements of the other stream to the result.
partition e ~f
splits e
into two streams, where the first stream have all the elements satisfying f
, the second stream is opposite. The order of elements in the source stream is preserved.
uniq e
returns a duplicate of e
with repeated values omitted. (similar to unix's uniq
command)
Convert/create a stream to/from another data structure.