OCaml package documentation
+-
+
- sequence 1.1 +
diff --git a/1.0/index.html b/1.0/index.html new file mode 100644 index 0000000..f7c1c18 --- /dev/null +++ b/1.0/index.html @@ -0,0 +1,17 @@ + + +
+val of_bigarray : ('a, _, _) Bigarray.Array1.t ‑> 'a Sequence.tIterate on the elements of a 1-D array
+This library exposes the following toplevel modules:
| SequenceBigarray |
.
\ No newline at end of file diff --git a/1.2/sequence/SequenceLabels/.dune-keep b/1.0/sequence/Sequence/.jbuilder-keep similarity index 100% rename from 1.2/sequence/SequenceLabels/.dune-keep rename to 1.0/sequence/Sequence/.jbuilder-keep diff --git a/1.0/sequence/Sequence/IO/index.html b/1.0/sequence/Sequence/IO/index.html new file mode 100644 index 0000000..8f33384 --- /dev/null +++ b/1.0/sequence/Sequence/IO/index.html @@ -0,0 +1,11 @@ + +val lines_of : ?mode:int ‑> ?flags:Pervasives.open_flag list ‑> string ‑> string tlines_of filename reads all lines of the given file. It raises the
+same exception as would opening the file and read from it, except
+from End_of_file (which is caught). The file is always properly
+closed.
+Every time the sequence is iterated on, the file is opened again, so
+different iterations might return different results
0o644[Open_rdonly]val chunks_of : ?mode:int ‑> ?flags:Pervasives.open_flag list ‑> ?size:int ‑> string ‑> string tRead chunks of the given size from the file. The last chunk might be
+smaller. Behaves like lines_of regarding errors and options.
+Every time the sequence is iterated on, the file is opened again, so
+different iterations might return different results
val write_to : ?mode:int ‑> ?flags:Pervasives.open_flag list ‑> string ‑> string t ‑> unitwrite_to filename seq writes all strings from seq into the given
+file. It takes care of opening and closing the file.
0o644open_out_gen. Default: [Open_creat;Open_wronly].val write_bytes_to : ?mode:int ‑> ?flags:Pervasives.open_flag list ‑> string ‑> Bytes.t t ‑> unitval write_lines : ?mode:int ‑> ?flags:Pervasives.open_flag list ‑> string ‑> string t ‑> unitSame as write_to, but intercales '\n' between each string
val write_bytes_lines : ?mode:int ‑> ?flags:Pervasives.open_flag list ‑> string ‑> Bytes.t t ‑> unitval (--) : int ‑> int ‑> int ta -- b is the range of integers from a to b, both included,
+in increasing order. It will therefore be empty if a > b.
val (--^) : int ‑> int ‑> int ta --^ b is the range of integers from b to a, both included,
+in decreasing order (starts from a).
+It will therefore be empty if a < b.
module type S : sig ... endmodule type S : sig ... endThe 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) ‑> unitA 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.
NOTE Type ('a, 'b) t2 = ('a -> 'b -> unit) -> unit
+has been removed and subsumed by ('a * 'b) t
val from_fun : (unit ‑> 'a option) ‑> 'a tCall the function repeatedly until it returns None. This +sequence is transient, use persistent if needed!
val repeat : 'a ‑> 'a tInfinite sequence of the same element. You may want to look +at take and the likes if you iterate on it.
val forever : (unit ‑> 'b) ‑> 'b tSequence 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.
val iter : ('a ‑> unit) ‑> 'a t ‑> unitConsume the sequence, passing all its arguments to the function.
+Basically iter f seq is just seq f.
val foldi : ('a ‑> int ‑> 'b ‑> 'a) ‑> 'a ‑> 'b t ‑> 'aFold over elements of the sequence and their index, consuming it
fold_filter_map f acc l is a fold_map-like function, but the
+function can choose to skip an element by retuning None.
Map objects two by two. lazily. +The last element is kept in the sequence if the count is odd.
val mem : ?eq:('a ‑> 'a ‑> bool) ‑> 'a ‑> 'a t ‑> boolIs the value a member of the sequence?
(=))val find : ('a ‑> 'b option) ‑> 'a t ‑> 'b optionFind the first element on which the function doesn't return None
val find_pred : ('a ‑> bool) ‑> 'a t ‑> 'a optionfind_pred p l finds the first element of l that satisfies p,
+or returns None if no element satisfies p
val find_pred_exn : ('a ‑> bool) ‑> 'a t ‑> 'aUnsafe version of find_pred
Append sequences. Iterating on the result is like iterating +on the each sequence of the list in order.
Monadic bind. Intuitively, it applies the function to every
+element of the initial sequence, and calls concat.
+Formerly flatMap
seq_list l returns all the ways to pick one element in each sub-sequence
+in l. Assumes the sub-sequences can be iterated on several times.
seq_list_map f l maps f over every element of l,
+then calls seq_list
val filter_count : ('a ‑> bool) ‑> 'a t ‑> intCount how many elements satisfy the given predicate
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.
Sort the sequence. Eager, O(n) ram and O(n ln(n)) time. +It iterates on elements of the argument sequence immediately, +before it sorts them.
val sorted : ?cmp:('a ‑> 'a ‑> int) ‑> 'a t ‑> boolChecks whether the sequence is sorted. Eager, same as sort.
Group equal elements, disregarding their order of appearance.
+The result sequence is traversable as many times as required.
+precondition: for any x and y, if eq x y then hash x=hash y must hold.
Map each distinct element to its number of occurrences in the whole seq.
+Similar to group_by seq |> map (fun l->List.hd l, List.length l)
+precondition: for any x and y, if eq x y then hash x=hash y must hold.
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 diagonal_l : 'a list ‑> ('a * 'a) tAll pairs of distinct positions of the list. diagonal l will
+return the sequence of all List.nth i l, List.nth j l if i < j.
All pairs of distinct positions of the sequence. +Iterates only once on the sequence, which must be finite.
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.
val join_by : ?eq:'key equal ‑> ?hash:'key hash ‑> ('a ‑> 'key) ‑> ('b ‑> 'key) ‑> merge:('key ‑> 'a ‑> 'b ‑> 'c option) ‑> 'a t ‑> 'b t ‑> 'c tjoin key1 key2 ~merge is a binary operation
+that takes two sequences a and b, projects their
+elements resp. with key1 and key2, and combine
+values (x,y) from (a,b) with the same key
+using merge. If merge returns None, the combination
+of values is discarded.
+precondition: for any x and y, if eq x y then hash x=hash y must hold.
val join_all_by : ?eq:'key equal ‑> ?hash:'key hash ‑> ('a ‑> 'key) ‑> ('b ‑> 'key) ‑> merge:('key ‑> 'a list ‑> 'b list ‑> 'c option) ‑> 'a t ‑> 'b t ‑> 'c tjoin_all_by key1 key2 ~merge is a binary operation
+that takes two sequences a and b, projects their
+elements resp. with key1 and key2, and, for each key k
+occurring in at least one of them:
+
l1 of elements of a that map to kl2 of elements of b that map to kmerge k l1 l2. If merge returns None, the combination
+of values is discarded, otherwise it returns Some c
+and c is inserted in the result.val group_join_by : ?eq:'a equal ‑> ?hash:'a hash ‑> ('b ‑> 'a) ‑> 'a t ‑> 'b t ‑> ('a * 'b list) tgroup_join_by key2 associates to every element x of
+the first sequence, all the elements y of the second
+sequence such that eq x (key y). Elements of the first
+sequences without corresponding values in the second one
+are mapped to []
+precondition: for any x and y, if eq x y then hash x=hash y must hold.
Intersection of two collections. Each element will occur at most once
+in the result. Eager.
+precondition: for any x and y, if eq x y then hash x=hash y must hold.
Union of two collections. Each element will occur at most once
+in the result. Eager.
+precondition: for any x and y, if eq x y then hash x=hash y must hold.
subset a b returns true if all elements of a belong to b. Eager.
+precondition: for any x and y, if eq x y then hash x=hash y must hold.
val unfoldr : ('b ‑> ('a * 'b) option) ‑> 'b ‑> 'a tunfoldr f b will apply f to b. If it
+yields Some (x,b') then x is returned
+and unfoldr recurses with b'.
val max : ?lt:('a ‑> 'a ‑> bool) ‑> 'a t ‑> 'a optionMax element of the sequence, using the given comparison function.
m where m is the maximal
+element otherwiseval max_exn : ?lt:('a ‑> 'a ‑> bool) ‑> 'a t ‑> 'aUnsafe version of max
val min : ?lt:('a ‑> 'a ‑> bool) ‑> 'a t ‑> 'a optionMin element of the sequence, using the given comparison function. +see max for more details.
val min_exn : ?lt:('a ‑> 'a ‑> bool) ‑> 'a t ‑> 'aUnsafe version of min
val head_exn : 'a t ‑> 'aFirst element, if any, fails
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.
val fold_while : ('a ‑> 'b ‑> 'a * [ `Stop | `Continue ]) ‑> 'a ‑> 'b t ‑> 'aFolds over elements of the sequence, stopping early if the accumulator
+returns ('a, `Stop)
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.
val fold2 : ('c ‑> 'a ‑> 'b ‑> 'c) ‑> 'c ‑> ('a * 'b) t ‑> 'cval iter2 : ('a ‑> 'b ‑> unit) ‑> ('a * 'b) t ‑> unitval to_list : 'a t ‑> 'a listConvert 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.
val to_rev_list : 'a t ‑> 'a listGet the list of the reversed sequence (more efficient than to_list)
val of_list : 'a list ‑> 'a tSimilar to zip_i but returns a normal sequence of tuples
val to_array : 'a t ‑> 'a arrayConvert to an array. Currently not very efficient because +an intermediate list is used.
val of_array : 'a array ‑> 'a tval array_slice : 'a array ‑> int ‑> int ‑> 'a tarray_slice a i j Sequence of elements whose indexes range
+from i to j
val to_stream : 'a t ‑> 'a Stream.tConvert to a stream. linear in memory and time (a copy is made in memory)
val hashtbl_add : ('a, 'b) Hashtbl.t ‑> ('a * 'b) t ‑> unitAdd elements of the sequence to the hashtable, with +Hashtbl.add
val hashtbl_replace : ('a, 'b) Hashtbl.t ‑> ('a * 'b) t ‑> unitAdd elements of the sequence to the hashtable, with +Hashtbl.replace (erases conflicting bindings)
val to_hashtbl : ('a * 'b) t ‑> ('a, 'b) Hashtbl.tBuild a hashtable from a sequence of key/value pairs
val hashtbl_keys : ('a, 'b) Hashtbl.t ‑> 'a tval hashtbl_values : ('a, 'b) Hashtbl.t ‑> 'b tval of_str : string ‑> char tval to_str : char t ‑> stringval concat_str : string t ‑> stringConcatenate strings together, eagerly. +Also see intersperse to add a separator.
exception OneShotSequenceRaised when the user tries to iterate several times on +a transient iterator
val of_in_channel : Pervasives.in_channel ‑> char tIterates 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.
val int_range : start:int ‑> stop:int ‑> int tval int_range_dec : start:int ‑> stop:int ‑> int tval int_range_by : step:int ‑> int ‑> int ‑> int tint_range_by ~step i j is the range starting at i, including j,
+where the difference between successive elements is step.
+use a negative step for a decreasing sequence.
step=0val of_set : (module Set.S with type elt = 'a and type t = 'b) ‑> 'b ‑> 'a tConvert the given set to a sequence. The set module must be provided.
val to_set : (module Set.S with type elt = 'a and type t = 'b) ‑> 'a t ‑> 'bConvert the sequence to a set, given the proper set module
module Set : sig ... endmodule Map : sig ... endval random_int : int ‑> int tInfinite sequence of random integers between 0 and +the given higher bound (see Random.int)
val random_float : float ‑> float tval random_list : 'a list ‑> 'a tInfinite sequence of random elements of the list. Basically the +same as random_array.
shuffle_buffer n seq returns a sequence of element of seq in random
+order. The shuffling is *not* uniform. Uses O(n) memory.
The first n elements of the sequence are consumed immediately. The
+rest is consumed lazily.
val sample : int ‑> 'a t ‑> 'a arraysample n seq returns k samples of seq, with uniform probability.
+It will consume the sequence and use O(n) memory.
It returns an array of size min (length seq) n.
module Infix : sig ... endinclude module type of Infixval (--) : int ‑> int ‑> int ta -- b is the range of integers from a to b, both included,
+in increasing order. It will therefore be empty if a > b.
val (--^) : int ‑> int ‑> int ta --^ b is the range of integers from b to a, both included,
+in decreasing order (starts from a).
+It will therefore be empty if a < b.
val pp_seq : ?sep:string ‑> (Format.formatter ‑> 'a ‑> unit) ‑> Format.formatter ‑> 'a t ‑> unitPretty 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 ‑> 'a t ‑> unitPrint into a buffer
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:
Sequence.(IO.lines_of "a" |> filter (fun l-> l<> "") |> IO.write_lines "b");;By chunks of 4096 bytes:
Sequence.IO.(chunks_of ~size:4096 "a" |> write_to "b");;Read the lines of a file into a list:
Sequence.IO.lines "a" |> Sequence.to_listmodule IO : sig ... endval lines_of : ?mode:int ‑> ?flags:Pervasives.open_flag list ‑> string ‑> string tlines_of filename reads all lines of the given file. It raises the
+same exception as would opening the file and read from it, except
+from End_of_file (which is caught). The file is always properly
+closed.
+Every time the sequence is iterated on, the file is opened again, so
+different iterations might return different results
0o644[Open_rdonly]val chunks_of : ?mode:int ‑> ?flags:Pervasives.open_flag list ‑> ?size:int ‑> string ‑> string tRead chunks of the given size from the file. The last chunk might be
+smaller. Behaves like lines_of regarding errors and options.
+Every time the sequence is iterated on, the file is opened again, so
+different iterations might return different results
val write_to : ?mode:int ‑> ?flags:Pervasives.open_flag list ‑> string ‑> string t ‑> unitwrite_to filename seq writes all strings from seq into the given
+file. It takes care of opening and closing the file.
0o644open_out_gen. Default: [Open_creat;Open_wronly].val write_bytes_to : ?mode:int ‑> ?flags:Pervasives.open_flag list ‑> string ‑> Bytes.t t ‑> unitval write_lines : ?mode:int ‑> ?flags:Pervasives.open_flag list ‑> string ‑> string t ‑> unitSame as write_to, but intercales '\n' between each string
val write_bytes_lines : ?mode:int ‑> ?flags:Pervasives.open_flag list ‑> string ‑> Bytes.t t ‑> unitval (--) : int ‑> int ‑> int ta -- b is the range of integers from a to b, both included,
+in increasing order. It will therefore be empty if a > b.
val (--^) : int ‑> int ‑> int ta --^ b is the range of integers from b to a, both included,
+in decreasing order (starts from a).
+It will therefore be empty if a < b.
module type S : sig ... endmodule type S : sig ... endVersion of Sequence with labels
type +'a t = ('a ‑> unit) ‑> unitA 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.
NOTE Type ('a, 'b) t2 = ('a -> 'b -> unit) -> unit
+has been removed and subsumed by ('a * 'b) t
val from_fun : (unit ‑> 'a option) ‑> 'a tCall the function repeatedly until it returns None. This +sequence is transient, use persistent if needed!
val repeat : 'a ‑> 'a tInfinite sequence of the same element. You may want to look +at take and the likes if you iterate on it.
val forever : (unit ‑> 'b) ‑> 'b tSequence 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.
val iter : f:('a ‑> unit) ‑> 'a t ‑> unitConsume the sequence, passing all its arguments to the function.
+Basically iter f seq is just seq f.
val iteri : f:(int ‑> 'a ‑> unit) ‑> 'a t ‑> unitIterate on elements and their index in the sequence
val fold : f:('a ‑> 'b ‑> 'a) ‑> init:'a ‑> 'b t ‑> 'aFold over elements of the sequence, consuming it
val foldi : f:('a ‑> int ‑> 'b ‑> 'a) ‑> init:'a ‑> 'b t ‑> 'aFold over elements of the sequence and their index, consuming it
fold_filter_map f acc l is a fold_map-like function, but the
+function can choose to skip an element by retuning None.
Map objects two by two. lazily. +The last element is kept in the sequence if the count is odd.
val mem : ?eq:('a ‑> 'a ‑> bool) ‑> x:'a ‑> 'a t ‑> boolIs the value a member of the sequence?
(=))val find : ('a ‑> 'b option) ‑> 'a t ‑> 'b optionFind the first element on which the function doesn't return None
val find_pred : f:('a ‑> bool) ‑> 'a t ‑> 'a optionfind_pred p l finds the first element of l that satisfies p,
+or returns None if no element satisfies p
val find_pred_exn : f:('a ‑> bool) ‑> 'a t ‑> 'aUnsafe version of find_pred
Append sequences. Iterating on the result is like iterating +on the each sequence of the list in order.
seq_list l returns all the ways to pick one element in each sub-sequence
+in l. Assumes the sub-sequences can be iterated on several times.
seq_list_map f l maps f over every element of l,
+then calls seq_list
val filter_count : f:('a ‑> bool) ‑> 'a t ‑> intCount how many elements satisfy the given predicate
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.
Sort the sequence. Eager, O(n) ram and O(n ln(n)) time. +It iterates on elements of the argument sequence immediately, +before it sorts them.
val sorted : ?cmp:('a ‑> 'a ‑> int) ‑> 'a t ‑> boolChecks whether the sequence is sorted. Eager, same as sort.
Group equal elements, disregarding their order of appearance.
+The result sequence is traversable as many times as required.
+precondition: for any x and y, if eq x y then hash x=hash y must hold.
Map each distinct element to its number of occurrences in the whole seq.
+Similar to group_by seq |> map (fun l->List.hd l, List.length l)
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 diagonal_l : 'a list ‑> ('a * 'a) tAll pairs of distinct positions of the list. diagonal l will
+return the sequence of all List.nth i l, List.nth j l if i < j.
All pairs of distinct positions of the sequence. +Iterates only once on the sequence, which must be finite.
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.
val join_by : ?eq:'key equal ‑> ?hash:'key hash ‑> ('a ‑> 'key) ‑> ('b ‑> 'key) ‑> merge:('key ‑> 'a ‑> 'b ‑> 'c option) ‑> 'a t ‑> 'b t ‑> 'c tjoin key1 key2 ~merge is a binary operation
+that takes two sequences a and b, projects their
+elements resp. with key1 and key2, and combine
+values (x,y) from (a,b) with the same key
+using merge. If merge returns None, the combination
+of values is discarded.
+precondition: for any x and y, if eq x y then hash x=hash y must hold.
val join_all_by : ?eq:'key equal ‑> ?hash:'key hash ‑> ('a ‑> 'key) ‑> ('b ‑> 'key) ‑> merge:('key ‑> 'a list ‑> 'b list ‑> 'c option) ‑> 'a t ‑> 'b t ‑> 'c tjoin_all_by key1 key2 ~merge is a binary operation
+that takes two sequences a and b, projects their
+elements resp. with key1 and key2, and, for each key k
+occurring in at least one of them:
+
l1 of elements of a that map to kl2 of elements of b that map to kmerge k l1 l2. If merge returns None, the combination
+of values is discarded, otherwise it returns Some c
+and c is inserted in the result.val group_join_by : ?eq:'a equal ‑> ?hash:'a hash ‑> ('b ‑> 'a) ‑> 'a t ‑> 'b t ‑> ('a * 'b list) tgroup_join_by key2 associates to every element x of
+the first sequence, all the elements y of the second
+sequence such that eq x (key y). Elements of the first
+sequences without corresponding values in the second one
+are mapped to []
+precondition: for any x and y, if eq x y then hash x=hash y must hold.
Intersection of two collections. Each element will occur at most once
+in the result. Eager.
+precondition: for any x and y, if eq x y then hash x=hash y must hold.
Union of two collections. Each element will occur at most once
+in the result. Eager.
+precondition: for any x and y, if eq x y then hash x=hash y must hold.
subset a b returns true if all elements of a belong to b. Eager.
+precondition: for any x and y, if eq x y then hash x=hash y must hold.
val unfoldr : ('b ‑> ('a * 'b) option) ‑> 'b ‑> 'a tunfoldr f b will apply f to b. If it
+yields Some (x,b') then x is returned
+and unfoldr recurses with b'.
val max : ?lt:('a ‑> 'a ‑> bool) ‑> 'a t ‑> 'a optionMax element of the sequence, using the given comparison function.
m where m is the maximal
+element otherwiseval max_exn : ?lt:('a ‑> 'a ‑> bool) ‑> 'a t ‑> 'aUnsafe version of max
val min : ?lt:('a ‑> 'a ‑> bool) ‑> 'a t ‑> 'a optionMin element of the sequence, using the given comparison function. +see max for more details.
val min_exn : ?lt:('a ‑> 'a ‑> bool) ‑> 'a t ‑> 'aUnsafe version of min
val head_exn : 'a t ‑> 'aFirst element, if any, fails
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.
val fold_while : f:('a ‑> 'b ‑> 'a * [ `Stop | `Continue ]) ‑> init:'a ‑> 'b t ‑> 'aFolds over elements of the sequence, stopping early if the accumulator
+returns ('a, `Stop)
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.
val fold2 : f:('c ‑> 'a ‑> 'b ‑> 'c) ‑> init:'c ‑> ('a * 'b) t ‑> 'cval iter2 : f:('a ‑> 'b ‑> unit) ‑> ('a * 'b) t ‑> unitval to_list : 'a t ‑> 'a listConvert 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.
val to_rev_list : 'a t ‑> 'a listGet the list of the reversed sequence (more efficient than to_list)
val of_list : 'a list ‑> 'a tSimilar to zip_i but returns a normal sequence of tuples
val to_array : 'a t ‑> 'a arrayConvert to an array. Currently not very efficient because +an intermediate list is used.
val of_array : 'a array ‑> 'a tval array_slice : 'a array ‑> int ‑> int ‑> 'a tarray_slice a i j Sequence of elements whose indexes range
+from i to j
val to_stream : 'a t ‑> 'a Stream.tConvert to a stream. linear in memory and time (a copy is made in memory)
val hashtbl_add : ('a, 'b) Hashtbl.t ‑> ('a * 'b) t ‑> unitAdd elements of the sequence to the hashtable, with +Hashtbl.add
val hashtbl_replace : ('a, 'b) Hashtbl.t ‑> ('a * 'b) t ‑> unitAdd elements of the sequence to the hashtable, with +Hashtbl.replace (erases conflicting bindings)
val to_hashtbl : ('a * 'b) t ‑> ('a, 'b) Hashtbl.tBuild a hashtable from a sequence of key/value pairs
val hashtbl_keys : ('a, 'b) Hashtbl.t ‑> 'a tval hashtbl_values : ('a, 'b) Hashtbl.t ‑> 'b tval of_str : string ‑> char tval to_str : char t ‑> stringval concat_str : string t ‑> stringConcatenate strings together, eagerly. +Also see intersperse to add a separator.
exception OneShotSequenceRaised when the user tries to iterate several times on +a transient iterator
val of_in_channel : Pervasives.in_channel ‑> char tIterates 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.
val int_range : start:int ‑> stop:int ‑> int tval int_range_dec : start:int ‑> stop:int ‑> int tval int_range_by : step:int ‑> start:int ‑> stop:int ‑> int tint_range_by ~step ~start:i ~stop:j is the range starting at i, including j,
+where the difference between successive elements is step.
+use a negative step for a decreasing sequence.
step=0val of_set : (module Set.S with type elt = 'a and type t = 'b) ‑> 'b ‑> 'a tConvert the given set to a sequence. The set module must be provided.
val to_set : (module Set.S with type elt = 'a and type t = 'b) ‑> 'a t ‑> 'bConvert the sequence to a set, given the proper set module
module Set : sig ... endmodule Map : sig ... endval random_int : int ‑> int tInfinite sequence of random integers between 0 and +the given higher bound (see Random.int)
val random_float : float ‑> float tval random_list : 'a list ‑> 'a tInfinite sequence of random elements of the list. Basically the +same as random_array.
shuffle_buffer n seq returns a sequence of element of seq in random
+order. The shuffling is not uniform. Uses O(n) memory.
The first n elements of the sequence are consumed immediately. The
+rest is consumed lazily.
val sample : n:int ‑> 'a t ‑> 'a arraysample n seq returns k samples of seq, with uniform probability.
+It will consume the sequence and use O(n) memory.
It returns an array of size min (length seq) n.
module Infix : sig ... endinclude module type of Infixval (--) : int ‑> int ‑> int ta -- b is the range of integers from a to b, both included,
+in increasing order. It will therefore be empty if a > b.
val (--^) : int ‑> int ‑> int ta --^ b is the range of integers from b to a, both included,
+in decreasing order (starts from a).
+It will therefore be empty if a < b.
val pp_seq : ?sep:string ‑> (Format.formatter ‑> 'a ‑> unit) ‑> Format.formatter ‑> 'a t ‑> unitPretty 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 ‑> 'a t ‑> unitPrint into a buffer
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:
Sequence.(IO.lines_of "a" |> filter (fun l-> l<> "") |> IO.write_lines "b");;By chunks of 4096 bytes:
Sequence.IO.(chunks_of ~size:4096 "a" |> write_to "b");;Read the lines of a file into a list:
Sequence.IO.lines "a" |> Sequence.to_listmodule IO : sig ... end+This library exposes the following toplevel modules:
| Sequence | |
| SequenceLabels |
.
\ No newline at end of file diff --git a/1.1/highlight.pack.js b/1.1/highlight.pack.js new file mode 100644 index 0000000..40370e8 --- /dev/null +++ b/1.1/highlight.pack.js @@ -0,0 +1,2 @@ +/*! highlight.js v9.12.0 | BSD3 License | git.io/hljslicense */ +!function(e){var n="object"==typeof window&&window||"object"==typeof self&&self;"undefined"!=typeof exports?e(exports):n&&(n.hljs=e({}),"function"==typeof define&&define.amd&&define([],function(){return n.hljs}))}(function(e){function n(e){return e.replace(/&/g,"&").replace(//g,">")}function t(e){return e.nodeName.toLowerCase()}function r(e,n){var t=e&&e.exec(n);return t&&0===t.index}function a(e){return k.test(e)}function i(e){var n,t,r,i,o=e.className+" ";if(o+=e.parentNode?e.parentNode.className:"",t=B.exec(o))return w(t[1])?t[1]:"no-highlight";for(o=o.split(/\s+/),n=0,r=o.length;r>n;n++)if(i=o[n],a(i)||w(i))return i}function o(e){var n,t={},r=Array.prototype.slice.call(arguments,1);for(n in e)t[n]=e[n];return r.forEach(function(e){for(n in e)t[n]=e[n]}),t}function u(e){var n=[];return function r(e,a){for(var i=e.firstChild;i;i=i.nextSibling)3===i.nodeType?a+=i.nodeValue.length:1===i.nodeType&&(n.push({event:"start",offset:a,node:i}),a=r(i,a),t(i).match(/br|hr|img|input/)||n.push({event:"stop",offset:a,node:i}));return a}(e,0),n}function c(e,r,a){function i(){return e.length&&r.length?e[0].offset!==r[0].offset?e[0].offsetIterThe 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 an iterator 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 iterators (e.g. group). The transformation is computed on the fly every time one iterates over the resulting iterator. 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 an iterator, one gets a new iterator, but nothing else happens until this new iterator is used (by folding or iterating on it).
If an iterator 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 an iterator that iterates on the elements of this memory structure, cheaply and repeatably.
type +'a t = ('a -> unit) -> unitAn iterator of values of type 'a. If you give it a function 'a -> unit it will be applied to every element of the iterator successively.
type +'a iter = 'a tNOTE Type ('a, 'b) t2 = ('a -> 'b -> unit) -> unit has been removed and subsumed by ('a * 'b) t
val from_iter : (('a -> unit) -> unit) -> 'a tBuild an iterator from a iter function
val from_labelled_iter : (f:('a -> unit) -> unit) -> 'a tBuild an iterator from a labelled iter function
val from_fun : (unit -> 'a option) -> 'a tCall the function repeatedly until it returns None. This iterator is transient, use persistent if needed!
val empty : 'a tEmpty iterator. It contains no element.
val singleton : 'a -> 'a tSingleton iterator, with exactly one element.
val doubleton : 'a -> 'a -> 'a tIterator with exactly two elements
val init : (int -> 'a) -> 'a tinit f is the infinite iterator f 0; f 1; f 2; ….
val repeat : 'a -> 'a tInfinite iterator of the same element. You may want to look at take and the likes if you iterate on it.
val iterate : ('a -> 'a) -> 'a -> 'a titerate f x is the infinite iterator x, f(x), f(f(x)), ...
val forever : (unit -> 'b) -> 'b tIterator that calls the given function to produce elements. The iterator may be transient (depending on the function), and definitely is infinite. You may want to use take and persistent.
val cycle : 'a t -> 'a tCycle forever through the given iterator. Assume the given iterator can be traversed any amount of times (not transient). This yields an infinite iterator, you should use something like take not to loop forever.
val unfoldr : ('b -> ('a * 'b) option) -> 'b -> 'a tunfoldr f b will apply f to b. If it yields Some (x,b') then x is returned and unfoldr recurses with b'.
val iter : ('a -> unit) -> 'a t -> unitConsume the iterator, passing all its arguments to the function. Basically iter f seq is just seq f.
val iteri : (int -> 'a -> unit) -> 'a t -> unitIterate on elements and their index in the iterator
val fold : ('a -> 'b -> 'a) -> 'a -> 'b t -> 'aFold over elements of the iterator, consuming it
val foldi : ('a -> int -> 'b -> 'a) -> 'a -> 'b t -> 'aFold over elements of the iterator and their index, consuming it
val fold_map : ('acc -> 'a -> 'acc * 'b) -> 'acc -> 'a t -> 'b tfold_map f acc l is like map, but it carries some state as in fold. The state is not returned, it is just used to thread some information to the map function.
val fold_filter_map : ('acc -> 'a -> 'acc * 'b option) -> 'acc -> 'a t -> 'b tfold_filter_map f acc l is a fold_map-like function, but the function can choose to skip an element by retuning None.
val map_by_2 : ('a -> 'a -> 'a) -> 'a t -> 'a tMap objects two by two. lazily. The last element is kept in the iterator if the count is odd.
val for_all : ('a -> bool) -> 'a t -> boolDo all elements satisfy the predicate?
val exists : ('a -> bool) -> 'a t -> boolExists there some element satisfying the predicate?
val mem : ?eq:('a -> 'a -> bool) -> 'a -> 'a t -> boolIs the value a member of the iterator?
the equality predicate to use (default (=))
val find : ('a -> 'b option) -> 'a t -> 'b optionFind the first element on which the function doesn't return None
val find_pred : ('a -> bool) -> 'a t -> 'a optionfind_pred p l finds the first element of l that satisfies p, or returns None if no element satisfies p
val find_pred_exn : ('a -> bool) -> 'a t -> 'aUnsafe version of find_pred
if no such element is found
val length : 'a t -> intHow long is the iterator? Forces the iterator.
val is_empty : 'a t -> boolIs the iterator empty? Forces the iterator.
val append : 'a t -> 'a t -> 'a tAppend two iterators. Iterating on the result is like iterating on the first, then on the second.
val append_l : 'a t list -> 'a tAppend iterators. Iterating on the result is like iterating on the each iterator of the list in order.
val flat_map : ('a -> 'b t) -> 'a t -> 'b tMonadic bind. Intuitively, it applies the function to every element of the initial iterator, and calls concat. Formerly flatMap
val flat_map_l : ('a -> 'b list) -> 'a t -> 'b tConvenience function combining flat_map and of_list
val seq_list : 'a t list -> 'a list tseq_list l returns all the ways to pick one element in each sub-iterator in l. Assumes the sub-iterators can be iterated on several times.
val seq_list_map : ('a -> 'b t) -> 'a list -> 'b list tseq_list_map f l maps f over every element of l, then calls seq_list
val filter_map : ('a -> 'b option) -> 'a t -> 'b tMap and only keep non-None elements Formerly fmap
val filter_mapi : (int -> 'a -> 'b option) -> 'a t -> 'b tMap with indices, and only keep non-None elements
val filter_count : ('a -> bool) -> 'a t -> intCount how many elements satisfy the given predicate
val keep_some : 'a option t -> 'a tfilter_some l retains only elements of the form Some x. Same as filter_map (fun x->x)
val persistent : 'a t -> 'a tIterate on the iterator, storing elements in an efficient internal structure.. The resulting iterator can be iterated on as many times as needed. Note: calling persistent on an already persistent iterator will still make a new copy of the iterator!
val persistent_lazy : 'a t -> 'a tLazy version of persistent. When calling persistent_lazy s, a new iterator 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.
val sort : ?cmp:('a -> 'a -> int) -> 'a t -> 'a tSort the iterator. Eager, O(n) ram and O(n ln(n)) time. It iterates on elements of the argument iterator immediately, before it sorts them.
val sort_uniq : ?cmp:('a -> 'a -> int) -> 'a t -> 'a tSort the iterator and remove duplicates. Eager, same as sort
val sorted : ?cmp:('a -> 'a -> int) -> 'a t -> boolChecks whether the iterator is sorted. Eager, same as sort.
val group_succ_by : ?eq:('a -> 'a -> bool) -> 'a t -> 'a list tGroup equal consecutive elements. Linear time. Formerly synonym to group.
val group_by : ?hash:('a -> int) -> ?eq:('a -> 'a -> bool) -> 'a t -> 'a list tGroup equal elements, disregarding their order of appearance. The result iterator is traversable as many times as required. precondition: for any x and y, if eq x y then hash x=hash y must hold.
val count : ?hash:('a -> int) -> ?eq:('a -> 'a -> bool) -> 'a t -> ('a * int) tMap each distinct element to its number of occurrences in the whole seq. Similar to group_by seq |> map (fun l->List.hd l, List.length l) precondition: for any x and y, if eq x y then hash x=hash y must hold.
val uniq : ?eq:('a -> 'a -> bool) -> 'a t -> 'a tRemove consecutive duplicate elements. Basically this is like fun seq -> map List.hd (group seq).
val product : 'a t -> 'b t -> ('a * 'b) tCartesian product of iterators. 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 diagonal_l : 'a list -> ('a * 'a) tAll pairs of distinct positions of the list. diagonal l will return the iterator of all List.nth i l, List.nth j l if i < j.
val diagonal : 'a t -> ('a * 'a) tAll pairs of distinct positions of the iterator. Iterates only once on the iterator, which must be finite.
val join : join_row:('a -> 'b -> 'c option) -> 'a t -> 'b t -> 'c tjoin ~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.
val join_by : ?eq:'key equal -> ?hash:'key hash -> ('a -> 'key) -> ('b -> 'key) -> merge:('key -> 'a -> 'b -> 'c option) -> 'a t -> 'b t -> 'c tjoin key1 key2 ~merge is a binary operation that takes two iterators a and b, projects their elements resp. with key1 and key2, and combine values (x,y) from (a,b) with the same key using merge. If merge returns None, the combination of values is discarded. precondition: for any x and y, if eq x y then hash x=hash y must hold.
val join_all_by : ?eq:'key equal -> ?hash:'key hash -> ('a -> 'key) -> ('b -> 'key) -> merge:('key -> 'a list -> 'b list -> 'c option) -> 'a t -> 'b t -> 'c tjoin_all_by key1 key2 ~merge is a binary operation that takes two iterators a and b, projects their elements resp. with key1 and key2, and, for each key k occurring in at least one of them:
l1 of elements of a that map to kl2 of elements of b that map to kmerge k l1 l2. If merge returns None, the combination of values is discarded, otherwise it returns Some c and c is inserted in the result.val group_join_by : ?eq:'a equal -> ?hash:'a hash -> ('b -> 'a) -> 'a t -> 'b t -> ('a * 'b list) tgroup_join_by key2 associates to every element x of the first iterator, all the elements y of the second iterator such that eq x (key y). Elements of the first iterators without corresponding values in the second one are mapped to [] precondition: for any x and y, if eq x y then hash x=hash y must hold.
val inter : ?eq:'a equal -> ?hash:'a hash -> 'a t -> 'a t -> 'a tIntersection of two collections. Each element will occur at most once in the result. Eager. precondition: for any x and y, if eq x y then hash x=hash y must hold.
val max : ?lt:('a -> 'a -> bool) -> 'a t -> 'a optionMax element of the iterator, using the given comparison function.
None if the iterator is empty, Some m where m is the maximal element otherwise
val max_exn : ?lt:('a -> 'a -> bool) -> 'a t -> 'aUnsafe version of max
if the iterator is empty
val min : ?lt:('a -> 'a -> bool) -> 'a t -> 'a optionMin element of the iterator, using the given comparison function. see max for more details.
val min_exn : ?lt:('a -> 'a -> bool) -> 'a t -> 'aUnsafe version of min
if the iterator is empty
val sum : int t -> intSum of elements
val sumf : float t -> floatSum of elements, using Kahan summation
val head : 'a t -> 'a optionFirst element, if any, otherwise None
val head_exn : 'a t -> 'aFirst element, if any, fails
if the iterator is empty
val take : int -> 'a t -> 'a tTake at most n elements from the iterator. Works on infinite iterators.
val take_while : ('a -> bool) -> 'a t -> 'a tTake elements while they satisfy the predicate, then stops iterating. Will work on an infinite iterator s if the predicate is false for at least one element of s.
val fold_while : ('a -> 'b -> 'a * [ `Stop | `Continue ]) -> 'a -> 'b t -> 'aFolds over elements of the iterator, stopping early if the accumulator returns ('a, `Stop)
val to_list : 'a t -> 'a listConvert the iterator 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.
val to_rev_list : 'a t -> 'a listGet the list of the reversed iterator (more efficient than to_list)
val of_list : 'a list -> 'a tval on_list : ('a t -> 'b t) -> 'a list -> 'b liston_list f l is equivalent to to_list @@ f @@ of_list l.
val pair_with_idx : 'a t -> (int * 'a) tSimilar to zip_i but returns a normal iterator of tuples
val to_array : 'a t -> 'a arrayConvert to an array. Currently not very efficient because an intermediate list is used.
val of_array : 'a array -> 'a tval of_array_i : 'a array -> (int * 'a) tElements of the array, with their index
val array_slice : 'a array -> int -> int -> 'a tarray_slice a i j Iterator of elements whose indexes range from i to j
val of_opt : 'a option -> 'a tIterate on 0 or 1 values.
val of_stream : 'a Stream.t -> 'a tIterator of elements of a stream (usable only once)
val to_stream : 'a t -> 'a Stream.tConvert to a stream. linear in memory and time (a copy is made in memory)
val to_stack : 'a Stack.t -> 'a t -> unitPush elements of the iterator on the stack
val of_stack : 'a Stack.t -> 'a tIterator of elements of the stack (same order as Stack.iter)
val to_queue : 'a Queue.t -> 'a t -> unitPush elements of the iterator into the queue
val of_queue : 'a Queue.t -> 'a tIterator of elements contained in the queue, FIFO order
val hashtbl_add : ('a, 'b) Hashtbl.t -> ('a * 'b) t -> unitAdd elements of the iterator to the hashtable, with Hashtbl.add
val hashtbl_replace : ('a, 'b) Hashtbl.t -> ('a * 'b) t -> unitAdd elements of the iterator to the hashtable, with Hashtbl.replace (erases conflicting bindings)
val to_hashtbl : ('a * 'b) t -> ('a, 'b) Hashtbl.tBuild a hashtable from an iterator of key/value pairs
val of_hashtbl : ('a, 'b) Hashtbl.t -> ('a * 'b) tIterator of key/value pairs from the hashtable
val hashtbl_keys : ('a, 'b) Hashtbl.t -> 'a tval hashtbl_values : ('a, 'b) Hashtbl.t -> 'b tval of_str : string -> char tval to_str : char t -> stringval concat_str : string t -> stringConcatenate strings together, eagerly. Also see intersperse to add a separator.
exception OneShotSequenceRaised when the user tries to iterate several times on a transient iterator
val of_in_channel : Pervasives.in_channel -> char tIterates on characters of the input (can block when one iterates over the iterator). If you need to iterate several times on this iterator, use persistent.
when used more than once.
val to_buffer : char t -> Buffer.t -> unitCopy content of the iterator into the buffer
val int_range : start:int -> stop:int -> int tIterator on integers in start...stop by steps 1. Also see (--) for an infix version.
val int_range_dec : start:int -> stop:int -> int tIterator on decreasing integers in stop...start by steps -1. See (--^) for an infix version
val int_range_by : step:int -> int -> int -> int tint_range_by ~step i j is the range starting at i, including j, where the difference between successive elements is step. use a negative step for a decreasing iterator.
if step=0
val bools : bool tIterates on true and false
val of_set : (module Set.S with type elt = 'a and type t = 'b) -> 'b -> 'a tConvert the given set to an iterator. The set module must be provided.
val to_set : (module Set.S with type elt = 'a and type t = 'b) -> 'a t -> 'bConvert the iterator to a set, given the proper set module
type 'a gen = unit -> 'a optiontype 'a klist = unit -> [ `Nil | `Cons of 'a * 'a klist ]module Set : sig ... endmodule Map : sig ... endval random_int : int -> int tInfinite iterator of random integers between 0 and the given higher bound (see Random.int)
val random_bool : bool tInfinite iterator of random bool values
val random_float : float -> float tval random_array : 'a array -> 'a tIterator of choices of an element in the array
val random_list : 'a list -> 'a tInfinite iterator of random elements of the list. Basically the same as random_array.
val shuffle : 'a t -> 'a tshuffle seq returns a perfect shuffle of seq. Uses O(length seq) memory and time. Eager.
val shuffle_buffer : int -> 'a t -> 'a tshuffle_buffer n seq returns an iterator of element of seq in random order. The shuffling is *not* uniform. Uses O(n) memory.
The first n elements of the iterator are consumed immediately. The rest is consumed lazily.
val sample : int -> 'a t -> 'a arraysample n seq returns k samples of seq, with uniform probability. It will consume the iterator and use O(n) memory.
It returns an array of size min (length seq) n.
module Infix : sig ... endinclude module type of Infixval (--) : int -> int -> int ta -- b is the range of integers from a to b, both included, in increasing order. It will therefore be empty if a > b.
val (--^) : int -> int -> int ta --^ b is the range of integers from b to a, both included, in decreasing order (starts from a). It will therefore be empty if a < b.
val pp_seq : ?sep:string -> (Format.formatter -> 'a -> unit) -> Format.formatter -> 'a t -> unitPretty print an iterator 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 -> 'a t -> unitPrint into a buffer
val to_string : ?sep:string -> ('a -> string) -> 'a t -> stringPrint into a string
Very basic interface to manipulate files as iterator of chunks/lines. The iterators take care of opening and closing files properly; every time one iterates over an iterator, the file is opened/closed again.
Example: copy a file "a" into file "b", removing blank lines:
Iterator.(IO.lines_of "a" |> filter (fun l-> l<> "") |> IO.write_lines "b");;By chunks of 4096 bytes:
Iterator.IO.(chunks_of ~size:4096 "a" |> write_to "b");;Read the lines of a file into a list:
Iterator.IO.lines "a" |> Iterator.to_listmodule IO : sig ... endIterThe 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 an iterator 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 iterators (e.g. group). The transformation is computed on the fly every time one iterates over the resulting iterator. 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 an iterator, one gets a new iterator, but nothing else happens until this new iterator is used (by folding or iterating on it).
If an iterator 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 an iterator that iterates on the elements of this memory structure, cheaply and repeatably.
type +'a t = ('a -> unit) -> unitAn iterator of values of type 'a. If you give it a function 'a -> unit it will be applied to every element of the iterator successively.
type +'a iter = 'a tNOTE Type ('a, 'b) t2 = ('a -> 'b -> unit) -> unit has been removed and subsumed by ('a * 'b) t
val from_iter : (('a -> unit) -> unit) -> 'a tBuild an iterator from a iter function
val from_labelled_iter : (f:('a -> unit) -> unit) -> 'a tBuild an iterator from a labelled iter function
val from_fun : (unit -> 'a option) -> 'a tCall the function repeatedly until it returns None. This iterator is transient, use persistent if needed!
val empty : 'a tEmpty iterator. It contains no element.
val singleton : 'a -> 'a tSingleton iterator, with exactly one element.
val doubleton : 'a -> 'a -> 'a tIterator with exactly two elements
val init : (int -> 'a) -> 'a tinit f is the infinite iterator f 0; f 1; f 2; ….
val repeat : 'a -> 'a tInfinite iterator of the same element. You may want to look at take and the likes if you iterate on it.
val iterate : ('a -> 'a) -> 'a -> 'a titerate f x is the infinite iterator x, f(x), f(f(x)), ...
val forever : (unit -> 'b) -> 'b tIterator that calls the given function to produce elements. The iterator may be transient (depending on the function), and definitely is infinite. You may want to use take and persistent.
val cycle : 'a t -> 'a tCycle forever through the given iterator. Assume the given iterator can be traversed any amount of times (not transient). This yields an infinite iterator, you should use something like take not to loop forever.
val unfoldr : ('b -> ('a * 'b) option) -> 'b -> 'a tunfoldr f b will apply f to b. If it yields Some (x,b') then x is returned and unfoldr recurses with b'.
val iter : ('a -> unit) -> 'a t -> unitConsume the iterator, passing all its arguments to the function. Basically iter f seq is just seq f.
val iteri : (int -> 'a -> unit) -> 'a t -> unitIterate on elements and their index in the iterator
val fold : ('a -> 'b -> 'a) -> 'a -> 'b t -> 'aFold over elements of the iterator, consuming it
val foldi : ('a -> int -> 'b -> 'a) -> 'a -> 'b t -> 'aFold over elements of the iterator and their index, consuming it
val fold_map : ('acc -> 'a -> 'acc * 'b) -> 'acc -> 'a t -> 'b tfold_map f acc l is like map, but it carries some state as in fold. The state is not returned, it is just used to thread some information to the map function.
val fold_filter_map : ('acc -> 'a -> 'acc * 'b option) -> 'acc -> 'a t -> 'b tfold_filter_map f acc l is a fold_map-like function, but the function can choose to skip an element by retuning None.
val map_by_2 : ('a -> 'a -> 'a) -> 'a t -> 'a tMap objects two by two. lazily. The last element is kept in the iterator if the count is odd.
val for_all : ('a -> bool) -> 'a t -> boolDo all elements satisfy the predicate?
val exists : ('a -> bool) -> 'a t -> boolExists there some element satisfying the predicate?
val mem : ?eq:('a -> 'a -> bool) -> 'a -> 'a t -> boolIs the value a member of the iterator?
the equality predicate to use (default (=))
val find : ('a -> 'b option) -> 'a t -> 'b optionFind the first element on which the function doesn't return None
val find_pred : ('a -> bool) -> 'a t -> 'a optionfind_pred p l finds the first element of l that satisfies p, or returns None if no element satisfies p
val find_pred_exn : ('a -> bool) -> 'a t -> 'aUnsafe version of find_pred
if no such element is found
val length : 'a t -> intHow long is the iterator? Forces the iterator.
val is_empty : 'a t -> boolIs the iterator empty? Forces the iterator.
val append : 'a t -> 'a t -> 'a tAppend two iterators. Iterating on the result is like iterating on the first, then on the second.
val append_l : 'a t list -> 'a tAppend iterators. Iterating on the result is like iterating on the each iterator of the list in order.
val flat_map : ('a -> 'b t) -> 'a t -> 'b tMonadic bind. Intuitively, it applies the function to every element of the initial iterator, and calls concat. Formerly flatMap
val flat_map_l : ('a -> 'b list) -> 'a t -> 'b tConvenience function combining flat_map and of_list
val seq_list : 'a t list -> 'a list tseq_list l returns all the ways to pick one element in each sub-iterator in l. Assumes the sub-iterators can be iterated on several times.
val seq_list_map : ('a -> 'b t) -> 'a list -> 'b list tseq_list_map f l maps f over every element of l, then calls seq_list
val filter_map : ('a -> 'b option) -> 'a t -> 'b tMap and only keep non-None elements Formerly fmap
val filter_mapi : (int -> 'a -> 'b option) -> 'a t -> 'b tMap with indices, and only keep non-None elements
val filter_count : ('a -> bool) -> 'a t -> intCount how many elements satisfy the given predicate
val keep_some : 'a option t -> 'a tfilter_some l retains only elements of the form Some x. Same as filter_map (fun x->x)
val persistent : 'a t -> 'a tIterate on the iterator, storing elements in an efficient internal structure.. The resulting iterator can be iterated on as many times as needed. Note: calling persistent on an already persistent iterator will still make a new copy of the iterator!
val persistent_lazy : 'a t -> 'a tLazy version of persistent. When calling persistent_lazy s, a new iterator 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.
val sort : ?cmp:('a -> 'a -> int) -> 'a t -> 'a tSort the iterator. Eager, O(n) ram and O(n ln(n)) time. It iterates on elements of the argument iterator immediately, before it sorts them.
val sort_uniq : ?cmp:('a -> 'a -> int) -> 'a t -> 'a tSort the iterator and remove duplicates. Eager, same as sort
val sorted : ?cmp:('a -> 'a -> int) -> 'a t -> boolChecks whether the iterator is sorted. Eager, same as sort.
val group_succ_by : ?eq:('a -> 'a -> bool) -> 'a t -> 'a list tGroup equal consecutive elements. Linear time. Formerly synonym to group.
val group_by : ?hash:('a -> int) -> ?eq:('a -> 'a -> bool) -> 'a t -> 'a list tGroup equal elements, disregarding their order of appearance. The result iterator is traversable as many times as required. precondition: for any x and y, if eq x y then hash x=hash y must hold.
val count : ?hash:('a -> int) -> ?eq:('a -> 'a -> bool) -> 'a t -> ('a * int) tMap each distinct element to its number of occurrences in the whole seq. Similar to group_by seq |> map (fun l->List.hd l, List.length l) precondition: for any x and y, if eq x y then hash x=hash y must hold.
val uniq : ?eq:('a -> 'a -> bool) -> 'a t -> 'a tRemove consecutive duplicate elements. Basically this is like fun seq -> map List.hd (group seq).
val product : 'a t -> 'b t -> ('a * 'b) tCartesian product of iterators. 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 diagonal_l : 'a list -> ('a * 'a) tAll pairs of distinct positions of the list. diagonal l will return the iterator of all List.nth i l, List.nth j l if i < j.
val diagonal : 'a t -> ('a * 'a) tAll pairs of distinct positions of the iterator. Iterates only once on the iterator, which must be finite.
val join : join_row:('a -> 'b -> 'c option) -> 'a t -> 'b t -> 'c tjoin ~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.
val join_by : ?eq:'key equal -> ?hash:'key hash -> ('a -> 'key) -> ('b -> 'key) -> merge:('key -> 'a -> 'b -> 'c option) -> 'a t -> 'b t -> 'c tjoin key1 key2 ~merge is a binary operation that takes two iterators a and b, projects their elements resp. with key1 and key2, and combine values (x,y) from (a,b) with the same key using merge. If merge returns None, the combination of values is discarded. precondition: for any x and y, if eq x y then hash x=hash y must hold.
val join_all_by : ?eq:'key equal -> ?hash:'key hash -> ('a -> 'key) -> ('b -> 'key) -> merge:('key -> 'a list -> 'b list -> 'c option) -> 'a t -> 'b t -> 'c tjoin_all_by key1 key2 ~merge is a binary operation that takes two iterators a and b, projects their elements resp. with key1 and key2, and, for each key k occurring in at least one of them:
l1 of elements of a that map to kl2 of elements of b that map to kmerge k l1 l2. If merge returns None, the combination of values is discarded, otherwise it returns Some c and c is inserted in the result.val group_join_by : ?eq:'a equal -> ?hash:'a hash -> ('b -> 'a) -> 'a t -> 'b t -> ('a * 'b list) tgroup_join_by key2 associates to every element x of the first iterator, all the elements y of the second iterator such that eq x (key y). Elements of the first iterators without corresponding values in the second one are mapped to [] precondition: for any x and y, if eq x y then hash x=hash y must hold.
val inter : ?eq:'a equal -> ?hash:'a hash -> 'a t -> 'a t -> 'a tIntersection of two collections. Each element will occur at most once in the result. Eager. precondition: for any x and y, if eq x y then hash x=hash y must hold.
val max : ?lt:('a -> 'a -> bool) -> 'a t -> 'a optionMax element of the iterator, using the given comparison function.
None if the iterator is empty, Some m where m is the maximal element otherwise
val max_exn : ?lt:('a -> 'a -> bool) -> 'a t -> 'aUnsafe version of max
if the iterator is empty
val min : ?lt:('a -> 'a -> bool) -> 'a t -> 'a optionMin element of the iterator, using the given comparison function. see max for more details.
val min_exn : ?lt:('a -> 'a -> bool) -> 'a t -> 'aUnsafe version of min
if the iterator is empty
val sum : int t -> intSum of elements
val sumf : float t -> floatSum of elements, using Kahan summation
val head : 'a t -> 'a optionFirst element, if any, otherwise None
val head_exn : 'a t -> 'aFirst element, if any, fails
if the iterator is empty
val take : int -> 'a t -> 'a tTake at most n elements from the iterator. Works on infinite iterators.
val take_while : ('a -> bool) -> 'a t -> 'a tTake elements while they satisfy the predicate, then stops iterating. Will work on an infinite iterator s if the predicate is false for at least one element of s.
val fold_while : ('a -> 'b -> 'a * [ `Stop | `Continue ]) -> 'a -> 'b t -> 'aFolds over elements of the iterator, stopping early if the accumulator returns ('a, `Stop)
val to_list : 'a t -> 'a listConvert the iterator 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.
val to_rev_list : 'a t -> 'a listGet the list of the reversed iterator (more efficient than to_list)
val of_list : 'a list -> 'a tval on_list : ('a t -> 'b t) -> 'a list -> 'b liston_list f l is equivalent to to_list @@ f @@ of_list l.
val pair_with_idx : 'a t -> (int * 'a) tSimilar to zip_i but returns a normal iterator of tuples
val to_array : 'a t -> 'a arrayConvert to an array. Currently not very efficient because an intermediate list is used.
val of_array : 'a array -> 'a tval of_array_i : 'a array -> (int * 'a) tElements of the array, with their index
val array_slice : 'a array -> int -> int -> 'a tarray_slice a i j Iterator of elements whose indexes range from i to j
val of_opt : 'a option -> 'a tIterate on 0 or 1 values.
val of_stream : 'a Stream.t -> 'a tIterator of elements of a stream (usable only once)
val to_stream : 'a t -> 'a Stream.tConvert to a stream. linear in memory and time (a copy is made in memory)
val to_stack : 'a Stack.t -> 'a t -> unitPush elements of the iterator on the stack
val of_stack : 'a Stack.t -> 'a tIterator of elements of the stack (same order as Stack.iter)
val to_queue : 'a Queue.t -> 'a t -> unitPush elements of the iterator into the queue
val of_queue : 'a Queue.t -> 'a tIterator of elements contained in the queue, FIFO order
val hashtbl_add : ('a, 'b) Hashtbl.t -> ('a * 'b) t -> unitAdd elements of the iterator to the hashtable, with Hashtbl.add
val hashtbl_replace : ('a, 'b) Hashtbl.t -> ('a * 'b) t -> unitAdd elements of the iterator to the hashtable, with Hashtbl.replace (erases conflicting bindings)
val to_hashtbl : ('a * 'b) t -> ('a, 'b) Hashtbl.tBuild a hashtable from an iterator of key/value pairs
val of_hashtbl : ('a, 'b) Hashtbl.t -> ('a * 'b) tIterator of key/value pairs from the hashtable
val hashtbl_keys : ('a, 'b) Hashtbl.t -> 'a tval hashtbl_values : ('a, 'b) Hashtbl.t -> 'b tval of_str : string -> char tval to_str : char t -> stringval concat_str : string t -> stringConcatenate strings together, eagerly. Also see intersperse to add a separator.
exception OneShotSequenceRaised when the user tries to iterate several times on a transient iterator
val of_in_channel : Pervasives.in_channel -> char tIterates on characters of the input (can block when one iterates over the iterator). If you need to iterate several times on this iterator, use persistent.
when used more than once.
val to_buffer : char t -> Buffer.t -> unitCopy content of the iterator into the buffer
val int_range : start:int -> stop:int -> int tIterator on integers in start...stop by steps 1. Also see (--) for an infix version.
val int_range_dec : start:int -> stop:int -> int tIterator on decreasing integers in stop...start by steps -1. See (--^) for an infix version
val int_range_by : step:int -> int -> int -> int tint_range_by ~step i j is the range starting at i, including j, where the difference between successive elements is step. use a negative step for a decreasing iterator.
if step=0
val bools : bool tIterates on true and false
val of_set : (module Set.S with type elt = 'a and type t = 'b) -> 'b -> 'a tConvert the given set to an iterator. The set module must be provided.
val to_set : (module Set.S with type elt = 'a and type t = 'b) -> 'a t -> 'bConvert the iterator to a set, given the proper set module
type 'a gen = unit -> 'a optiontype 'a klist = unit -> [ `Nil | `Cons of 'a * 'a klist ]module Set : sig ... endmodule Map : sig ... endval random_int : int -> int tInfinite iterator of random integers between 0 and the given higher bound (see Random.int)
val random_bool : bool tInfinite iterator of random bool values
val random_float : float -> float tval random_array : 'a array -> 'a tIterator of choices of an element in the array
val random_list : 'a list -> 'a tInfinite iterator of random elements of the list. Basically the same as random_array.
val shuffle : 'a t -> 'a tshuffle seq returns a perfect shuffle of seq. Uses O(length seq) memory and time. Eager.
val shuffle_buffer : int -> 'a t -> 'a tshuffle_buffer n seq returns an iterator of element of seq in random order. The shuffling is *not* uniform. Uses O(n) memory.
The first n elements of the iterator are consumed immediately. The rest is consumed lazily.
val sample : int -> 'a t -> 'a arraysample n seq returns k samples of seq, with uniform probability. It will consume the iterator and use O(n) memory.
It returns an array of size min (length seq) n.
module Infix : sig ... endinclude module type of Infixval (--) : int -> int -> int ta -- b is the range of integers from a to b, both included, in increasing order. It will therefore be empty if a > b.
val (--^) : int -> int -> int ta --^ b is the range of integers from b to a, both included, in decreasing order (starts from a). It will therefore be empty if a < b.
val pp_seq : ?sep:string -> (Format.formatter -> 'a -> unit) -> Format.formatter -> 'a t -> unitPretty print an iterator 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 -> 'a t -> unitPrint into a buffer
val to_string : ?sep:string -> ('a -> string) -> 'a t -> stringPrint into a string
Very basic interface to manipulate files as iterator of chunks/lines. The iterators take care of opening and closing files properly; every time one iterates over an iterator, the file is opened/closed again.
Example: copy a file "a" into file "b", removing blank lines:
Iterator.(IO.lines_of "a" |> filter (fun l-> l<> "") |> IO.write_lines "b");;By chunks of 4096 bytes:
Iterator.IO.(chunks_of ~size:4096 "a" |> write_to "b");;Read the lines of a file into a list:
Iterator.IO.lines "a" |> Iterator.to_listmodule IO : sig ... endIterLabelsVersion of Iterator with labels
type +'a t = ('a -> unit) -> unitAn iterator of values of type 'a. If you give it a function 'a -> unit it will be applied to every element of the iterator successively.
type +'a iter = 'a tNOTE Type ('a, 'b) t2 = ('a -> 'b -> unit) -> unit has been removed and subsumed by ('a * 'b) t
val from_iter : (('a -> unit) -> unit) -> 'a tBuild an iterator from a iter function
val from_labelled_iter : (f:('a -> unit) -> unit) -> 'a tBuild an iterator from a labelled iter function
val from_fun : (unit -> 'a option) -> 'a tCall the function repeatedly until it returns None. This iterator is transient, use persistent if needed!
val empty : 'a tEmpty iterator. It contains no element.
val singleton : 'a -> 'a tSingleton iterator, with exactly one element.
val doubleton : 'a -> 'a -> 'a tIterator with exactly two elements
val init : f:(int -> 'a) -> 'a tinit f is the infinite iterator f 0; f 1; f 2; ….
val repeat : 'a -> 'a tInfinite iterator of the same element. You may want to look at take and the likes if you iterate on it.
val iterate : ('a -> 'a) -> 'a -> 'a titerate f x is the infinite iterator x, f(x), f(f(x)), ...
val forever : (unit -> 'b) -> 'b tIterator that calls the given function to produce elements. The iterator may be transient (depending on the function), and definitely is infinite. You may want to use take and persistent.
val iter : f:('a -> unit) -> 'a t -> unitConsume the iterator, passing all its arguments to the function. Basically iter f seq is just seq f.
val iteri : f:(int -> 'a -> unit) -> 'a t -> unitIterate on elements and their index in the iterator
val fold : f:('a -> 'b -> 'a) -> init:'a -> 'b t -> 'aFold over elements of the iterator, consuming it
val foldi : f:('a -> int -> 'b -> 'a) -> init:'a -> 'b t -> 'aFold over elements of the iterator and their index, consuming it
val fold_map : f:('acc -> 'a -> 'acc * 'b) -> init:'acc -> 'a t -> 'b tfold_map f acc l is like map, but it carries some state as in fold. The state is not returned, it is just used to thread some information to the map function.
val fold_filter_map : f:('acc -> 'a -> 'acc * 'b option) -> init:'acc -> 'a t -> 'b tfold_filter_map f acc l is a fold_map-like function, but the function can choose to skip an element by retuning None.
val map_by_2 : f:('a -> 'a -> 'a) -> 'a t -> 'a tMap objects two by two. lazily. The last element is kept in the iterator if the count is odd.
val for_all : f:('a -> bool) -> 'a t -> boolDo all elements satisfy the predicate?
val exists : f:('a -> bool) -> 'a t -> boolExists there some element satisfying the predicate?
val mem : ?eq:('a -> 'a -> bool) -> x:'a -> 'a t -> boolIs the value a member of the iterator?
the equality predicate to use (default (=))
val find : ('a -> 'b option) -> 'a t -> 'b optionFind the first element on which the function doesn't return None
val find_pred : f:('a -> bool) -> 'a t -> 'a optionfind_pred p l finds the first element of l that satisfies p, or returns None if no element satisfies p
val find_pred_exn : f:('a -> bool) -> 'a t -> 'aUnsafe version of find_pred
if no such element is found
val length : 'a t -> intHow long is the iterator? Forces the iterator.
val is_empty : 'a t -> boolIs the iterator empty? Forces the iterator.
val append : 'a t -> 'a t -> 'a tAppend two iterators. Iterating on the result is like iterating on the first, then on the second.
val append_l : 'a t list -> 'a tAppend iterators. Iterating on the result is like iterating on the each iterator of the list in order.
val flat_map_l : f:('a -> 'b list) -> 'a t -> 'b tConvenience function combining flat_map and of_list
val seq_list : 'a t list -> 'a list tseq_list l returns all the ways to pick one element in each sub-iterator in l. Assumes the sub-iterators can be iterated on several times.
val seq_list_map : f:('a -> 'b t) -> 'a list -> 'b list tseq_list_map f l maps f over every element of l, then calls seq_list
val filter_map : f:('a -> 'b option) -> 'a t -> 'b tMap and only keep non-None elements Formerly fmap
val filter_mapi : f:(int -> 'a -> 'b option) -> 'a t -> 'b tMap with indices, and only keep non-None elements
val filter_count : f:('a -> bool) -> 'a t -> intCount how many elements satisfy the given predicate
val intersperse : x:'a -> 'a t -> 'a tInsert the single element between every element of the iterator
val keep_some : 'a option t -> 'a tfilter_some l retains only elements of the form Some x. Same as filter_map (fun x->x)
val persistent : 'a t -> 'a tIterate on the iterator, storing elements in an efficient internal structure.. The resulting iterator can be iterated on as many times as needed. Note: calling persistent on an already persistent iterator will still make a new copy of the iterator!
val persistent_lazy : 'a t -> 'a tLazy version of persistent. When calling persistent_lazy s, a new iterator 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.
val sort : ?cmp:('a -> 'a -> int) -> 'a t -> 'a tSort the iterator. Eager, O(n) ram and O(n ln(n)) time. It iterates on elements of the argument iterator immediately, before it sorts them.
val sort_uniq : ?cmp:('a -> 'a -> int) -> 'a t -> 'a tSort the iterator and remove duplicates. Eager, same as sort
val sorted : ?cmp:('a -> 'a -> int) -> 'a t -> boolChecks whether the iterator is sorted. Eager, same as sort.
val group_succ_by : ?eq:('a -> 'a -> bool) -> 'a t -> 'a list tGroup equal consecutive elements. Formerly synonym to group.
val group_by : ?hash:('a -> int) -> ?eq:('a -> 'a -> bool) -> 'a t -> 'a list tGroup equal elements, disregarding their order of appearance. The result iterator is traversable as many times as required. precondition: for any x and y, if eq x y then hash x=hash y must hold.
val count : ?hash:('a -> int) -> ?eq:('a -> 'a -> bool) -> 'a t -> ('a * int) tMap each distinct element to its number of occurrences in the whole seq. Similar to group_by seq |> map (fun l->List.hd l, List.length l)
val uniq : ?eq:('a -> 'a -> bool) -> 'a t -> 'a tRemove consecutive duplicate elements. Basically this is like fun seq -> map List.hd (group seq).
val product : 'a t -> 'b t -> ('a * 'b) tCartesian product of the iterators. 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 diagonal_l : 'a list -> ('a * 'a) tAll pairs of distinct positions of the list. diagonal l will return the iterator of all List.nth i l, List.nth j l if i < j.
val diagonal : 'a t -> ('a * 'a) tAll pairs of distinct positions of the iterator. Iterates only once on the iterator, which must be finite.
val join : join_row:('a -> 'b -> 'c option) -> 'a t -> 'b t -> 'c tjoin ~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.
val join_by : ?eq:'key equal -> ?hash:'key hash -> ('a -> 'key) -> ('b -> 'key) -> merge:('key -> 'a -> 'b -> 'c option) -> 'a t -> 'b t -> 'c tjoin key1 key2 ~merge is a binary operation that takes two iterators a and b, projects their elements resp. with key1 and key2, and combine values (x,y) from (a,b) with the same key using merge. If merge returns None, the combination of values is discarded. precondition: for any x and y, if eq x y then hash x=hash y must hold.
val join_all_by : ?eq:'key equal -> ?hash:'key hash -> ('a -> 'key) -> ('b -> 'key) -> merge:('key -> 'a list -> 'b list -> 'c option) -> 'a t -> 'b t -> 'c tjoin_all_by key1 key2 ~merge is a binary operation that takes two iterators a and b, projects their elements resp. with key1 and key2, and, for each key k occurring in at least one of them:
l1 of elements of a that map to kl2 of elements of b that map to kmerge k l1 l2. If merge returns None, the combination of values is discarded, otherwise it returns Some c and c is inserted in the result.val group_join_by : ?eq:'a equal -> ?hash:'a hash -> ('b -> 'a) -> 'a t -> 'b t -> ('a * 'b list) tgroup_join_by key2 associates to every element x of the first iterator, all the elements y of the second iterator such that eq x (key y). Elements of the first iterators without corresponding values in the second one are mapped to [] precondition: for any x and y, if eq x y then hash x=hash y must hold.
val inter : ?eq:'a equal -> ?hash:'a hash -> 'a t -> 'a t -> 'a tIntersection of two collections. Each element will occur at most once in the result. Eager. precondition: for any x and y, if eq x y then hash x=hash y must hold.
val union : ?eq:'a equal -> ?hash:'a hash -> 'a t -> 'a t -> 'a tUnion of two collections. Each element will occur at most once in the result. Eager. precondition: for any x and y, if eq x y then hash x=hash y must hold.
val subset : ?eq:'a equal -> ?hash:'a hash -> 'a t -> 'a t -> boolsubset a b returns true if all elements of a belong to b. Eager. precondition: for any x and y, if eq x y then hash x=hash y must hold.
val unfoldr : ('b -> ('a * 'b) option) -> 'b -> 'a tunfoldr f b will apply f to b. If it yields Some (x,b') then x is returned and unfoldr recurses with b'.
val max : ?lt:('a -> 'a -> bool) -> 'a t -> 'a optionMax element of the iterator, using the given comparison function.
None if the iterator is empty, Some m where m is the maximal element otherwise
val max_exn : ?lt:('a -> 'a -> bool) -> 'a t -> 'aUnsafe version of max
if the iterator is empty
val min : ?lt:('a -> 'a -> bool) -> 'a t -> 'a optionMin element of the iterator, using the given comparison function. see max for more details.
val min_exn : ?lt:('a -> 'a -> bool) -> 'a t -> 'aUnsafe version of min
if the iterator is empty
val sum : int t -> intSum of elements
val sumf : float t -> floatSum of elements, using Kahan summation
val head : 'a t -> 'a optionFirst element, if any, otherwise None
val head_exn : 'a t -> 'aFirst element, if any, fails
if the iterator is empty
val take : int -> 'a t -> 'a tTake at most n elements from the iterator. Works on infinite iterators.
val take_while : f:('a -> bool) -> 'a t -> 'a tTake elements while they satisfy the predicate, then stops iterating. Will work on an infinite iterator s if the predicate is false for at least one element of s.
val fold_while : f:('a -> 'b -> 'a * [ `Stop | `Continue ]) -> init:'a -> 'b t -> 'aFolds over elements of the iterator, stopping early if the accumulator returns ('a, `Stop)
val rev : 'a t -> 'a tReverse the iterator. O(n) memory and time, needs the iterator to be finite. The result is persistent and does not depend on the input being repeatable.
val zip_i : 'a t -> (int * 'a) tZip elements of the iterator with their index in the iterator.
val fold2 : f:('c -> 'a -> 'b -> 'c) -> init:'c -> ('a * 'b) t -> 'cval iter2 : f:('a -> 'b -> unit) -> ('a * 'b) t -> unitval map2 : f:('a -> 'b -> 'c) -> ('a * 'b) t -> 'c tval map2_2 : f:('a -> 'b -> 'c) -> ('a -> 'b -> 'd) -> ('a * 'b) t -> ('c * 'd) tmap2_2 f g seq2 maps each x, y of seq2 into f x y, g x y
val to_list : 'a t -> 'a listConvert the iterator 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.
val to_rev_list : 'a t -> 'a listGet the list of the reversed iterator (more efficient than to_list)
val of_list : 'a list -> 'a tval on_list : ('a t -> 'b t) -> 'a list -> 'b liston_list f l is equivalent to to_list @@ f @@ of_list l.
val pair_with_idx : 'a t -> (int * 'a) tSimilar to zip_i but returns a normal iterator of tuples
val to_array : 'a t -> 'a arrayConvert to an array. Currently not very efficient because an intermediate list is used.
val of_array : 'a array -> 'a tval of_array_i : 'a array -> (int * 'a) tElements of the array, with their index
val array_slice : 'a array -> int -> int -> 'a tarray_slice a i j Iterator of elements whose indexes range from i to j
val of_opt : 'a option -> 'a tIterate on 0 or 1 values.
val of_stream : 'a Stream.t -> 'a tIterator of elements of a stream (usable only once)
val to_stream : 'a t -> 'a Stream.tConvert to a stream. linear in memory and time (a copy is made in memory)
val to_stack : 'a Stack.t -> 'a t -> unitPush elements of the iterator on the stack
val of_stack : 'a Stack.t -> 'a tIterator of elements of the stack (same order as Stack.iter)
val to_queue : 'a Queue.t -> 'a t -> unitPush elements of the iterator into the queue
val of_queue : 'a Queue.t -> 'a tIterator of elements contained in the queue, FIFO order
val hashtbl_add : ('a, 'b) Hashtbl.t -> ('a * 'b) t -> unitAdd elements of the iterator to the hashtable, with Hashtbl.add
val hashtbl_replace : ('a, 'b) Hashtbl.t -> ('a * 'b) t -> unitAdd elements of the iterator to the hashtable, with Hashtbl.replace (erases conflicting bindings)
val to_hashtbl : ('a * 'b) t -> ('a, 'b) Hashtbl.tBuild a hashtable from an iterator of key/value pairs
val of_hashtbl : ('a, 'b) Hashtbl.t -> ('a * 'b) tIterator of key/value pairs from the hashtable
val hashtbl_keys : ('a, 'b) Hashtbl.t -> 'a tval hashtbl_values : ('a, 'b) Hashtbl.t -> 'b tval of_str : string -> char tval to_str : char t -> stringval concat_str : string t -> stringConcatenate strings together, eagerly. Also see intersperse to add a separator.
exception OneShotSequenceRaised when the user tries to iterate several times on a transient iterator
val of_in_channel : Pervasives.in_channel -> char tIterates on characters of the input (can block when one iterates over the iterator). If you need to iterate several times on this iterator, use persistent.
when used more than once.
val to_buffer : char t -> Buffer.t -> unitCopy content of the iterator into the buffer
val int_range : start:int -> stop:int -> int tIterator on integers in start...stop by steps 1. Also see (--) for an infix version.
val int_range_dec : start:int -> stop:int -> int tIterator on decreasing integers in stop...start by steps -1. See (--^) for an infix version
val int_range_by : step:int -> start:int -> stop:int -> int tint_range_by ~step ~start:i ~stop:j is the range starting at i, including j, where the difference between successive elements is step. use a negative step for a decreasing iterator.
if step=0
val bools : bool tIterates on true and false
val of_set : (module Set.S with type elt = 'a and type t = 'b) -> 'b -> 'a tConvert the given set to an iterator. The set module must be provided.
val to_set : (module Set.S with type elt = 'a and type t = 'b) -> 'a t -> 'bConvert the iterator to a set, given the proper set module
type 'a gen = unit -> 'a optiontype 'a klist = unit -> [ `Nil | `Cons of 'a * 'a klist ]module Set : sig ... endmodule Map : sig ... endval random_int : int -> int tInfinite iterator of random integers between 0 and the given higher bound (see Random.int)
val random_bool : bool tInfinite iterator of random bool values
val random_float : float -> float tval random_array : 'a array -> 'a tIterator of choices of an element in the array
val random_list : 'a list -> 'a tInfinite iterator of random elements of the list. Basically the same as random_array.
val shuffle : 'a t -> 'a tshuffle seq returns a perfect shuffle of seq. Uses O(length seq) memory and time. Eager.
val shuffle_buffer : n:int -> 'a t -> 'a tshuffle_buffer n seq returns an iterator of element of seq in random order. The shuffling is not uniform. Uses O(n) memory.
The first n elements of the iterator are consumed immediately. The rest is consumed lazily.
val sample : n:int -> 'a t -> 'a arraysample n seq returns k samples of seq, with uniform probability. It will consume the iterator and use O(n) memory.
It returns an array of size min (length seq) n.
module Infix : sig ... endinclude module type of Infixval (--) : int -> int -> int ta -- b is the range of integers from a to b, both included, in increasing order. It will therefore be empty if a > b.
val (--^) : int -> int -> int ta --^ b is the range of integers from b to a, both included, in decreasing order (starts from a). It will therefore be empty if a < b.
val pp_seq : ?sep:string -> (Format.formatter -> 'a -> unit) -> Format.formatter -> 'a t -> unitPretty print an iterator 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 -> 'a t -> unitPrint into a buffer
val to_string : ?sep:string -> ('a -> string) -> 'a t -> stringPrint into a string
Very basic interface to manipulate files as iterator of chunks/lines. The iterators take care of opening and closing files properly; every time one iterates over an iterator, the file is opened/closed again.
Example: copy a file "a" into file "b", removing blank lines:
Iterator.(IO.lines_of "a" |> filter (fun l-> l<> "") |> IO.write_lines "b");;By chunks of 4096 bytes:
Iterator.IO.(chunks_of ~size:4096 "a" |> write_to "b");;Read the lines of a file into a list:
Iterator.IO.lines "a" |> Iterator.to_listmodule IO : sig ... endIterLabelsVersion of Iterator with labels
type +'a t = ('a -> unit) -> unitAn iterator of values of type 'a. If you give it a function 'a -> unit it will be applied to every element of the iterator successively.
type +'a iter = 'a tNOTE Type ('a, 'b) t2 = ('a -> 'b -> unit) -> unit has been removed and subsumed by ('a * 'b) t
val from_iter : (('a -> unit) -> unit) -> 'a tBuild an iterator from a iter function
val from_labelled_iter : (f:('a -> unit) -> unit) -> 'a tBuild an iterator from a labelled iter function
val from_fun : (unit -> 'a option) -> 'a tCall the function repeatedly until it returns None. This iterator is transient, use persistent if needed!
val empty : 'a tEmpty iterator. It contains no element.
val singleton : 'a -> 'a tSingleton iterator, with exactly one element.
val doubleton : 'a -> 'a -> 'a tIterator with exactly two elements
val init : f:(int -> 'a) -> 'a tinit f is the infinite iterator f 0; f 1; f 2; ….
val repeat : 'a -> 'a tInfinite iterator of the same element. You may want to look at take and the likes if you iterate on it.
val iterate : ('a -> 'a) -> 'a -> 'a titerate f x is the infinite iterator x, f(x), f(f(x)), ...
val forever : (unit -> 'b) -> 'b tIterator that calls the given function to produce elements. The iterator may be transient (depending on the function), and definitely is infinite. You may want to use take and persistent.
val iter : f:('a -> unit) -> 'a t -> unitConsume the iterator, passing all its arguments to the function. Basically iter f seq is just seq f.
val iteri : f:(int -> 'a -> unit) -> 'a t -> unitIterate on elements and their index in the iterator
val fold : f:('a -> 'b -> 'a) -> init:'a -> 'b t -> 'aFold over elements of the iterator, consuming it
val foldi : f:('a -> int -> 'b -> 'a) -> init:'a -> 'b t -> 'aFold over elements of the iterator and their index, consuming it
val fold_map : f:('acc -> 'a -> 'acc * 'b) -> init:'acc -> 'a t -> 'b tfold_map f acc l is like map, but it carries some state as in fold. The state is not returned, it is just used to thread some information to the map function.
val fold_filter_map : f:('acc -> 'a -> 'acc * 'b option) -> init:'acc -> 'a t -> 'b tfold_filter_map f acc l is a fold_map-like function, but the function can choose to skip an element by retuning None.
val map_by_2 : f:('a -> 'a -> 'a) -> 'a t -> 'a tMap objects two by two. lazily. The last element is kept in the iterator if the count is odd.
val for_all : f:('a -> bool) -> 'a t -> boolDo all elements satisfy the predicate?
val exists : f:('a -> bool) -> 'a t -> boolExists there some element satisfying the predicate?
val mem : ?eq:('a -> 'a -> bool) -> x:'a -> 'a t -> boolIs the value a member of the iterator?
the equality predicate to use (default (=))
val find : ('a -> 'b option) -> 'a t -> 'b optionFind the first element on which the function doesn't return None
val find_pred : f:('a -> bool) -> 'a t -> 'a optionfind_pred p l finds the first element of l that satisfies p, or returns None if no element satisfies p
val find_pred_exn : f:('a -> bool) -> 'a t -> 'aUnsafe version of find_pred
if no such element is found
val length : 'a t -> intHow long is the iterator? Forces the iterator.
val is_empty : 'a t -> boolIs the iterator empty? Forces the iterator.
val append : 'a t -> 'a t -> 'a tAppend two iterators. Iterating on the result is like iterating on the first, then on the second.
val append_l : 'a t list -> 'a tAppend iterators. Iterating on the result is like iterating on the each iterator of the list in order.
val flat_map_l : f:('a -> 'b list) -> 'a t -> 'b tConvenience function combining flat_map and of_list
val seq_list : 'a t list -> 'a list tseq_list l returns all the ways to pick one element in each sub-iterator in l. Assumes the sub-iterators can be iterated on several times.
val seq_list_map : f:('a -> 'b t) -> 'a list -> 'b list tseq_list_map f l maps f over every element of l, then calls seq_list
val filter_map : f:('a -> 'b option) -> 'a t -> 'b tMap and only keep non-None elements Formerly fmap
val filter_mapi : f:(int -> 'a -> 'b option) -> 'a t -> 'b tMap with indices, and only keep non-None elements
val filter_count : f:('a -> bool) -> 'a t -> intCount how many elements satisfy the given predicate
val intersperse : x:'a -> 'a t -> 'a tInsert the single element between every element of the iterator
val keep_some : 'a option t -> 'a tfilter_some l retains only elements of the form Some x. Same as filter_map (fun x->x)
val persistent : 'a t -> 'a tIterate on the iterator, storing elements in an efficient internal structure.. The resulting iterator can be iterated on as many times as needed. Note: calling persistent on an already persistent iterator will still make a new copy of the iterator!
val persistent_lazy : 'a t -> 'a tLazy version of persistent. When calling persistent_lazy s, a new iterator 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.
val sort : ?cmp:('a -> 'a -> int) -> 'a t -> 'a tSort the iterator. Eager, O(n) ram and O(n ln(n)) time. It iterates on elements of the argument iterator immediately, before it sorts them.
val sort_uniq : ?cmp:('a -> 'a -> int) -> 'a t -> 'a tSort the iterator and remove duplicates. Eager, same as sort
val sorted : ?cmp:('a -> 'a -> int) -> 'a t -> boolChecks whether the iterator is sorted. Eager, same as sort.
val group_succ_by : ?eq:('a -> 'a -> bool) -> 'a t -> 'a list tGroup equal consecutive elements. Formerly synonym to group.
val group_by : ?hash:('a -> int) -> ?eq:('a -> 'a -> bool) -> 'a t -> 'a list tGroup equal elements, disregarding their order of appearance. The result iterator is traversable as many times as required. precondition: for any x and y, if eq x y then hash x=hash y must hold.
val count : ?hash:('a -> int) -> ?eq:('a -> 'a -> bool) -> 'a t -> ('a * int) tMap each distinct element to its number of occurrences in the whole seq. Similar to group_by seq |> map (fun l->List.hd l, List.length l)
val uniq : ?eq:('a -> 'a -> bool) -> 'a t -> 'a tRemove consecutive duplicate elements. Basically this is like fun seq -> map List.hd (group seq).
val product : 'a t -> 'b t -> ('a * 'b) tCartesian product of the iterators. 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 diagonal_l : 'a list -> ('a * 'a) tAll pairs of distinct positions of the list. diagonal l will return the iterator of all List.nth i l, List.nth j l if i < j.
val diagonal : 'a t -> ('a * 'a) tAll pairs of distinct positions of the iterator. Iterates only once on the iterator, which must be finite.
val join : join_row:('a -> 'b -> 'c option) -> 'a t -> 'b t -> 'c tjoin ~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.
val join_by : ?eq:'key equal -> ?hash:'key hash -> ('a -> 'key) -> ('b -> 'key) -> merge:('key -> 'a -> 'b -> 'c option) -> 'a t -> 'b t -> 'c tjoin key1 key2 ~merge is a binary operation that takes two iterators a and b, projects their elements resp. with key1 and key2, and combine values (x,y) from (a,b) with the same key using merge. If merge returns None, the combination of values is discarded. precondition: for any x and y, if eq x y then hash x=hash y must hold.
val join_all_by : ?eq:'key equal -> ?hash:'key hash -> ('a -> 'key) -> ('b -> 'key) -> merge:('key -> 'a list -> 'b list -> 'c option) -> 'a t -> 'b t -> 'c tjoin_all_by key1 key2 ~merge is a binary operation that takes two iterators a and b, projects their elements resp. with key1 and key2, and, for each key k occurring in at least one of them:
l1 of elements of a that map to kl2 of elements of b that map to kmerge k l1 l2. If merge returns None, the combination of values is discarded, otherwise it returns Some c and c is inserted in the result.val group_join_by : ?eq:'a equal -> ?hash:'a hash -> ('b -> 'a) -> 'a t -> 'b t -> ('a * 'b list) tgroup_join_by key2 associates to every element x of the first iterator, all the elements y of the second iterator such that eq x (key y). Elements of the first iterators without corresponding values in the second one are mapped to [] precondition: for any x and y, if eq x y then hash x=hash y must hold.
val inter : ?eq:'a equal -> ?hash:'a hash -> 'a t -> 'a t -> 'a tIntersection of two collections. Each element will occur at most once in the result. Eager. precondition: for any x and y, if eq x y then hash x=hash y must hold.
val union : ?eq:'a equal -> ?hash:'a hash -> 'a t -> 'a t -> 'a tUnion of two collections. Each element will occur at most once in the result. Eager. precondition: for any x and y, if eq x y then hash x=hash y must hold.
val subset : ?eq:'a equal -> ?hash:'a hash -> 'a t -> 'a t -> boolsubset a b returns true if all elements of a belong to b. Eager. precondition: for any x and y, if eq x y then hash x=hash y must hold.
val unfoldr : ('b -> ('a * 'b) option) -> 'b -> 'a tunfoldr f b will apply f to b. If it yields Some (x,b') then x is returned and unfoldr recurses with b'.
val max : ?lt:('a -> 'a -> bool) -> 'a t -> 'a optionMax element of the iterator, using the given comparison function.
None if the iterator is empty, Some m where m is the maximal element otherwise
val max_exn : ?lt:('a -> 'a -> bool) -> 'a t -> 'aUnsafe version of max
if the iterator is empty
val min : ?lt:('a -> 'a -> bool) -> 'a t -> 'a optionMin element of the iterator, using the given comparison function. see max for more details.
val min_exn : ?lt:('a -> 'a -> bool) -> 'a t -> 'aUnsafe version of min
if the iterator is empty
val sum : int t -> intSum of elements
val sumf : float t -> floatSum of elements, using Kahan summation
val head : 'a t -> 'a optionFirst element, if any, otherwise None
val head_exn : 'a t -> 'aFirst element, if any, fails
if the iterator is empty
val take : int -> 'a t -> 'a tTake at most n elements from the iterator. Works on infinite iterators.
val take_while : f:('a -> bool) -> 'a t -> 'a tTake elements while they satisfy the predicate, then stops iterating. Will work on an infinite iterator s if the predicate is false for at least one element of s.
val fold_while : f:('a -> 'b -> 'a * [ `Stop | `Continue ]) -> init:'a -> 'b t -> 'aFolds over elements of the iterator, stopping early if the accumulator returns ('a, `Stop)
val rev : 'a t -> 'a tReverse the iterator. O(n) memory and time, needs the iterator to be finite. The result is persistent and does not depend on the input being repeatable.
val zip_i : 'a t -> (int * 'a) tZip elements of the iterator with their index in the iterator.
val fold2 : f:('c -> 'a -> 'b -> 'c) -> init:'c -> ('a * 'b) t -> 'cval iter2 : f:('a -> 'b -> unit) -> ('a * 'b) t -> unitval map2 : f:('a -> 'b -> 'c) -> ('a * 'b) t -> 'c tval map2_2 : f:('a -> 'b -> 'c) -> ('a -> 'b -> 'd) -> ('a * 'b) t -> ('c * 'd) tmap2_2 f g seq2 maps each x, y of seq2 into f x y, g x y
val to_list : 'a t -> 'a listConvert the iterator 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.
val to_rev_list : 'a t -> 'a listGet the list of the reversed iterator (more efficient than to_list)
val of_list : 'a list -> 'a tval on_list : ('a t -> 'b t) -> 'a list -> 'b liston_list f l is equivalent to to_list @@ f @@ of_list l.
val pair_with_idx : 'a t -> (int * 'a) tSimilar to zip_i but returns a normal iterator of tuples
val to_array : 'a t -> 'a arrayConvert to an array. Currently not very efficient because an intermediate list is used.
val of_array : 'a array -> 'a tval of_array_i : 'a array -> (int * 'a) tElements of the array, with their index
val array_slice : 'a array -> int -> int -> 'a tarray_slice a i j Iterator of elements whose indexes range from i to j
val of_opt : 'a option -> 'a tIterate on 0 or 1 values.
val of_stream : 'a Stream.t -> 'a tIterator of elements of a stream (usable only once)
val to_stream : 'a t -> 'a Stream.tConvert to a stream. linear in memory and time (a copy is made in memory)
val to_stack : 'a Stack.t -> 'a t -> unitPush elements of the iterator on the stack
val of_stack : 'a Stack.t -> 'a tIterator of elements of the stack (same order as Stack.iter)
val to_queue : 'a Queue.t -> 'a t -> unitPush elements of the iterator into the queue
val of_queue : 'a Queue.t -> 'a tIterator of elements contained in the queue, FIFO order
val hashtbl_add : ('a, 'b) Hashtbl.t -> ('a * 'b) t -> unitAdd elements of the iterator to the hashtable, with Hashtbl.add
val hashtbl_replace : ('a, 'b) Hashtbl.t -> ('a * 'b) t -> unitAdd elements of the iterator to the hashtable, with Hashtbl.replace (erases conflicting bindings)
val to_hashtbl : ('a * 'b) t -> ('a, 'b) Hashtbl.tBuild a hashtable from an iterator of key/value pairs
val of_hashtbl : ('a, 'b) Hashtbl.t -> ('a * 'b) tIterator of key/value pairs from the hashtable
val hashtbl_keys : ('a, 'b) Hashtbl.t -> 'a tval hashtbl_values : ('a, 'b) Hashtbl.t -> 'b tval of_str : string -> char tval to_str : char t -> stringval concat_str : string t -> stringConcatenate strings together, eagerly. Also see intersperse to add a separator.
exception OneShotSequenceRaised when the user tries to iterate several times on a transient iterator
val of_in_channel : Pervasives.in_channel -> char tIterates on characters of the input (can block when one iterates over the iterator). If you need to iterate several times on this iterator, use persistent.
when used more than once.
val to_buffer : char t -> Buffer.t -> unitCopy content of the iterator into the buffer
val int_range : start:int -> stop:int -> int tIterator on integers in start...stop by steps 1. Also see (--) for an infix version.
val int_range_dec : start:int -> stop:int -> int tIterator on decreasing integers in stop...start by steps -1. See (--^) for an infix version
val int_range_by : step:int -> start:int -> stop:int -> int tint_range_by ~step ~start:i ~stop:j is the range starting at i, including j, where the difference between successive elements is step. use a negative step for a decreasing iterator.
if step=0
val bools : bool tIterates on true and false
val of_set : (module Set.S with type elt = 'a and type t = 'b) -> 'b -> 'a tConvert the given set to an iterator. The set module must be provided.
val to_set : (module Set.S with type elt = 'a and type t = 'b) -> 'a t -> 'bConvert the iterator to a set, given the proper set module
type 'a gen = unit -> 'a optiontype 'a klist = unit -> [ `Nil | `Cons of 'a * 'a klist ]module Set : sig ... endmodule Map : sig ... endval random_int : int -> int tInfinite iterator of random integers between 0 and the given higher bound (see Random.int)
val random_bool : bool tInfinite iterator of random bool values
val random_float : float -> float tval random_array : 'a array -> 'a tIterator of choices of an element in the array
val random_list : 'a list -> 'a tInfinite iterator of random elements of the list. Basically the same as random_array.
val shuffle : 'a t -> 'a tshuffle seq returns a perfect shuffle of seq. Uses O(length seq) memory and time. Eager.
val shuffle_buffer : n:int -> 'a t -> 'a tshuffle_buffer n seq returns an iterator of element of seq in random order. The shuffling is not uniform. Uses O(n) memory.
The first n elements of the iterator are consumed immediately. The rest is consumed lazily.
val sample : n:int -> 'a t -> 'a arraysample n seq returns k samples of seq, with uniform probability. It will consume the iterator and use O(n) memory.
It returns an array of size min (length seq) n.
module Infix : sig ... endinclude module type of Infixval (--) : int -> int -> int ta -- b is the range of integers from a to b, both included, in increasing order. It will therefore be empty if a > b.
val (--^) : int -> int -> int ta --^ b is the range of integers from b to a, both included, in decreasing order (starts from a). It will therefore be empty if a < b.
val pp_seq : ?sep:string -> (Format.formatter -> 'a -> unit) -> Format.formatter -> 'a t -> unitPretty print an iterator 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 -> 'a t -> unitPrint into a buffer
val to_string : ?sep:string -> ('a -> string) -> 'a t -> stringPrint into a string
Very basic interface to manipulate files as iterator of chunks/lines. The iterators take care of opening and closing files properly; every time one iterates over an iterator, the file is opened/closed again.
Example: copy a file "a" into file "b", removing blank lines:
Iterator.(IO.lines_of "a" |> filter (fun l-> l<> "") |> IO.write_lines "b");;By chunks of 4096 bytes:
Iterator.IO.(chunks_of ~size:4096 "a" |> write_to "b");;Read the lines of a file into a list:
Iterator.IO.lines "a" |> Iterator.to_listmodule IO : sig ... endSequenceinclude IterThe 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 an iterator 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 iterators (e.g. group). The transformation is computed on the fly every time one iterates over the resulting iterator. 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 an iterator, one gets a new iterator, but nothing else happens until this new iterator is used (by folding or iterating on it).
If an iterator 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 an iterator that iterates on the elements of this memory structure, cheaply and repeatably.
type +'a t = ('a -> unit) -> unitAn iterator of values of type 'a. If you give it a function 'a -> unit it will be applied to every element of the iterator successively.
type +'a iter = 'a tNOTE Type ('a, 'b) t2 = ('a -> 'b -> unit) -> unit has been removed and subsumed by ('a * 'b) t
val from_iter : (('a -> unit) -> unit) -> 'a tBuild an iterator from a iter function
val from_labelled_iter : (f:('a -> unit) -> unit) -> 'a tBuild an iterator from a labelled iter function
val from_fun : (unit -> 'a option) -> 'a tCall the function repeatedly until it returns None. This iterator is transient, use persistent if needed!
val empty : 'a tEmpty iterator. It contains no element.
val singleton : 'a -> 'a tSingleton iterator, with exactly one element.
val doubleton : 'a -> 'a -> 'a tIterator with exactly two elements
val init : (int -> 'a) -> 'a tinit f is the infinite iterator f 0; f 1; f 2; ….
val repeat : 'a -> 'a tInfinite iterator of the same element. You may want to look at take and the likes if you iterate on it.
val iterate : ('a -> 'a) -> 'a -> 'a titerate f x is the infinite iterator x, f(x), f(f(x)), ...
val forever : (unit -> 'b) -> 'b tIterator that calls the given function to produce elements. The iterator may be transient (depending on the function), and definitely is infinite. You may want to use take and persistent.
val cycle : 'a t -> 'a tCycle forever through the given iterator. Assume the given iterator can be traversed any amount of times (not transient). This yields an infinite iterator, you should use something like take not to loop forever.
val unfoldr : ('b -> ('a * 'b) option) -> 'b -> 'a tunfoldr f b will apply f to b. If it yields Some (x,b') then x is returned and unfoldr recurses with b'.
val iter : ('a -> unit) -> 'a t -> unitConsume the iterator, passing all its arguments to the function. Basically iter f seq is just seq f.
val iteri : (int -> 'a -> unit) -> 'a t -> unitIterate on elements and their index in the iterator
val fold : ('a -> 'b -> 'a) -> 'a -> 'b t -> 'aFold over elements of the iterator, consuming it
val foldi : ('a -> int -> 'b -> 'a) -> 'a -> 'b t -> 'aFold over elements of the iterator and their index, consuming it
val fold_map : ('acc -> 'a -> 'acc * 'b) -> 'acc -> 'a t -> 'b tfold_map f acc l is like map, but it carries some state as in fold. The state is not returned, it is just used to thread some information to the map function.
val fold_filter_map : ('acc -> 'a -> 'acc * 'b option) -> 'acc -> 'a t -> 'b tfold_filter_map f acc l is a fold_map-like function, but the function can choose to skip an element by retuning None.
val map_by_2 : ('a -> 'a -> 'a) -> 'a t -> 'a tMap objects two by two. lazily. The last element is kept in the iterator if the count is odd.
val for_all : ('a -> bool) -> 'a t -> boolDo all elements satisfy the predicate?
val exists : ('a -> bool) -> 'a t -> boolExists there some element satisfying the predicate?
val mem : ?eq:('a -> 'a -> bool) -> 'a -> 'a t -> boolIs the value a member of the iterator?
the equality predicate to use (default (=))
val find : ('a -> 'b option) -> 'a t -> 'b optionFind the first element on which the function doesn't return None
val find_pred : ('a -> bool) -> 'a t -> 'a optionfind_pred p l finds the first element of l that satisfies p, or returns None if no element satisfies p
val find_pred_exn : ('a -> bool) -> 'a t -> 'aUnsafe version of find_pred
if no such element is found
val length : 'a t -> intHow long is the iterator? Forces the iterator.
val is_empty : 'a t -> boolIs the iterator empty? Forces the iterator.
val append : 'a t -> 'a t -> 'a tAppend two iterators. Iterating on the result is like iterating on the first, then on the second.
val append_l : 'a t list -> 'a tAppend iterators. Iterating on the result is like iterating on the each iterator of the list in order.
val flat_map : ('a -> 'b t) -> 'a t -> 'b tMonadic bind. Intuitively, it applies the function to every element of the initial iterator, and calls concat. Formerly flatMap
val flat_map_l : ('a -> 'b list) -> 'a t -> 'b tConvenience function combining flat_map and of_list
val seq_list : 'a t list -> 'a list tseq_list l returns all the ways to pick one element in each sub-iterator in l. Assumes the sub-iterators can be iterated on several times.
val seq_list_map : ('a -> 'b t) -> 'a list -> 'b list tseq_list_map f l maps f over every element of l, then calls seq_list
val filter_map : ('a -> 'b option) -> 'a t -> 'b tMap and only keep non-None elements Formerly fmap
val filter_mapi : (int -> 'a -> 'b option) -> 'a t -> 'b tMap with indices, and only keep non-None elements
val filter_count : ('a -> bool) -> 'a t -> intCount how many elements satisfy the given predicate
val keep_some : 'a option t -> 'a tfilter_some l retains only elements of the form Some x. Same as filter_map (fun x->x)
val persistent : 'a t -> 'a tIterate on the iterator, storing elements in an efficient internal structure.. The resulting iterator can be iterated on as many times as needed. Note: calling persistent on an already persistent iterator will still make a new copy of the iterator!
val persistent_lazy : 'a t -> 'a tLazy version of persistent. When calling persistent_lazy s, a new iterator 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.
val sort : ?cmp:('a -> 'a -> int) -> 'a t -> 'a tSort the iterator. Eager, O(n) ram and O(n ln(n)) time. It iterates on elements of the argument iterator immediately, before it sorts them.
val sort_uniq : ?cmp:('a -> 'a -> int) -> 'a t -> 'a tSort the iterator and remove duplicates. Eager, same as sort
val sorted : ?cmp:('a -> 'a -> int) -> 'a t -> boolChecks whether the iterator is sorted. Eager, same as sort.
val group_succ_by : ?eq:('a -> 'a -> bool) -> 'a t -> 'a list tGroup equal consecutive elements. Linear time. Formerly synonym to group.
val group_by : ?hash:('a -> int) -> ?eq:('a -> 'a -> bool) -> 'a t -> 'a list tGroup equal elements, disregarding their order of appearance. The result iterator is traversable as many times as required. precondition: for any x and y, if eq x y then hash x=hash y must hold.
val count : ?hash:('a -> int) -> ?eq:('a -> 'a -> bool) -> 'a t -> ('a * int) tMap each distinct element to its number of occurrences in the whole seq. Similar to group_by seq |> map (fun l->List.hd l, List.length l) precondition: for any x and y, if eq x y then hash x=hash y must hold.
val uniq : ?eq:('a -> 'a -> bool) -> 'a t -> 'a tRemove consecutive duplicate elements. Basically this is like fun seq -> map List.hd (group seq).
val product : 'a t -> 'b t -> ('a * 'b) tCartesian product of iterators. 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 diagonal_l : 'a list -> ('a * 'a) tAll pairs of distinct positions of the list. diagonal l will return the iterator of all List.nth i l, List.nth j l if i < j.
val diagonal : 'a t -> ('a * 'a) tAll pairs of distinct positions of the iterator. Iterates only once on the iterator, which must be finite.
val join : join_row:('a -> 'b -> 'c option) -> 'a t -> 'b t -> 'c tjoin ~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.
val join_by : ?eq:'key equal -> ?hash:'key hash -> ('a -> 'key) -> ('b -> 'key) -> merge:('key -> 'a -> 'b -> 'c option) -> 'a t -> 'b t -> 'c tjoin key1 key2 ~merge is a binary operation that takes two iterators a and b, projects their elements resp. with key1 and key2, and combine values (x,y) from (a,b) with the same key using merge. If merge returns None, the combination of values is discarded. precondition: for any x and y, if eq x y then hash x=hash y must hold.
val join_all_by : ?eq:'key equal -> ?hash:'key hash -> ('a -> 'key) -> ('b -> 'key) -> merge:('key -> 'a list -> 'b list -> 'c option) -> 'a t -> 'b t -> 'c tjoin_all_by key1 key2 ~merge is a binary operation that takes two iterators a and b, projects their elements resp. with key1 and key2, and, for each key k occurring in at least one of them:
l1 of elements of a that map to kl2 of elements of b that map to kmerge k l1 l2. If merge returns None, the combination of values is discarded, otherwise it returns Some c and c is inserted in the result.val group_join_by : ?eq:'a equal -> ?hash:'a hash -> ('b -> 'a) -> 'a t -> 'b t -> ('a * 'b list) tgroup_join_by key2 associates to every element x of the first iterator, all the elements y of the second iterator such that eq x (key y). Elements of the first iterators without corresponding values in the second one are mapped to [] precondition: for any x and y, if eq x y then hash x=hash y must hold.
val inter : ?eq:'a equal -> ?hash:'a hash -> 'a t -> 'a t -> 'a tIntersection of two collections. Each element will occur at most once in the result. Eager. precondition: for any x and y, if eq x y then hash x=hash y must hold.
val max : ?lt:('a -> 'a -> bool) -> 'a t -> 'a optionMax element of the iterator, using the given comparison function.
None if the iterator is empty, Some m where m is the maximal element otherwise
val max_exn : ?lt:('a -> 'a -> bool) -> 'a t -> 'aUnsafe version of max
if the iterator is empty
val min : ?lt:('a -> 'a -> bool) -> 'a t -> 'a optionMin element of the iterator, using the given comparison function. see max for more details.
val min_exn : ?lt:('a -> 'a -> bool) -> 'a t -> 'aUnsafe version of min
if the iterator is empty
val sum : int t -> intSum of elements
val sumf : float t -> floatSum of elements, using Kahan summation
val head : 'a t -> 'a optionFirst element, if any, otherwise None
val head_exn : 'a t -> 'aFirst element, if any, fails
if the iterator is empty
val take : int -> 'a t -> 'a tTake at most n elements from the iterator. Works on infinite iterators.
val take_while : ('a -> bool) -> 'a t -> 'a tTake elements while they satisfy the predicate, then stops iterating. Will work on an infinite iterator s if the predicate is false for at least one element of s.
val fold_while : ('a -> 'b -> 'a * [ `Stop | `Continue ]) -> 'a -> 'b t -> 'aFolds over elements of the iterator, stopping early if the accumulator returns ('a, `Stop)
val to_list : 'a t -> 'a listConvert the iterator 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.
val to_rev_list : 'a t -> 'a listGet the list of the reversed iterator (more efficient than to_list)
val of_list : 'a list -> 'a tval on_list : ('a t -> 'b t) -> 'a list -> 'b liston_list f l is equivalent to to_list @@ f @@ of_list l.
val pair_with_idx : 'a t -> (int * 'a) tSimilar to zip_i but returns a normal iterator of tuples
val to_array : 'a t -> 'a arrayConvert to an array. Currently not very efficient because an intermediate list is used.
val of_array : 'a array -> 'a tval of_array_i : 'a array -> (int * 'a) tElements of the array, with their index
val array_slice : 'a array -> int -> int -> 'a tarray_slice a i j Iterator of elements whose indexes range from i to j
val of_opt : 'a option -> 'a tIterate on 0 or 1 values.
val of_stream : 'a Stream.t -> 'a tIterator of elements of a stream (usable only once)
val to_stream : 'a t -> 'a Stream.tConvert to a stream. linear in memory and time (a copy is made in memory)
val to_stack : 'a Stack.t -> 'a t -> unitPush elements of the iterator on the stack
val of_stack : 'a Stack.t -> 'a tIterator of elements of the stack (same order as Stack.iter)
val to_queue : 'a Queue.t -> 'a t -> unitPush elements of the iterator into the queue
val of_queue : 'a Queue.t -> 'a tIterator of elements contained in the queue, FIFO order
val hashtbl_add : ('a, 'b) Hashtbl.t -> ('a * 'b) t -> unitAdd elements of the iterator to the hashtable, with Hashtbl.add
val hashtbl_replace : ('a, 'b) Hashtbl.t -> ('a * 'b) t -> unitAdd elements of the iterator to the hashtable, with Hashtbl.replace (erases conflicting bindings)
val to_hashtbl : ('a * 'b) t -> ('a, 'b) Hashtbl.tBuild a hashtable from an iterator of key/value pairs
val of_hashtbl : ('a, 'b) Hashtbl.t -> ('a * 'b) tIterator of key/value pairs from the hashtable
val hashtbl_keys : ('a, 'b) Hashtbl.t -> 'a tval hashtbl_values : ('a, 'b) Hashtbl.t -> 'b tval of_str : string -> char tval to_str : char t -> stringval concat_str : string t -> stringConcatenate strings together, eagerly. Also see intersperse to add a separator.
exception OneShotSequenceRaised when the user tries to iterate several times on a transient iterator
val of_in_channel : Pervasives.in_channel -> char tIterates on characters of the input (can block when one iterates over the iterator). If you need to iterate several times on this iterator, use persistent.
when used more than once.
val to_buffer : char t -> Buffer.t -> unitCopy content of the iterator into the buffer
val int_range : start:int -> stop:int -> int tIterator on integers in start...stop by steps 1. Also see (--) for an infix version.
val int_range_dec : start:int -> stop:int -> int tIterator on decreasing integers in stop...start by steps -1. See (--^) for an infix version
val int_range_by : step:int -> int -> int -> int tint_range_by ~step i j is the range starting at i, including j, where the difference between successive elements is step. use a negative step for a decreasing iterator.
if step=0
val bools : bool tIterates on true and false
val of_set : (module Set.S with type elt = 'a and type t = 'b) -> 'b -> 'a tConvert the given set to an iterator. The set module must be provided.
val to_set : (module Set.S with type elt = 'a and type t = 'b) -> 'a t -> 'bConvert the iterator to a set, given the proper set module
type 'a gen = unit -> 'a optiontype 'a klist = unit -> [ `Nil | `Cons of 'a * 'a klist ]module Set = Iter.Setmodule Map = Iter.Mapval random_int : int -> int tInfinite iterator of random integers between 0 and the given higher bound (see Random.int)
val random_bool : bool tInfinite iterator of random bool values
val random_float : float -> float tval random_array : 'a array -> 'a tIterator of choices of an element in the array
val random_list : 'a list -> 'a tInfinite iterator of random elements of the list. Basically the same as random_array.
val shuffle : 'a t -> 'a tshuffle seq returns a perfect shuffle of seq. Uses O(length seq) memory and time. Eager.
val shuffle_buffer : int -> 'a t -> 'a tshuffle_buffer n seq returns an iterator of element of seq in random order. The shuffling is *not* uniform. Uses O(n) memory.
The first n elements of the iterator are consumed immediately. The rest is consumed lazily.
val sample : int -> 'a t -> 'a arraysample n seq returns k samples of seq, with uniform probability. It will consume the iterator and use O(n) memory.
It returns an array of size min (length seq) n.
module Infix = Iter.Infixinclude module type of Infixval (--) : int -> int -> int ta -- b is the range of integers from a to b, both included, in increasing order. It will therefore be empty if a > b.
val (--^) : int -> int -> int ta --^ b is the range of integers from b to a, both included, in decreasing order (starts from a). It will therefore be empty if a < b.
val pp_seq : ?sep:string -> (Format.formatter -> 'a -> unit) -> Format.formatter -> 'a t -> unitPretty print an iterator 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 -> 'a t -> unitPrint into a buffer
val to_string : ?sep:string -> ('a -> string) -> 'a t -> stringPrint into a string
Very basic interface to manipulate files as iterator of chunks/lines. The iterators take care of opening and closing files properly; every time one iterates over an iterator, the file is opened/closed again.
Example: copy a file "a" into file "b", removing blank lines:
Iterator.(IO.lines_of "a" |> filter (fun l-> l<> "") |> IO.write_lines "b");;By chunks of 4096 bytes:
Iterator.IO.(chunks_of ~size:4096 "a" |> write_to "b");;Read the lines of a file into a list:
Iterator.IO.lines "a" |> Iterator.to_listmodule IO = Iter.IOtype 'a sequence = 'a iterSequenceLabelsinclude IterLabelstype +'a t = ('a -> unit) -> unitAn iterator of values of type 'a. If you give it a function 'a -> unit it will be applied to every element of the iterator successively.
type +'a iter = 'a tNOTE Type ('a, 'b) t2 = ('a -> 'b -> unit) -> unit has been removed and subsumed by ('a * 'b) t
val from_iter : (('a -> unit) -> unit) -> 'a tBuild an iterator from a iter function
val from_labelled_iter : (f:('a -> unit) -> unit) -> 'a tBuild an iterator from a labelled iter function
val from_fun : (unit -> 'a option) -> 'a tCall the function repeatedly until it returns None. This iterator is transient, use persistent if needed!
val empty : 'a tEmpty iterator. It contains no element.
val singleton : 'a -> 'a tSingleton iterator, with exactly one element.
val doubleton : 'a -> 'a -> 'a tIterator with exactly two elements
val init : f:(int -> 'a) -> 'a tinit f is the infinite iterator f 0; f 1; f 2; ….
val repeat : 'a -> 'a tInfinite iterator of the same element. You may want to look at take and the likes if you iterate on it.
val iterate : ('a -> 'a) -> 'a -> 'a titerate f x is the infinite iterator x, f(x), f(f(x)), ...
val forever : (unit -> 'b) -> 'b tIterator that calls the given function to produce elements. The iterator may be transient (depending on the function), and definitely is infinite. You may want to use take and persistent.
val iter : f:('a -> unit) -> 'a t -> unitConsume the iterator, passing all its arguments to the function. Basically iter f seq is just seq f.
val iteri : f:(int -> 'a -> unit) -> 'a t -> unitIterate on elements and their index in the iterator
val fold : f:('a -> 'b -> 'a) -> init:'a -> 'b t -> 'aFold over elements of the iterator, consuming it
val foldi : f:('a -> int -> 'b -> 'a) -> init:'a -> 'b t -> 'aFold over elements of the iterator and their index, consuming it
val fold_map : f:('acc -> 'a -> 'acc * 'b) -> init:'acc -> 'a t -> 'b tfold_map f acc l is like map, but it carries some state as in fold. The state is not returned, it is just used to thread some information to the map function.
val fold_filter_map : f:('acc -> 'a -> 'acc * 'b option) -> init:'acc -> 'a t -> 'b tfold_filter_map f acc l is a fold_map-like function, but the function can choose to skip an element by retuning None.
val map_by_2 : f:('a -> 'a -> 'a) -> 'a t -> 'a tMap objects two by two. lazily. The last element is kept in the iterator if the count is odd.
val for_all : f:('a -> bool) -> 'a t -> boolDo all elements satisfy the predicate?
val exists : f:('a -> bool) -> 'a t -> boolExists there some element satisfying the predicate?
val mem : ?eq:('a -> 'a -> bool) -> x:'a -> 'a t -> boolIs the value a member of the iterator?
the equality predicate to use (default (=))
val find : ('a -> 'b option) -> 'a t -> 'b optionFind the first element on which the function doesn't return None
val find_pred : f:('a -> bool) -> 'a t -> 'a optionfind_pred p l finds the first element of l that satisfies p, or returns None if no element satisfies p
val find_pred_exn : f:('a -> bool) -> 'a t -> 'aUnsafe version of find_pred
if no such element is found
val length : 'a t -> intHow long is the iterator? Forces the iterator.
val is_empty : 'a t -> boolIs the iterator empty? Forces the iterator.
val append : 'a t -> 'a t -> 'a tAppend two iterators. Iterating on the result is like iterating on the first, then on the second.
val append_l : 'a t list -> 'a tAppend iterators. Iterating on the result is like iterating on the each iterator of the list in order.
val flat_map_l : f:('a -> 'b list) -> 'a t -> 'b tConvenience function combining flat_map and of_list
val seq_list : 'a t list -> 'a list tseq_list l returns all the ways to pick one element in each sub-iterator in l. Assumes the sub-iterators can be iterated on several times.
val seq_list_map : f:('a -> 'b t) -> 'a list -> 'b list tseq_list_map f l maps f over every element of l, then calls seq_list
val filter_map : f:('a -> 'b option) -> 'a t -> 'b tMap and only keep non-None elements Formerly fmap
val filter_mapi : f:(int -> 'a -> 'b option) -> 'a t -> 'b tMap with indices, and only keep non-None elements
val filter_count : f:('a -> bool) -> 'a t -> intCount how many elements satisfy the given predicate
val intersperse : x:'a -> 'a t -> 'a tInsert the single element between every element of the iterator
val keep_some : 'a option t -> 'a tfilter_some l retains only elements of the form Some x. Same as filter_map (fun x->x)
val persistent : 'a t -> 'a tIterate on the iterator, storing elements in an efficient internal structure.. The resulting iterator can be iterated on as many times as needed. Note: calling persistent on an already persistent iterator will still make a new copy of the iterator!
val persistent_lazy : 'a t -> 'a tLazy version of persistent. When calling persistent_lazy s, a new iterator 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.
val sort : ?cmp:('a -> 'a -> int) -> 'a t -> 'a tSort the iterator. Eager, O(n) ram and O(n ln(n)) time. It iterates on elements of the argument iterator immediately, before it sorts them.
val sort_uniq : ?cmp:('a -> 'a -> int) -> 'a t -> 'a tSort the iterator and remove duplicates. Eager, same as sort
val sorted : ?cmp:('a -> 'a -> int) -> 'a t -> boolChecks whether the iterator is sorted. Eager, same as sort.
val group_succ_by : ?eq:('a -> 'a -> bool) -> 'a t -> 'a list tGroup equal consecutive elements. Formerly synonym to group.
val group_by : ?hash:('a -> int) -> ?eq:('a -> 'a -> bool) -> 'a t -> 'a list tGroup equal elements, disregarding their order of appearance. The result iterator is traversable as many times as required. precondition: for any x and y, if eq x y then hash x=hash y must hold.
val count : ?hash:('a -> int) -> ?eq:('a -> 'a -> bool) -> 'a t -> ('a * int) tMap each distinct element to its number of occurrences in the whole seq. Similar to group_by seq |> map (fun l->List.hd l, List.length l)
val uniq : ?eq:('a -> 'a -> bool) -> 'a t -> 'a tRemove consecutive duplicate elements. Basically this is like fun seq -> map List.hd (group seq).
val product : 'a t -> 'b t -> ('a * 'b) tCartesian product of the iterators. 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 diagonal_l : 'a list -> ('a * 'a) tAll pairs of distinct positions of the list. diagonal l will return the iterator of all List.nth i l, List.nth j l if i < j.
val diagonal : 'a t -> ('a * 'a) tAll pairs of distinct positions of the iterator. Iterates only once on the iterator, which must be finite.
val join : join_row:('a -> 'b -> 'c option) -> 'a t -> 'b t -> 'c tjoin ~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.
val join_by : ?eq:'key equal -> ?hash:'key hash -> ('a -> 'key) -> ('b -> 'key) -> merge:('key -> 'a -> 'b -> 'c option) -> 'a t -> 'b t -> 'c tjoin key1 key2 ~merge is a binary operation that takes two iterators a and b, projects their elements resp. with key1 and key2, and combine values (x,y) from (a,b) with the same key using merge. If merge returns None, the combination of values is discarded. precondition: for any x and y, if eq x y then hash x=hash y must hold.
val join_all_by : ?eq:'key equal -> ?hash:'key hash -> ('a -> 'key) -> ('b -> 'key) -> merge:('key -> 'a list -> 'b list -> 'c option) -> 'a t -> 'b t -> 'c tjoin_all_by key1 key2 ~merge is a binary operation that takes two iterators a and b, projects their elements resp. with key1 and key2, and, for each key k occurring in at least one of them:
l1 of elements of a that map to kl2 of elements of b that map to kmerge k l1 l2. If merge returns None, the combination of values is discarded, otherwise it returns Some c and c is inserted in the result.val group_join_by : ?eq:'a equal -> ?hash:'a hash -> ('b -> 'a) -> 'a t -> 'b t -> ('a * 'b list) tgroup_join_by key2 associates to every element x of the first iterator, all the elements y of the second iterator such that eq x (key y). Elements of the first iterators without corresponding values in the second one are mapped to [] precondition: for any x and y, if eq x y then hash x=hash y must hold.
val inter : ?eq:'a equal -> ?hash:'a hash -> 'a t -> 'a t -> 'a tIntersection of two collections. Each element will occur at most once in the result. Eager. precondition: for any x and y, if eq x y then hash x=hash y must hold.
val union : ?eq:'a equal -> ?hash:'a hash -> 'a t -> 'a t -> 'a tUnion of two collections. Each element will occur at most once in the result. Eager. precondition: for any x and y, if eq x y then hash x=hash y must hold.
val subset : ?eq:'a equal -> ?hash:'a hash -> 'a t -> 'a t -> boolsubset a b returns true if all elements of a belong to b. Eager. precondition: for any x and y, if eq x y then hash x=hash y must hold.
val unfoldr : ('b -> ('a * 'b) option) -> 'b -> 'a tunfoldr f b will apply f to b. If it yields Some (x,b') then x is returned and unfoldr recurses with b'.
val max : ?lt:('a -> 'a -> bool) -> 'a t -> 'a optionMax element of the iterator, using the given comparison function.
None if the iterator is empty, Some m where m is the maximal element otherwise
val max_exn : ?lt:('a -> 'a -> bool) -> 'a t -> 'aUnsafe version of max
if the iterator is empty
val min : ?lt:('a -> 'a -> bool) -> 'a t -> 'a optionMin element of the iterator, using the given comparison function. see max for more details.
val min_exn : ?lt:('a -> 'a -> bool) -> 'a t -> 'aUnsafe version of min
if the iterator is empty
val sum : int t -> intSum of elements
val sumf : float t -> floatSum of elements, using Kahan summation
val head : 'a t -> 'a optionFirst element, if any, otherwise None
val head_exn : 'a t -> 'aFirst element, if any, fails
if the iterator is empty
val take : int -> 'a t -> 'a tTake at most n elements from the iterator. Works on infinite iterators.
val take_while : f:('a -> bool) -> 'a t -> 'a tTake elements while they satisfy the predicate, then stops iterating. Will work on an infinite iterator s if the predicate is false for at least one element of s.
val fold_while : f:('a -> 'b -> 'a * [ `Stop | `Continue ]) -> init:'a -> 'b t -> 'aFolds over elements of the iterator, stopping early if the accumulator returns ('a, `Stop)
val rev : 'a t -> 'a tReverse the iterator. O(n) memory and time, needs the iterator to be finite. The result is persistent and does not depend on the input being repeatable.
val zip_i : 'a t -> (int * 'a) tZip elements of the iterator with their index in the iterator.
val fold2 : f:('c -> 'a -> 'b -> 'c) -> init:'c -> ('a * 'b) t -> 'cval iter2 : f:('a -> 'b -> unit) -> ('a * 'b) t -> unitval map2 : f:('a -> 'b -> 'c) -> ('a * 'b) t -> 'c tval map2_2 : f:('a -> 'b -> 'c) -> ('a -> 'b -> 'd) -> ('a * 'b) t -> ('c * 'd) tmap2_2 f g seq2 maps each x, y of seq2 into f x y, g x y
val to_list : 'a t -> 'a listConvert the iterator 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.
val to_rev_list : 'a t -> 'a listGet the list of the reversed iterator (more efficient than to_list)
val of_list : 'a list -> 'a tval on_list : ('a t -> 'b t) -> 'a list -> 'b liston_list f l is equivalent to to_list @@ f @@ of_list l.
val pair_with_idx : 'a t -> (int * 'a) tSimilar to zip_i but returns a normal iterator of tuples
val to_array : 'a t -> 'a arrayConvert to an array. Currently not very efficient because an intermediate list is used.
val of_array : 'a array -> 'a tval of_array_i : 'a array -> (int * 'a) tElements of the array, with their index
val array_slice : 'a array -> int -> int -> 'a tarray_slice a i j Iterator of elements whose indexes range from i to j
val of_opt : 'a option -> 'a tIterate on 0 or 1 values.
val of_stream : 'a Stream.t -> 'a tIterator of elements of a stream (usable only once)
val to_stream : 'a t -> 'a Stream.tConvert to a stream. linear in memory and time (a copy is made in memory)
val to_stack : 'a Stack.t -> 'a t -> unitPush elements of the iterator on the stack
val of_stack : 'a Stack.t -> 'a tIterator of elements of the stack (same order as Stack.iter)
val to_queue : 'a Queue.t -> 'a t -> unitPush elements of the iterator into the queue
val of_queue : 'a Queue.t -> 'a tIterator of elements contained in the queue, FIFO order
val hashtbl_add : ('a, 'b) Hashtbl.t -> ('a * 'b) t -> unitAdd elements of the iterator to the hashtable, with Hashtbl.add
val hashtbl_replace : ('a, 'b) Hashtbl.t -> ('a * 'b) t -> unitAdd elements of the iterator to the hashtable, with Hashtbl.replace (erases conflicting bindings)
val to_hashtbl : ('a * 'b) t -> ('a, 'b) Hashtbl.tBuild a hashtable from an iterator of key/value pairs
val of_hashtbl : ('a, 'b) Hashtbl.t -> ('a * 'b) tIterator of key/value pairs from the hashtable
val hashtbl_keys : ('a, 'b) Hashtbl.t -> 'a tval hashtbl_values : ('a, 'b) Hashtbl.t -> 'b tval of_str : string -> char tval to_str : char t -> stringval concat_str : string t -> stringConcatenate strings together, eagerly. Also see intersperse to add a separator.
exception OneShotSequenceRaised when the user tries to iterate several times on a transient iterator
val of_in_channel : Pervasives.in_channel -> char tIterates on characters of the input (can block when one iterates over the iterator). If you need to iterate several times on this iterator, use persistent.
when used more than once.
val to_buffer : char t -> Buffer.t -> unitCopy content of the iterator into the buffer
val int_range : start:int -> stop:int -> int tIterator on integers in start...stop by steps 1. Also see (--) for an infix version.
val int_range_dec : start:int -> stop:int -> int tIterator on decreasing integers in stop...start by steps -1. See (--^) for an infix version
val int_range_by : step:int -> start:int -> stop:int -> int tint_range_by ~step ~start:i ~stop:j is the range starting at i, including j, where the difference between successive elements is step. use a negative step for a decreasing iterator.
if step=0
val bools : bool tIterates on true and false
val of_set : (module Set.S with type elt = 'a and type t = 'b) -> 'b -> 'a tConvert the given set to an iterator. The set module must be provided.
val to_set : (module Set.S with type elt = 'a and type t = 'b) -> 'a t -> 'bConvert the iterator to a set, given the proper set module
type 'a gen = unit -> 'a optiontype 'a klist = unit -> [ `Nil | `Cons of 'a * 'a klist ]module Set = IterLabels.Setmodule Map = IterLabels.Mapval random_int : int -> int tInfinite iterator of random integers between 0 and the given higher bound (see Random.int)
val random_bool : bool tInfinite iterator of random bool values
val random_float : float -> float tval random_array : 'a array -> 'a tIterator of choices of an element in the array
val random_list : 'a list -> 'a tInfinite iterator of random elements of the list. Basically the same as random_array.
val shuffle : 'a t -> 'a tshuffle seq returns a perfect shuffle of seq. Uses O(length seq) memory and time. Eager.
val shuffle_buffer : n:int -> 'a t -> 'a tshuffle_buffer n seq returns an iterator of element of seq in random order. The shuffling is not uniform. Uses O(n) memory.
The first n elements of the iterator are consumed immediately. The rest is consumed lazily.
val sample : n:int -> 'a t -> 'a arraysample n seq returns k samples of seq, with uniform probability. It will consume the iterator and use O(n) memory.
It returns an array of size min (length seq) n.
module Infix = IterLabels.Infixinclude module type of Infixval (--) : int -> int -> int ta -- b is the range of integers from a to b, both included, in increasing order. It will therefore be empty if a > b.
val (--^) : int -> int -> int ta --^ b is the range of integers from b to a, both included, in decreasing order (starts from a). It will therefore be empty if a < b.
val pp_seq : ?sep:string -> (Format.formatter -> 'a -> unit) -> Format.formatter -> 'a t -> unitPretty print an iterator 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 -> 'a t -> unitPrint into a buffer
val to_string : ?sep:string -> ('a -> string) -> 'a t -> stringPrint into a string
Very basic interface to manipulate files as iterator of chunks/lines. The iterators take care of opening and closing files properly; every time one iterates over an iterator, the file is opened/closed again.
Example: copy a file "a" into file "b", removing blank lines:
Iterator.(IO.lines_of "a" |> filter (fun l-> l<> "") |> IO.write_lines "b");;By chunks of 4096 bytes:
Iterator.IO.(chunks_of ~size:4096 "a" |> write_to "b");;Read the lines of a file into a list:
Iterator.IO.lines "a" |> Iterator.to_listmodule IO = IterLabels.IO