Sequences (functional iterators).
'a Seq.t is a delayed list, i.e. a list where some
evaluation is needed to access the next element. This makes it possible
to build infinite sequences, to build sequences as we traverse them, and
to transform them in a lazy fashion rather than upfront.
unit -> 'a node
The type of delayed lists containing elements of type
Note that the concrete list node
'a node is delayed under a closure,
lazy block, which means it might be recomputed every time
we access it.
A fully-evaluated list node, either empty or containing an element and a delayed tail.
val empty :
The empty sequence, containing no elements.
val return :
'a -> 'a t
The singleton sequence containing only the given element.
cons x xs is the sequence containing the element
x followed by
append xs ys is the sequence
xs followed by the sequence
map f seq returns a new sequence whose elements are the elements of
seq, transformed by
This transformation is lazy, it only applies when the result is traversed.
seq = [1;2;3], then
map f seq = [f 1; f 2; f 3].
Remove from the sequence the elements that do not satisfy the given predicate. This transformation is lazy, it only applies when the result is traversed.
Apply the function to every element; if
f x = None then
x is dropped;
f x = Some y then
y is returned.
This transformation is lazy, it only applies when the result is
concatenate a sequence of sequences.
Map each element to a subsequence, then return each element of this sub-sequence in turn. This transformation is lazy, it only applies when the result is traversed.
val fold_left :
('a -> 'b -> 'a) -> 'a -> 'b t -> 'a
Traverse the sequence from left to right, combining each element with the accumulator using the given function. The traversal happens immediately and will not terminate on infinite sequences.
val iter :
('a -> unit) -> 'a t -> unit
Iterate on the sequence, calling the (imperative) function on every element. The traversal happens immediately and will not terminate on infinite sequences.
val unfold :
('b -> ('a * 'b) option) -> 'b -> 'a t
Build a sequence from a step function and an initial value.
unfold f u returns
f u returns
fun () -> Cons (x, unfold f y) if
f u returns
Some (x, y).
unfold (function  -> None | h::t -> Some (h,t)) l
is equivalent to