Legend:
Library
Module
Module type
Parameter
Class
Class type
Simple and Efficient Iterators
The iterators are designed to allow easy transfer (mappings) between data structures, without defining n^2 conversions between the n types. The implementation relies on the assumption that a sequence can be iterated on as many times as needed; this choice allows for high performance of many combinators. However, for transient iterators, the persistent function is provided, storing elements of a transient iterator in memory; the iterator can then be used several times (See further).
Note that some combinators also return sequences (e.g. group). The transformation is computed on the fly every time one iterates over the resulting sequence. If a transformation performs heavy computation, persistent can also be used as intermediate storage.
Most functions are lazy, i.e. they do not actually use their arguments until their result is iterated on. For instance, if one calls map on a sequence, one gets a new sequence, but nothing else happens until this new sequence is used (by folding or iterating on it).
If a sequence is built from an iteration function that is repeatable (i.e. calling it several times always iterates on the same set of elements, for instance List.iter or Map.iter), then the resulting t object is also repeatable. For one-time iter functions such as iteration on a file descriptor or a Stream, the persistent function can be used to iterate and store elements in a memory structure; the result is a sequence that iterates on the elements of this memory structure, cheaply and repeatably.
type+'a t = ('a-> unit)-> unit
A sequence of values of type 'a. If you give it a function 'a -> unit it will be applied to every element of the sequence successively.
Sequence that calls the given function to produce elements. The sequence may be transient (depending on the function), and definitely is infinite. You may want to use take and persistent.
Cycle forever through the given sequence. Assume the given sequence can be traversed any amount of times (not transient). This yields an infinite sequence, you should use something like take not to loop forever.
Iterate on the sequence, storing elements in an efficient internal structure.. The resulting sequence can be iterated on as many times as needed. Note: calling persistent on an already persistent sequence will still make a new copy of the sequence!
Lazy version of persistent. When calling persistent_lazy s, a new sequence s' is immediately returned (without actually consuming s) in constant time; the first time s' is iterated on, it also consumes s and caches its content into a inner data structure that will back s' for future iterations.
warning: on the first traversal of s', if the traversal is interrupted prematurely (take, etc.) then s' will not be memorized, and the next call to s' will traverse s again.
Cartesian product of the sequences. When calling product a b, the caller MUST ensure that b can be traversed as many times as required (several times), possibly by calling persistent on it beforehand.
val join : join_row:('a->'b->'c option)->'at->'bt->'ct
join ~join_row a b combines every element of a with every element of b using join_row. If join_row returns None, then the two elements do not combine. Assume that b allows for multiple iterations.
Take elements while they satisfy the predicate, then stops iterating. Will work on an infinite sequence s if the predicate is false for at least one element of s.
Reverse the sequence. O(n) memory and time, needs the sequence to be finite. The result is persistent and does not depend on the input being repeatable.
Convert the sequence into a list. Preserves order of elements. This function is tail-recursive, but consumes 2*n memory. If order doesn't matter to you, consider to_rev_list.
Iterates on characters of the input (can block when one iterates over the sequence). If you need to iterate several times on this sequence, use persistent.
Pretty print a sequence of 'a, using the given pretty printer to print each elements. An optional separator string can be provided.
val pp_buf :
?sep:string ->(Buffer.t->'a-> unit)->Buffer.t->'at->
unit
Print into a buffer
val to_string : ?sep:string ->('a-> string)->'at-> string
Print into a string
Basic IO
Very basic interface to manipulate files as sequence of chunks/lines. The sequences take care of opening and closing files properly; every time one iterates over a sequence, the file is opened/closed again.
Example: copy a file "a" into file "b", removing blank lines: