diff --git a/dev/iter/Iter/index.html b/dev/iter/Iter/index.html index 41300e5..6424890 100644 --- a/dev/iter/Iter/index.html +++ b/dev/iter/Iter/index.html @@ -1,5 +1,5 @@ -Iter (iter.Iter)

Module Iter

Simple and Efficient Iterators.

The iterators are designed to allow easy transfer (mappings) between data structures, without defining n^2 conversions between the n types. The implementation relies on the assumption that 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 Seq, 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) -> unit

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.

type +'a iter = 'a t
type 'a equal = 'a -> 'a -> bool
type 'a hash = 'a -> int

Creation

val from_iter : (('a -> unit) -> unit) -> 'a t

Build an iterator from a iter function

val from_labelled_iter : (f:('a -> unit) -> unit) -> 'a t

Build an iterator from a labelled iter function

  • since 1.2
val from_fun : (unit -> 'a option) -> 'a t

Call the function repeatedly until it returns None. This iterator is transient, use persistent if needed!

val empty : 'a t

Empty iterator. It contains no element.

val singleton : 'a -> 'a t

Singleton iterator, with exactly one element.

val doubleton : 'a -> 'a -> 'a t

Iterator with exactly two elements

val init : (int -> 'a) -> 'a t

init f is the infinite iterator f 0; f 1; f 2; ….

  • since 0.9
val cons : 'a -> 'a t -> 'a t

cons x l yields x, then yields from l. Same as append (singleton x) l.

Caution: it is advised not to build long iterators out of cons, because it's inefficient. Each additional cons x i adds one layer of function call per item traversed in i.

val snoc : 'a t -> 'a -> 'a t

Same as cons but yields the element after iterating on l.

val return : 'a -> 'a t

Synonym to singleton

val pure : 'a -> 'a t

Synonym to singleton

val repeat : 'a -> 'a t

Infinite 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 t

iterate f x is the infinite iterator x, f(x), f(f(x)), ...

val forever : (unit -> 'b) -> 'b t

Iterator 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 t

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 unfoldr : ('b -> ('a * 'b) option) -> 'b -> 'a t

unfoldr f b will apply f to b. If it yields Some (x,b') then x is returned and unfoldr recurses with b'.

val scan : ('b -> 'a -> 'b) -> 'b -> 'a t -> 'b t

Iterator of intermediate results

Consumption

val iter : ('a -> unit) -> 'a t -> unit

Consume the iterator, passing all its arguments to the function. Basically iter f seq is just seq f.

val iteri : (int -> 'a -> unit) -> 'a t -> unit

Iterate on elements and their index in the iterator

val for_each : 'a t -> ('a -> unit) -> unit

Consume 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.

  • since 1.4
val for_eachi : 'a t -> (int -> 'a -> unit) -> unit

Iterate 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.

  • since 1.4
val fold : ('a -> 'b -> 'a) -> 'a -> 'b t -> 'a

Fold over elements of the iterator, consuming it

val foldi : ('a -> int -> 'b -> 'a) -> 'a -> 'b t -> 'a

Fold over elements of the iterator and their index, consuming it

val fold_map : ('acc -> 'a -> 'acc * 'b) -> 'acc -> 'a t -> 'b t

fold_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.

  • since 0.9
val fold_filter_map : ('acc -> 'a -> 'acc * 'b option) -> 'acc -> 'a t -> 'b t

fold_filter_map f acc l is a fold_map-like function, but the function can choose to skip an element by retuning None.

  • since 0.9
val map : ('a -> 'b) -> 'a t -> 'b t

Map objects of the iterator into other elements, lazily

val mapi : (int -> 'a -> 'b) -> 'a t -> 'b t

Map objects, along with their index in the iterator

val map_by_2 : ('a -> 'a -> 'a) -> 'a t -> 'a t

Map objects two by two. lazily. The last element is kept in the iterator if the count is odd.

  • since 0.7
val for_all : ('a -> bool) -> 'a t -> bool

Do all elements satisfy the predicate?

val exists : ('a -> bool) -> 'a t -> bool

Exists there some element satisfying the predicate?

val mem : ?eq:('a -> 'a -> bool) -> 'a -> 'a t -> bool

Is the value a member of the iterator?

  • parameter eq

    the equality predicate to use (default (=))

  • since 0.5
val find : ('a -> 'b option) -> 'a t -> 'b option

Find the first element on which the function doesn't return None

  • since 0.5
val find_map : ('a -> 'b option) -> 'a t -> 'b option

Alias to find

  • since 0.10
val findi : (int -> 'a -> 'b option) -> 'a t -> 'b option

Indexed version of find

  • since 0.9
val find_mapi : (int -> 'a -> 'b option) -> 'a t -> 'b option

Alias to findi

  • since 0.10
val find_pred : ('a -> bool) -> 'a t -> 'a option

find_pred p l finds the first element of l that satisfies p, or returns None if no element satisfies p

  • since 0.9
val find_pred_exn : ('a -> bool) -> 'a t -> 'a

Unsafe version of find_pred

  • raises Not_found

    if no such element is found

  • since 0.9
val length : 'a t -> int

How long is the iterator? Forces the iterator.

val is_empty : 'a t -> bool

Is the iterator empty? Forces the iterator.

Transformation

val filter : ('a -> bool) -> 'a t -> 'a t

Filter on elements of the iterator

val append : 'a t -> 'a t -> 'a t

Append two iterators. Iterating on the result is like iterating on the first, then on the second.

val append_l : 'a t list -> 'a t

Append iterators. Iterating on the result is like iterating on the each iterator of the list in order.

  • since 0.11
val concat : 'a t t -> 'a t

Concatenate an iterator of iterators into one iterator.

val flatten : 'a t t -> 'a t

Alias for concat

val flat_map : ('a -> 'b t) -> 'a t -> 'b t

Monadic bind. Intuitively, it applies the function to every element of the initial iterator, and calls concat. Formerly flatMap

  • since 0.5
val flat_map_l : ('a -> 'b list) -> 'a t -> 'b t

Convenience function combining flat_map and of_list

  • since 0.9
val seq_list : 'a t list -> 'a list t

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.

  • since 0.11
val seq_list_map : ('a -> 'b t) -> 'a list -> 'b list t

seq_list_map f l maps f over every element of l, then calls seq_list

  • since 0.11
val filter_map : ('a -> 'b option) -> 'a t -> 'b t

Map and only keep non-None elements Formerly fmap

  • since 0.5
val filter_mapi : (int -> 'a -> 'b option) -> 'a t -> 'b t

Map with indices, and only keep non-None elements

  • since 0.11
val filter_count : ('a -> bool) -> 'a t -> int

Count how many elements satisfy the given predicate

  • since 1.0
val intersperse : 'a -> 'a t -> 'a t

Insert the single element between every element of the iterator

val keep_some : 'a option t -> 'a t

filter_some l retains only elements of the form Some x. Same as filter_map (fun x->x)

  • since 1.0
val keep_ok : ('a, _) Result.result t -> 'a t

keep_ok l retains only elements of the form Ok x.

  • since 1.0
val keep_error : (_, 'e) Result.result t -> 'e t

keep_error l retains only elements of the form Error x.

  • since 1.0

Caching

val persistent : 'a t -> 'a t

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!

val persistent_lazy : 'a t -> 'a t

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.

Misc

val sort : ?cmp:('a -> 'a -> int) -> 'a t -> 'a t

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.

val sort_uniq : ?cmp:('a -> 'a -> int) -> 'a t -> 'a t

Sort the iterator and remove duplicates. Eager, same as sort

val sorted : ?cmp:('a -> 'a -> int) -> 'a t -> bool

Checks whether the iterator is sorted. Eager, same as sort.

  • since 0.9
val group_succ_by : ?eq:('a -> 'a -> bool) -> 'a t -> 'a list t

Group equal consecutive elements. Linear time. Formerly synonym to group. note: Order of items in each list is unspecified.

  • since 0.6
val group_by : ?hash:('a -> int) -> ?eq:('a -> 'a -> bool) -> 'a t -> 'a list t

Group equal elements, disregarding their order of appearance. precondition: for any x and y, if eq x y then hash x=hash y must hold. note: Order of items in each list is unspecified.

  • since 0.6
val count : ?hash:('a -> int) -> ?eq:('a -> 'a -> bool) -> 'a t -> ('a * int) t

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.

  • since 0.10
val uniq : ?eq:('a -> 'a -> bool) -> 'a t -> 'a t

Remove consecutive duplicate elements. Basically this is like fun seq -> map List.hd (group seq).

val product : 'a t -> 'b t -> ('a * 'b) t

Cartesian 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) t

All 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.

  • since 0.9
val diagonal : 'a t -> ('a * 'a) t

All pairs of distinct positions of the iterator. Iterates only once on the iterator, which must be finite.

  • since 0.9
val join : join_row:('a -> 'b -> 'c option) -> 'a t -> 'b t -> 'c t

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 : +Iter (iter.Iter)

Module Iter

Simple and Efficient Iterators.

The iterators are designed to allow easy transfer (mappings) between data structures, without defining n^2 conversions between the n types. The implementation relies on the assumption that 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 Seq, 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) -> unit

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.

type +'a iter = 'a t
type 'a equal = 'a -> 'a -> bool
type 'a hash = 'a -> int

Creation

val from_iter : (('a -> unit) -> unit) -> 'a t

Build an iterator from a iter function

val from_labelled_iter : (f:('a -> unit) -> unit) -> 'a t

Build an iterator from a labelled iter function

  • since 1.2
val from_fun : (unit -> 'a option) -> 'a t

Call the function repeatedly until it returns None. This iterator is transient, use persistent if needed!

val empty : 'a t

Empty iterator. It contains no element.

val singleton : 'a -> 'a t

Singleton iterator, with exactly one element.

val doubleton : 'a -> 'a -> 'a t

Iterator with exactly two elements

val init : (int -> 'a) -> 'a t

init f is the infinite iterator f 0; f 1; f 2; ….

  • since 0.9
val cons : 'a -> 'a t -> 'a t

cons x l yields x, then yields from l. Same as append (singleton x) l.

Caution: it is advised not to build long iterators out of cons, because it's inefficient. Each additional cons x i adds one layer of function call per item traversed in i.

val snoc : 'a t -> 'a -> 'a t

Same as cons but yields the element after iterating on l.

val return : 'a -> 'a t

Synonym to singleton

val pure : 'a -> 'a t

Synonym to singleton

val repeat : 'a -> 'a t

Infinite 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 t

iterate f x is the infinite iterator x, f(x), f(f(x)), ...

val forever : (unit -> 'b) -> 'b t

Iterator 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 t

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 unfoldr : ('b -> ('a * 'b) option) -> 'b -> 'a t

unfoldr f b will apply f to b. If it yields Some (x,b') then x is returned and unfoldr recurses with b'.

val scan : ('b -> 'a -> 'b) -> 'b -> 'a t -> 'b t

Iterator of intermediate results

Consumption

val iter : ('a -> unit) -> 'a t -> unit

Consume the iterator, passing all its arguments to the function. Basically iter f seq is just seq f.

val iteri : (int -> 'a -> unit) -> 'a t -> unit

Iterate on elements and their index in the iterator

val for_each : 'a t -> ('a -> unit) -> unit

Consume 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.

  • since 1.4
val for_eachi : 'a t -> (int -> 'a -> unit) -> unit

Iterate 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.

  • since 1.4
val fold : ('a -> 'b -> 'a) -> 'a -> 'b t -> 'a

Fold over elements of the iterator, consuming it

val foldi : ('a -> int -> 'b -> 'a) -> 'a -> 'b t -> 'a

Fold over elements of the iterator and their index, consuming it

val fold_map : ('acc -> 'a -> 'acc * 'b) -> 'acc -> 'a t -> 'b t

fold_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.

  • since 0.9
val fold_filter_map : ('acc -> 'a -> 'acc * 'b option) -> 'acc -> 'a t -> 'b t

fold_filter_map f acc l is a fold_map-like function, but the function can choose to skip an element by retuning None.

  • since 0.9
val map : ('a -> 'b) -> 'a t -> 'b t

Map objects of the iterator into other elements, lazily

val mapi : (int -> 'a -> 'b) -> 'a t -> 'b t

Map objects, along with their index in the iterator

val map_by_2 : ('a -> 'a -> 'a) -> 'a t -> 'a t

Map objects two by two. lazily. The last element is kept in the iterator if the count is odd.

  • since 0.7
val for_all : ('a -> bool) -> 'a t -> bool

Do all elements satisfy the predicate?

val exists : ('a -> bool) -> 'a t -> bool

Exists there some element satisfying the predicate?

val mem : ?eq:('a -> 'a -> bool) -> 'a -> 'a t -> bool

Is the value a member of the iterator?

  • parameter eq

    the equality predicate to use (default (=))

  • since 0.5
val find : ('a -> 'b option) -> 'a t -> 'b option

Find the first element on which the function doesn't return None

  • since 0.5
val find_map : ('a -> 'b option) -> 'a t -> 'b option

Alias to find

  • since 0.10
val findi : (int -> 'a -> 'b option) -> 'a t -> 'b option

Indexed version of find

  • since 0.9
val find_mapi : (int -> 'a -> 'b option) -> 'a t -> 'b option

Alias to findi

  • since 0.10
val find_pred : ('a -> bool) -> 'a t -> 'a option

find_pred p l finds the first element of l that satisfies p, or returns None if no element satisfies p

  • since 0.9
val find_pred_exn : ('a -> bool) -> 'a t -> 'a

Unsafe version of find_pred

  • raises Not_found

    if no such element is found

  • since 0.9
val length : 'a t -> int

How long is the iterator? Forces the iterator.

val is_empty : 'a t -> bool

Is the iterator empty? Forces the iterator.

Transformation

val filter : ('a -> bool) -> 'a t -> 'a t

Filter on elements of the iterator

val append : 'a t -> 'a t -> 'a t

Append two iterators. Iterating on the result is like iterating on the first, then on the second.

val append_l : 'a t list -> 'a t

Append iterators. Iterating on the result is like iterating on the each iterator of the list in order.

  • since 0.11
val concat : 'a t t -> 'a t

Concatenate an iterator of iterators into one iterator.

val flatten : 'a t t -> 'a t

Alias for concat

val flat_map : ('a -> 'b t) -> 'a t -> 'b t

Monadic bind. Intuitively, it applies the function to every element of the initial iterator, and calls concat. Formerly flatMap

  • since 0.5
val flat_map_l : ('a -> 'b list) -> 'a t -> 'b t

Convenience function combining flat_map and of_list

  • since 0.9
val seq_list : 'a t list -> 'a list t

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.

  • since 0.11
val seq_list_map : ('a -> 'b t) -> 'a list -> 'b list t

seq_list_map f l maps f over every element of l, then calls seq_list

  • since 0.11
val filter_map : ('a -> 'b option) -> 'a t -> 'b t

Map and only keep non-None elements Formerly fmap

  • since 0.5
val filter_mapi : (int -> 'a -> 'b option) -> 'a t -> 'b t

Map with indices, and only keep non-None elements

  • since 0.11
val filter_count : ('a -> bool) -> 'a t -> int

Count how many elements satisfy the given predicate

  • since 1.0
val intersperse : 'a -> 'a t -> 'a t

Insert the single element between every element of the iterator

val keep_some : 'a option t -> 'a t

filter_some l retains only elements of the form Some x. Same as filter_map (fun x->x)

  • since 1.0
val keep_ok : ('a, _) Stdlib.result t -> 'a t

keep_ok l retains only elements of the form Ok x.

  • since 1.0
val keep_error : (_, 'e) Stdlib.result t -> 'e t

keep_error l retains only elements of the form Error x.

  • since 1.0

Caching

val persistent : 'a t -> 'a t

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!

val persistent_lazy : 'a t -> 'a t

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.

Misc

val sort : ?cmp:('a -> 'a -> int) -> 'a t -> 'a t

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.

val sort_uniq : ?cmp:('a -> 'a -> int) -> 'a t -> 'a t

Sort the iterator and remove duplicates. Eager, same as sort

val sorted : ?cmp:('a -> 'a -> int) -> 'a t -> bool

Checks whether the iterator is sorted. Eager, same as sort.

  • since 0.9
val group_succ_by : ?eq:('a -> 'a -> bool) -> 'a t -> 'a list t

Group equal consecutive elements. Linear time. Formerly synonym to group. note: Order of items in each list is unspecified.

  • since 0.6
val group_by : ?hash:('a -> int) -> ?eq:('a -> 'a -> bool) -> 'a t -> 'a list t

Group equal elements, disregarding their order of appearance. precondition: for any x and y, if eq x y then hash x=hash y must hold. note: Order of items in each list is unspecified.

  • since 0.6
val count : ?hash:('a -> int) -> ?eq:('a -> 'a -> bool) -> 'a t -> ('a * int) t

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.

  • since 0.10
val uniq : ?eq:('a -> 'a -> bool) -> 'a t -> 'a t

Remove consecutive duplicate elements. Basically this is like fun seq -> map List.hd (group seq).

val product : 'a t -> 'b t -> ('a * 'b) t

Cartesian 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) t

All 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.

  • since 0.9
val diagonal : 'a t -> ('a * 'a) t

All pairs of distinct positions of the iterator. Iterates only once on the iterator, which must be finite.

  • since 0.9
val join : join_row:('a -> 'b -> 'c option) -> 'a t -> 'b t -> 'c t

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) -> diff --git a/dev/iter/IterLabels/index.html b/dev/iter/IterLabels/index.html index 6d4c16c..89fbf11 100644 --- a/dev/iter/IterLabels/index.html +++ b/dev/iter/IterLabels/index.html @@ -3,7 +3,7 @@ f:('acc -> 'a -> 'acc * 'b option) -> init:'acc -> 'a t -> - 'b t

fold_filter_map f acc l is a fold_map-like function, but the function can choose to skip an element by retuning None.

  • since 0.9
val map : f:('a -> 'b) -> 'a t -> 'b t

Map objects of the iterator into other elements, lazily

val mapi : f:(int -> 'a -> 'b) -> 'a t -> 'b t

Map objects, along with their index in the iterator

val map_by_2 : f:('a -> 'a -> 'a) -> 'a t -> 'a t

Map objects two by two. lazily. The last element is kept in the iterator if the count is odd.

  • since 0.7
val for_all : f:('a -> bool) -> 'a t -> bool

Do all elements satisfy the predicate?

val exists : f:('a -> bool) -> 'a t -> bool

Exists there some element satisfying the predicate?

val mem : ?eq:('a -> 'a -> bool) -> x:'a -> 'a t -> bool

Is the value a member of the iterator?

  • parameter eq

    the equality predicate to use (default (=))

  • since 0.5
val find : ('a -> 'b option) -> 'a t -> 'b option

Find the first element on which the function doesn't return None

  • since 0.5
val find_map : f:('a -> 'b option) -> 'a t -> 'b option

Alias to find

  • since 0.10
val findi : f:(int -> 'a -> 'b option) -> 'a t -> 'b option

Indexed version of find

  • since 0.9
val find_mapi : f:(int -> 'a -> 'b option) -> 'a t -> 'b option

Alias to findi

  • since 0.10
val find_pred : f:('a -> bool) -> 'a t -> 'a option

find_pred p l finds the first element of l that satisfies p, or returns None if no element satisfies p

  • since 0.9
val find_pred_exn : f:('a -> bool) -> 'a t -> 'a

Unsafe version of find_pred

  • raises Not_found

    if no such element is found

  • since 0.9
val length : 'a t -> int

How long is the iterator? Forces the iterator.

val is_empty : 'a t -> bool

Is the iterator empty? Forces the iterator.

Transformation

val filter : f:('a -> bool) -> 'a t -> 'a t

Filter on elements of the iterator

val append : 'a t -> 'a t -> 'a t

Append two iterators. Iterating on the result is like iterating on the first, then on the second.

val append_l : 'a t list -> 'a t

Append iterators. Iterating on the result is like iterating on the each iterator of the list in order.

  • since 0.11
val concat : 'a t t -> 'a t

Concatenate an iterator of iterators into one iterator.

val flatten : 'a t t -> 'a t

Alias for concat

val flat_map : f:('a -> 'b t) -> 'a t -> 'b t

Alias to flatMap with a more explicit name

val flat_map_l : f:('a -> 'b list) -> 'a t -> 'b t

Convenience function combining flat_map and of_list

  • since 0.9
val seq_list : 'a t list -> 'a list t

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.

  • since 0.11
val seq_list_map : f:('a -> 'b t) -> 'a list -> 'b list t

seq_list_map f l maps f over every element of l, then calls seq_list

  • since 0.11
val filter_map : f:('a -> 'b option) -> 'a t -> 'b t

Map and only keep non-None elements Formerly fmap

val filter_mapi : f:(int -> 'a -> 'b option) -> 'a t -> 'b t

Map with indices, and only keep non-None elements

  • since 0.11
val filter_count : f:('a -> bool) -> 'a t -> int

Count how many elements satisfy the given predicate

  • since 1.0
val intersperse : x:'a -> 'a t -> 'a t

Insert the single element between every element of the iterator

val keep_some : 'a option t -> 'a t

filter_some l retains only elements of the form Some x. Same as filter_map (fun x->x)

  • since 1.0
val keep_ok : ('a, _) Result.result t -> 'a t

keep_ok l retains only elements of the form Ok x.

  • since 1.0
val keep_error : (_, 'e) Result.result t -> 'e t

keep_error l retains only elements of the form Error x.

  • since 1.0

Caching

val persistent : 'a t -> 'a t

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!

val persistent_lazy : 'a t -> 'a t

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.

Misc

val sort : ?cmp:('a -> 'a -> int) -> 'a t -> 'a t

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.

val sort_uniq : ?cmp:('a -> 'a -> int) -> 'a t -> 'a t

Sort the iterator and remove duplicates. Eager, same as sort

val sorted : ?cmp:('a -> 'a -> int) -> 'a t -> bool

Checks whether the iterator is sorted. Eager, same as sort.

  • since 0.9
val group_succ_by : ?eq:('a -> 'a -> bool) -> 'a t -> 'a list t

Group equal consecutive elements. Formerly synonym to group.

  • since 0.6
val group_by : ?hash:('a -> int) -> ?eq:('a -> 'a -> bool) -> 'a t -> 'a list t

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.

  • since 0.6
val count : ?hash:('a -> int) -> ?eq:('a -> 'a -> bool) -> 'a t -> ('a * int) t

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)

  • since 0.10
val uniq : ?eq:('a -> 'a -> bool) -> 'a t -> 'a t

Remove consecutive duplicate elements. Basically this is like fun seq -> map List.hd (group seq).

val product : 'a t -> 'b t -> ('a * 'b) t

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) t

All 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.

  • since 0.9
val diagonal : 'a t -> ('a * 'a) t

All pairs of distinct positions of the iterator. Iterates only once on the iterator, which must be finite.

  • since 0.9
val join : join_row:('a -> 'b -> 'c option) -> 'a t -> 'b t -> 'c t

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 : + 'b t

fold_filter_map f acc l is a fold_map-like function, but the function can choose to skip an element by retuning None.

  • since 0.9
val map : f:('a -> 'b) -> 'a t -> 'b t

Map objects of the iterator into other elements, lazily

val mapi : f:(int -> 'a -> 'b) -> 'a t -> 'b t

Map objects, along with their index in the iterator

val map_by_2 : f:('a -> 'a -> 'a) -> 'a t -> 'a t

Map objects two by two. lazily. The last element is kept in the iterator if the count is odd.

  • since 0.7
val for_all : f:('a -> bool) -> 'a t -> bool

Do all elements satisfy the predicate?

val exists : f:('a -> bool) -> 'a t -> bool

Exists there some element satisfying the predicate?

val mem : ?eq:('a -> 'a -> bool) -> x:'a -> 'a t -> bool

Is the value a member of the iterator?

  • parameter eq

    the equality predicate to use (default (=))

  • since 0.5
val find : ('a -> 'b option) -> 'a t -> 'b option

Find the first element on which the function doesn't return None

  • since 0.5
val find_map : f:('a -> 'b option) -> 'a t -> 'b option

Alias to find

  • since 0.10
val findi : f:(int -> 'a -> 'b option) -> 'a t -> 'b option

Indexed version of find

  • since 0.9
val find_mapi : f:(int -> 'a -> 'b option) -> 'a t -> 'b option

Alias to findi

  • since 0.10
val find_pred : f:('a -> bool) -> 'a t -> 'a option

find_pred p l finds the first element of l that satisfies p, or returns None if no element satisfies p

  • since 0.9
val find_pred_exn : f:('a -> bool) -> 'a t -> 'a

Unsafe version of find_pred

  • raises Not_found

    if no such element is found

  • since 0.9
val length : 'a t -> int

How long is the iterator? Forces the iterator.

val is_empty : 'a t -> bool

Is the iterator empty? Forces the iterator.

Transformation

val filter : f:('a -> bool) -> 'a t -> 'a t

Filter on elements of the iterator

val append : 'a t -> 'a t -> 'a t

Append two iterators. Iterating on the result is like iterating on the first, then on the second.

val append_l : 'a t list -> 'a t

Append iterators. Iterating on the result is like iterating on the each iterator of the list in order.

  • since 0.11
val concat : 'a t t -> 'a t

Concatenate an iterator of iterators into one iterator.

val flatten : 'a t t -> 'a t

Alias for concat

val flat_map : f:('a -> 'b t) -> 'a t -> 'b t

Alias to flatMap with a more explicit name

val flat_map_l : f:('a -> 'b list) -> 'a t -> 'b t

Convenience function combining flat_map and of_list

  • since 0.9
val seq_list : 'a t list -> 'a list t

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.

  • since 0.11
val seq_list_map : f:('a -> 'b t) -> 'a list -> 'b list t

seq_list_map f l maps f over every element of l, then calls seq_list

  • since 0.11
val filter_map : f:('a -> 'b option) -> 'a t -> 'b t

Map and only keep non-None elements Formerly fmap

val filter_mapi : f:(int -> 'a -> 'b option) -> 'a t -> 'b t

Map with indices, and only keep non-None elements

  • since 0.11
val filter_count : f:('a -> bool) -> 'a t -> int

Count how many elements satisfy the given predicate

  • since 1.0
val intersperse : x:'a -> 'a t -> 'a t

Insert the single element between every element of the iterator

val keep_some : 'a option t -> 'a t

filter_some l retains only elements of the form Some x. Same as filter_map (fun x->x)

  • since 1.0
val keep_ok : ('a, _) Stdlib.result t -> 'a t

keep_ok l retains only elements of the form Ok x.

  • since 1.0
val keep_error : (_, 'e) Stdlib.result t -> 'e t

keep_error l retains only elements of the form Error x.

  • since 1.0

Caching

val persistent : 'a t -> 'a t

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!

val persistent_lazy : 'a t -> 'a t

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.

Misc

val sort : ?cmp:('a -> 'a -> int) -> 'a t -> 'a t

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.

val sort_uniq : ?cmp:('a -> 'a -> int) -> 'a t -> 'a t

Sort the iterator and remove duplicates. Eager, same as sort

val sorted : ?cmp:('a -> 'a -> int) -> 'a t -> bool

Checks whether the iterator is sorted. Eager, same as sort.

  • since 0.9
val group_succ_by : ?eq:('a -> 'a -> bool) -> 'a t -> 'a list t

Group equal consecutive elements. Formerly synonym to group.

  • since 0.6
val group_by : ?hash:('a -> int) -> ?eq:('a -> 'a -> bool) -> 'a t -> 'a list t

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.

  • since 0.6
val count : ?hash:('a -> int) -> ?eq:('a -> 'a -> bool) -> 'a t -> ('a * int) t

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)

  • since 0.10
val uniq : ?eq:('a -> 'a -> bool) -> 'a t -> 'a t

Remove consecutive duplicate elements. Basically this is like fun seq -> map List.hd (group seq).

val product : 'a t -> 'b t -> ('a * 'b) t

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) t

All 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.

  • since 0.9
val diagonal : 'a t -> ('a * 'a) t

All pairs of distinct positions of the iterator. Iterates only once on the iterator, which must be finite.

  • since 0.9
val join : join_row:('a -> 'b -> 'c option) -> 'a t -> 'b t -> 'c t

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) ->