Helpers for the standard Seq type
See oseq for a richer API.
Sourcetype 'a iter = ('a -> unit) -> unit Sourcetype 'a gen = unit -> 'a option Sourcetype 'a equal = 'a -> 'a -> bool Sourcetype 'a ord = 'a -> 'a -> int Basics
Sourceval repeat : ?n:int -> 'a -> 'a t repeat ~n x repeats x n times then stops. If n is omitted, then x is repeated forever.
Cycle through the iterator infinitely. The iterator shouldn't be empty.
Sourceval unfold : ('b -> ('a * 'b) option) -> 'b -> 'a t unfold f acc calls f acc and:
- if
f acc = Some (x, acc'), yield x, continue with unfold f acc'. - if
f acc = None, stops.
Equality step by step. Eager.
Lexicographic comparison. Eager.
Sourceval fold : ('a -> 'b -> 'a) -> 'a -> 'b t -> 'a Sourceval fold_left : ('a -> 'b -> 'a) -> 'a -> 'b t -> 'a Sourceval iter : ('a -> unit) -> 'a t -> unit Sourceval iteri : (int -> 'a -> unit) -> 'a t -> unit Iterate with index (starts at 0).
Number of elements in the list. Will not terminate if the list if infinite: use (for instance) take to make the list finite if necessary.
Sourceval take_while : ('a -> bool) -> 'a t -> 'a t Sourceval drop_while : ('a -> bool) -> 'a t -> 'a t Sourceval map : ('a -> 'b) -> 'a t -> 'b t Sourceval mapi : (int -> 'a -> 'b) -> 'a t -> 'b t Map with index (starts at 0).
Sourceval fmap : ('a -> 'b option) -> 'a t -> 'b t Sourceval filter : ('a -> bool) -> 'a t -> 'a t Sourceval product_with : ('a -> 'b -> 'c) -> 'a t -> 'b t -> 'c t Fair product of two (possibly infinite) lists into a new list. Lazy. The first parameter is used to combine each pair of elements.
Sourceval product : 'a t -> 'b t -> ('a * 'b) t group eq l groups together consecutive elements that satisfy eq. Lazy. For instance group (=) [1;1;1;2;2;3;3;1] yields [1;1;1]; [2;2]; [3;3]; [1].
uniq eq l returns l but removes consecutive duplicates. Lazy. In other words, if several values that are equal follow one another, only the first of them is kept.
Sourceval flat_map : ('a -> 'b t) -> 'a t -> 'b t Sourceval filter_map : ('a -> 'b option) -> 'a t -> 'b t Sourceval range : int -> int -> int t Sourceval (--) : int -> int -> int t a -- b is the range of integers containing a and b (therefore, never empty).
Sourceval (--^) : int -> int -> int t a -- b is the integer range from a to b, where b is excluded.
Operations on two Collections
Sourceval fold2 : ('acc -> 'a -> 'b -> 'acc) -> 'acc -> 'a t -> 'b t -> 'acc Fold on two collections at once. Stop at soon as one of them ends.
Sourceval map2 : ('a -> 'b -> 'c) -> 'a t -> 'b t -> 'c t Map on two collections at once. Stop as soon as one of the arguments is exhausted.
Sourceval iter2 : ('a -> 'b -> unit) -> 'a t -> 'b t -> unit Iterate on two collections at once. Stop as soon as one of them ends.
Sourceval for_all2 : ('a -> 'b -> bool) -> 'a t -> 'b t -> bool Sourceval exists2 : ('a -> 'b -> bool) -> 'a t -> 'b t -> bool Merge two sorted iterators into a sorted iterator.
Combine elements pairwise. Stop as soon as one of the lists stops.
Split each tuple in the list.
Misc
Eager sort. Require the iterator to be finite. O(n ln(n)) time and space.
Eager sort that removes duplicate values. Require the iterator to be finite. O(n ln(n)) time and space.
Avoid recomputations by caching intermediate results.
Fair Combinations
Fair interleaving of both streams.
Sourceval fair_flat_map : ('a -> 'b t) -> 'a t -> 'b t Sourceval fair_app : ('a -> 'b) t -> 'a t -> 'b t Implementations
Sourceval (>>=) : 'a t -> ('a -> 'b t) -> 'b t Sourceval (>|=) : 'a t -> ('a -> 'b) -> 'b t Sourceval (<*>) : ('a -> 'b) t -> 'a t -> 'b t Sourceval (>>-) : 'a t -> ('a -> 'b t) -> 'b t Sourceval (<.>) : ('a -> 'b) t -> 'a t -> 'b t Infix operators
Conversions
Sourceval of_list : 'a list -> 'a t Sourceval to_list : 'a t -> 'a list Gather all values into a list.
Sourceval of_array : 'a array -> 'a t Sourceval to_array : 'a t -> 'a array Convert into array. Iterate twice.
Sourceval to_rev_list : 'a t -> 'a list Convert to a list, in reverse order. More efficient than to_list.
of_gen g consumes the generator and caches intermediate results.
IO
pp ~pp_start ~pp_stop ~pp_sep pp_item ppf s formats the sequence s on ppf. Each element is formatted with pp_item, pp_start is called at the beginning, pp_stop is called at the end, pp_sep is called between each elements. By defaults pp_start and pp_stop does nothing and pp_sep defaults to (fun out -> Format.fprintf out ",@ ").