Module CCKList
Continuation List
type 'a sequence
= ('a -> unit) -> unit
type 'a gen
= unit -> 'a option
type 'a equal
= 'a -> 'a -> bool
type 'a ord
= 'a -> 'a -> int
type 'a printer
= Stdlib.Format.formatter -> 'a -> unit
Basics
type +'a t
= unit -> [ `Nil | `Cons of 'a * 'a t ]
val nil : 'a t
val empty : 'a t
val cons : 'a -> 'a t -> 'a t
val singleton : 'a -> 'a t
val repeat : ?n:int -> 'a -> 'a t
repeat ~n x
repeatsx
n
times then stops. Ifn
is omitted, thenx
is repeated forever.- since
- 0.3.3
val cycle : 'a t -> 'a t
Cycle through the iterator infinitely. The iterator shouldn't be empty.
- since
- 0.3.3
val unfold : ('b -> ('a * 'b) option) -> 'b -> 'a t
unfold f acc
callsf acc
and:- if
f acc = Some (x, acc')
, yieldx
, continue withunfold f acc'
. - if
f acc = None
, stops.
- since
- 0.13
- if
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).
- since
- 0.13
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.
val take : int -> 'a t -> 'a t
val take_while : ('a -> bool) -> 'a t -> 'a t
val drop : int -> 'a t -> 'a t
val drop_while : ('a -> bool) -> 'a t -> 'a t
val map : ('a -> 'b) -> 'a t -> 'b t
val mapi : (int -> 'a -> 'b) -> 'a t -> 'b t
Map with index (starts at 0).
- since
- 0.13
val fmap : ('a -> 'b option) -> 'a t -> 'b t
val filter : ('a -> bool) -> 'a t -> 'a t
val append : 'a t -> 'a t -> 'a t
val 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.
- since
- 0.3.3
val product : 'a t -> 'b t -> ('a * 'b) t
Specialization of
product_with
producing tuples.- since
- 0.3.3
val group : 'a equal -> 'a t -> 'a t t
group eq l
groups together consecutive elements that satisfyeq
. Lazy. For instancegroup (=) [1;1;1;2;2;3;3;1]
yields[1;1;1]; [2;2]; [3;3]; [1]
.- since
- 0.3.3
val uniq : 'a equal -> 'a t -> 'a t
uniq eq l
returnsl
but removes consecutive duplicates. Lazy. In other words, if several values that are equal follow one another, only the first of them is kept.- since
- 0.3.3
val flat_map : ('a -> 'b t) -> 'a t -> 'b t
val filter_map : ('a -> 'b option) -> 'a t -> 'b t
val flatten : 'a t t -> 'a t
val range : int -> int -> int t
val (--) : int -> int -> int t
a -- b
is the range of integers containinga
andb
(therefore, never empty).
val (--^) : int -> int -> int t
a -- b
is the integer range froma
tob
, whereb
is excluded.- since
- 0.17
Operations on two Collections
val 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.
val 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.
val iter2 : ('a -> 'b -> unit) -> 'a t -> 'b t -> unit
Iterate on two collections at once. Stop as soon as one of them ends.
val for_all2 : ('a -> 'b -> bool) -> 'a t -> 'b t -> bool
val exists2 : ('a -> 'b -> bool) -> 'a t -> 'b t -> bool
val merge : 'a ord -> 'a t -> 'a t -> 'a t
Merge two sorted iterators into a sorted iterator.
Misc
val sort : cmp:'a ord -> 'a t -> 'a t
Eager sort. Require the iterator to be finite.
O(n ln(n))
time and space.- since
- 0.3.3
Fair Combinations
Implementations
- since
- 0.3.3
Infix operators
- since
- 0.17
module Infix : sig ... end
module type MONAD = sig ... end
Conversions
val of_array : 'a array -> 'a t
Iterate on the array.
- since
- 0.13
val to_array : 'a t -> 'a array
Convert into array. Iterate twice.
- since
- 0.13