CCSeq
Helpers for the standard Seq type
See oseq for a richer API.
type !'a t = unit -> 'a node
val find_index : ('a -> bool) -> 'a t -> int option
val find_mapi : (int -> 'a -> 'b option) -> 'a t -> 'b option
val of_dispenser : (unit -> 'a option) -> 'a t
val to_dispenser : 'a t -> unit -> 'a option
val ints : int -> int t
val nil : 'a t
val empty : 'a t
val singleton : 'a -> 'a t
val init : int -> (int -> 'a) -> 'a t
init n f
corresponds to the sequence f 0; f 1; ...; f (n-1)
.
val repeat : ?n:int -> 'a -> 'a t
repeat ~n x
repeats x
n
times then stops. If n
is omitted, then x
is repeated forever.
val forever : (unit -> 'a) -> 'a t
forever f
corresponds to the infinite sequence containing all the f ()
.
val iterate : ('a -> 'a) -> 'a -> 'a t
iterate f a
corresponds to the infinite sequence containing a
, f a
, f (f a)
, ...
val unfold : ('b -> ('a * 'b) option) -> 'b -> 'a t
unfold f acc
calls f acc
and:
f acc = Some (x, acc')
, yield x
, continue with unfold f acc'
.f acc = None
, stops.val is_empty : 'a t -> bool
is_empty xs
checks in the sequence xs
is empty
val head : 'a t -> 'a option
Head of the list.
val fold : ('a -> 'b -> 'a) -> 'a -> 'b t -> 'a
Fold on values.
val foldi : ('a -> int -> 'b -> 'a) -> 'a -> 'b t -> 'a
fold_lefti f init xs
applies f acc i x
where acc
is the result of the previous computation or init
for the first one, i
is the index in the sequence (starts at 0) and x
is the element of the sequence.
val iter : ('a -> unit) -> 'a t -> unit
val iteri : (int -> 'a -> unit) -> 'a t -> unit
Iterate with index (starts at 0).
val length : _ t -> int
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.
Fair product of two (possibly infinite) lists into a new list. Lazy. The first parameter is used to combine each pair of elements.
Alias of product_with
.
Specialization of product_with
producing tuples.
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.
val for_all : ('a -> bool) -> 'a t -> bool
for_all p [a1; ...; an]
checks if all elements of the sequence satisfy the predicate p
. That is, it returns (p a1) && ... && (p an)
for a non-empty list and true
if the sequence is empty. It consumes the sequence until it finds an element not satisfying the predicate.
val exists : ('a -> bool) -> 'a t -> bool
exists p [a1; ...; an]
checks if at least one element of the sequence satisfies the predicate p
. That is, it returns (p a1) || ... || (p an)
for a non-empty sequence and false
if the list is empty. It consumes the sequence until it finds an element satisfying the predicate.
val find : ('a -> bool) -> 'a t -> 'a option
find p [a1; ...; an]
return Some ai
for the first ai
satisfying the predicate p
and return None
otherwise.
val find_map : ('a -> 'b option) -> 'a t -> 'b option
find f [a1; ...; an]
return Some (f ai)
for the first ai
such that f ai = Some _
and return None
otherwise.
scan f init xs
is the sequence containing the intermediate result of fold f init xs
.
val range : int -> int -> int t
val (--) : int -> int -> int t
a -- b
is the range of integers containing a
and b
(therefore, never empty).
val (--^) : int -> int -> int t
a -- b
is the integer range from a
to b
, where b
is excluded.
Fold on two collections at once. Stop as soon as one of them ends.
Map on two collections at once. Stop as soon as one of the arguments is exhausted.
Iterate on two collections at once. Stop as soon as one of them ends.
Combine elements pairwise. Stop as soon as one of the lists stops.
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.
val return : 'a -> 'a t
val pure : 'a -> 'a t
Infix version of fair_flat_map
.
module Infix : sig ... end
module type MONAD = sig ... end
val of_list : 'a list -> 'a t
val to_list : 'a t -> 'a list
Gather all values into a list.
val of_array : 'a array -> 'a t
Iterate on the array.
val to_array : 'a t -> 'a array
Convert into array.
val of_string : string -> char t
Iterate on characters.
val pp :
?pp_start:unit printer ->
?pp_stop:unit printer ->
?pp_sep:unit printer ->
'a printer ->
'a t printer
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 ",@ ").