NS.IterVersion of Iterator with labels
An 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.
module Import : sig ... endNOTE 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 -> f:('a -> 'a) -> 'a titerate ~f x is the infinite iterator x, f(x), f(f(x)), ...
val forever : f:(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.
Cycle 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 iter : 'a t -> f:('a -> unit) -> unitConsume the iterator, passing all its arguments to the function. Basically iter ~f seq is just seq f.
val iteri : 'a t -> f:(int -> 'a -> unit) -> unitIterate on elements and their index in the iterator
val for_each : 'a t -> ('a -> unit) -> unitConsume the iterator, passing all its arguments to the function. for_each seq f is the same as iter ~f seq, i.e., iter with arguments reversed.
val for_eachi : 'a t -> (int -> 'a -> unit) -> unitIterate on elements and their index in the iterator. for_eachi seq f is the same as iteri ~f seq, i.e., iteri with arguments reversed.
val fold : 'a t -> 's -> f:('a -> 's -> 's) -> 'sFold over elements of the iterator, consuming it
val foldi : 'a t -> 's -> f:(int -> 'a -> 's -> 's) -> 'sFold over elements of the iterator and their index, consuming it
val reduce : 'a t -> f:('a -> 'a -> 'a) -> 'a optionLike fold but use the first element as the initial accumulator.
val reduce_exn : 'a t -> f:('a -> 'a -> 'a) -> 'aLike fold but use the first element as the initial accumulator.
fold_filter_map seq s ~f 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 iterator if the count is odd.
val for_all : 'a t -> f:('a -> bool) -> boolDo all elements satisfy the predicate?
val exists : 'a t -> f:('a -> bool) -> boolExists there some element satisfying the predicate?
val mem : 'a -> 'a t -> eq:('a -> 'a -> bool) -> boolIs the value a member of the iterator?
val find : 'a t -> f:('a -> bool) -> 'a optionfind seq ~f finds the first element of seq that satisfies f, or returns None if no element satisfies f
val find_map : 'a t -> f:('a -> 'b option) -> 'b optionFind the first element on which the function doesn't return None
val length : 'a t -> intHow long is the iterator? Forces the iterator.
val is_empty : 'a t -> boolIs the iterator empty? Forces the iterator.
Append two iterators. Iterating on the result is like iterating on the first, then on the second.
Append iterators. Iterating on the result is like iterating on the each iterator of the list in order.
Monadic bind. Intuitively, it applies the function to every element of the initial sequence, and calls concat.
seq_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.
seq_list_map f l maps f over every element of l, then calls seq_list
Map and only keep non-None elements Formerly fmap
Map with indices, and only keep non-None elements
val filter_count : 'a t -> f:('a -> bool) -> intCount how many elements satisfy the given predicate
filter_some l retains only elements of the form Some x. Same as filter_map (fun x->x)
keep_error l retains only elements of the form Error x.
Iterate 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!
Lazy 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.
Sort 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.
Sort the iterator and remove duplicates. Eager, same as sort
val sorted : 'a t -> cmp:('a -> 'a -> int) -> boolChecks whether the iterator is sorted. Eager, same as sort.
Group equal consecutive elements. Formerly synonym to group.
Group 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.
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)
Remove consecutive duplicate elements. Basically this is like fun seq -> map List.hd (group seq).
val contains_dup : 'a t -> cmp:('a -> 'a -> int) -> boolHolds if there exist (not necessarily consecutive) duplicate elements.
Cartesian 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.
All pairs of distinct positions of the iterator. Iterates only once on the iterator, which must be finite.
join ~f: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 :
'a t ->
'b t ->
key1:('a -> 'key) ->
key2:('b -> 'key) ->
eq:'key equal ->
hash:'key hash ->
f:('key -> 'a -> 'b -> 'c option) ->
'c tjoin key1 key2 ~f: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 :
'a t ->
'b t ->
key1:('a -> 'key) ->
key2:('b -> 'key) ->
eq:'key equal ->
hash:'key hash ->
f:('key -> 'a list -> 'b list -> 'c option) ->
'c tjoin_all_by key1 key2 ~f: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.group_join_by ~f:key 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.
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 -> f:('b -> ('a * 'b) option) -> '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 : 'a t -> lt:('a -> 'a -> bool) -> 'a optionMax element of the iterator, using the given comparison function.
val min : 'a t -> lt:('a -> 'a -> bool) -> 'a optionMin element of the iterator, using the given comparison function. see max for more details.
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
Take at most n elements from the iterator. Works on infinite iterators.
Take 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 : 'b t -> 'a -> f:('b -> 'a -> [ `Stop | `Continue ] * 'a) -> 'aFolds over elements of the iterator, stopping early if the accumulator returns ('a, `Stop)
val fold_opt : 'a t -> 's -> f:('a -> 's -> 's option) -> 's optionFolds over elements of the iterator, stopping early if the accumulator returns None.
val fold_result :
'a t ->
's ->
f:('a -> 's -> ('s, 'e) Stdlib.result) ->
('s, 'e) Stdlib.resultFolds over elements of the iterator, stopping early if the accumulator returns Error.
val fold_until :
'a t ->
's ->
f:('a -> 's -> [ `Continue of 's | `Stop of 'b ]) ->
finish:('s -> 'b) ->
'bFolds over elements of the iterator, stopping early if the accumulator returns `Stop b, and using finish to transform the final state if the end of the iterator is reached.
Reverse 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 fold2 : ('a * 'b) t -> 'c -> f:('a -> 'b -> 'c -> 'c) -> 'cval iter2 : ('a * 'b) t -> f:('a -> 'b -> unit) -> unitmap2_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 ton_list f l is equivalent to to_list @@ f @@ of_list l.
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_seq : 'a Stdlib.Seq.t -> 'a tIterator of elements of a Seq.t.
val to_seq_persistent : 'a t -> 'a Stdlib.Seq.tConvert to a Seq. Linear in memory and time (a copy is made in memory). This does not work on infinite iterators.
val to_stack : 'a Stdlib.Stack.t -> 'a t -> unitPush elements of the iterator on the stack
val of_stack : 'a Stdlib.Stack.t -> 'a tIterator of elements of the stack (same order as Stack.iter)
val to_queue : 'a Stdlib.Queue.t -> 'a t -> unitPush elements of the iterator into the queue
val of_queue : 'a Stdlib.Queue.t -> 'a tIterator of elements contained in the queue, FIFO order
val hashtbl_add : ('a, 'b) Stdlib.Hashtbl.t -> ('a * 'b) t -> unitAdd elements of the iterator to the hashtable, with Hashtbl.add
val hashtbl_replace : ('a, 'b) Stdlib.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) Stdlib.Hashtbl.tBuild a hashtable from an iterator of key/value pairs
val of_hashtbl : ('a, 'b) Stdlib.Hashtbl.t -> ('a * 'b) tIterator of key/value pairs from the hashtable
val hashtbl_keys : ('a, 'b) Stdlib.Hashtbl.t -> 'a tval hashtbl_values : ('a, 'b) Stdlib.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.
Raised when the user tries to iterate several times on a transient iterator
val of_in_channel : Stdlib.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.
val to_buffer : char t -> Stdlib.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.
val bools : bool tIterates on true and false
module type Iterable = sig ... endConvert the given set to an iterator. The set module must be provided.
module type Addable = sig ... endConvert the iterator to a set, given the proper set module
One shot iterator using this generator. It must not be traversed twice.
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.
shuffle seq returns a perfect shuffle of seq. Uses O(length seq) memory and time. Eager.
shuffle_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 ->
(Stdlib.Format.formatter -> 'a -> unit) ->
Stdlib.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 ->
(Stdlib.Buffer.t -> 'a -> unit) ->
Stdlib.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 ... end