Legend:
Library
Module
Module type
Parameter
Class
Class type
Sequence of elements
A sequence represent a collection of elements, for which you never construct the complete representation.
Basically you should use a sequence when you would prefer using a list or a lazy-list but constructing the whole list explicitly would explode your memory.
All functions returning a sequence operates in time and space O(1).
Note that if you want a ``consumable sequence'', you should prefer using enumerations (from module BatEnum).
Build a sequence from a step function and an initial value. unfold f u returns empty if f u returns None, or fun () -> Cons (x, unfold f y) if f u returns Some (x, y).
For example, unfold (function [] -> None | h::t -> Some (h,t)) l is equivalent to List.to_seq l.
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.
map2 f s1 s2 returns a sequence of elements, resulting from combininig elements of s1 and s2 at the same index using f. The result is as long as the shortest argument.
equal ~eq s1 s2 compares elements of s1 and s2 pairwise using eq
parametereq
optional equality function (default Pervasives.(=))
since 2.2.0
Sequence scanning
Most functions in the following sections have a shortcut semantic similar to the behavior of the usual (&&) and (||) operators : they will force the sequence until they find an satisfying element, and then return immediately.
For example, for_all will only diverge if the sequence begins with an infinite number of true elements --- elements for which the predicate p returns true.
for_all p (cons a0 (cons a1 ...)) checks if all elements of the given sequence satisfy the predicate p. That is, it returns (p a0) && (p a1) && .... Eager, shortcut.
exists p (cons a0 (cons a1 ...)) checks if at least one element of the sequence satisfies the predicate p. That is, it returns (p a0) || (p a1) || .... Eager, shortcut.
filter p s returns the sequence of elements of s satisfying p. Lazy.
Note filter is lazy in that it returns a lazy sequence, but each element in the result is eagerly searched in the input sequence. Therefore, the access to a given element in the result will diverge if it is preceded, in the input sequence, by infinitely many false elements (elements on which the predicate p returns false).
Other functions that may drop an unbound number of elements (filter_map, take_while, etc.) have the same behavior.