Library
Module
Module type
Parameter
Class
Class type
Continuation List
type 'a printer = Format.formatter -> 'a -> unit
Basics
type +'a t = unit -> [ `Nil | `Cons of 'a * 'a t ]
val nil : 'a t
val empty : 'a t
val singleton : 'a -> 'a t
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 unfold : ('b -> ('a * 'b) option) -> 'b -> 'a t
unfold f acc
calls f acc
and:
- if
f acc = Some (x, acc')
, yieldx
, continue withunfold f acc'
. - if
f acc = None
, stops.
val is_empty : 'a t -> bool
val head : 'a t -> 'a option
Head of the list.
val fold : ('a -> 'b -> 'a) -> 'a -> 'b t -> 'a
Fold on values.
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.
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 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.
Operations on two Collections
Fold on two collections at once. Stop at 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.
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.
Fair Combinations
Implementations
val return : 'a -> 'a t
val pure : 'a -> 'a t
Infix version of fair_flat_map
.
Infix operators
module Infix : sig ... end
module type MONAD = sig ... end
Conversions
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. Iterate twice.