Module CCListLabels
Complements to list
type 'a gen
= unit -> 'a option
type 'a printer
= Stdlib.Format.formatter -> 'a -> unit
type 'a random_gen
= Stdlib.Random.State.t -> 'a
Documentation for the standard ListLabels module
include module type of Stdlib.ListLabels
val length : 'a list -> int
val hd : 'a list -> 'a
val compare_lengths : 'a list -> 'b list -> int
val compare_length_with : 'a list -> len:int -> int
val cons : 'a -> 'a list -> 'a list
val tl : 'a list -> 'a list
val nth : 'a list -> int -> 'a
val nth_opt : 'a list -> int -> 'a option
val rev : 'a list -> 'a list
val init : len:int -> f:(int -> 'a) -> 'a list
val append : 'a list -> 'a list -> 'a list
val rev_append : 'a list -> 'a list -> 'a list
val concat : 'a list list -> 'a list
val flatten : 'a list list -> 'a list
val iter : f:('a -> unit) -> 'a list -> unit
val iteri : f:(int -> 'a -> unit) -> 'a list -> unit
val map : f:('a -> 'b) -> 'a list -> 'b list
val mapi : f:(int -> 'a -> 'b) -> 'a list -> 'b list
val rev_map : f:('a -> 'b) -> 'a list -> 'b list
val filter_map : f:('a -> 'b option) -> 'a list -> 'b list
val fold_left : f:('a -> 'b -> 'a) -> init:'a -> 'b list -> 'a
val fold_right : f:('a -> 'b -> 'b) -> 'a list -> init:'b -> 'b
val iter2 : f:('a -> 'b -> unit) -> 'a list -> 'b list -> unit
val map2 : f:('a -> 'b -> 'c) -> 'a list -> 'b list -> 'c list
val rev_map2 : f:('a -> 'b -> 'c) -> 'a list -> 'b list -> 'c list
val fold_left2 : f:('a -> 'b -> 'c -> 'a) -> init:'a -> 'b list -> 'c list -> 'a
val fold_right2 : f:('a -> 'b -> 'c -> 'c) -> 'a list -> 'b list -> init:'c -> 'c
val for_all : f:('a -> bool) -> 'a list -> bool
val exists : f:('a -> bool) -> 'a list -> bool
val for_all2 : f:('a -> 'b -> bool) -> 'a list -> 'b list -> bool
val exists2 : f:('a -> 'b -> bool) -> 'a list -> 'b list -> bool
val mem : 'a -> set:'a list -> bool
val memq : 'a -> set:'a list -> bool
val find : f:('a -> bool) -> 'a list -> 'a
val find_opt : f:('a -> bool) -> 'a list -> 'a option
val filter : f:('a -> bool) -> 'a list -> 'a list
val find_all : f:('a -> bool) -> 'a list -> 'a list
val partition : f:('a -> bool) -> 'a list -> 'a list * 'a list
val assoc : 'a -> ('a * 'b) list -> 'b
val assoc_opt : 'a -> ('a * 'b) list -> 'b option
val assq : 'a -> ('a * 'b) list -> 'b
val assq_opt : 'a -> ('a * 'b) list -> 'b option
val mem_assoc : 'a -> map:('a * 'b) list -> bool
val mem_assq : 'a -> map:('a * 'b) list -> bool
val remove_assoc : 'a -> ('a * 'b) list -> ('a * 'b) list
val remove_assq : 'a -> ('a * 'b) list -> ('a * 'b) list
val split : ('a * 'b) list -> 'a list * 'b list
val combine : 'a list -> 'b list -> ('a * 'b) list
val sort : cmp:('a -> 'a -> int) -> 'a list -> 'a list
val stable_sort : cmp:('a -> 'a -> int) -> 'a list -> 'a list
val fast_sort : cmp:('a -> 'a -> int) -> 'a list -> 'a list
val sort_uniq : cmp:('a -> 'a -> int) -> 'a list -> 'a list
val merge : cmp:('a -> 'a -> int) -> 'a list -> 'a list -> 'a list
val to_seq : 'a list -> 'a Stdlib.Seq.t
val of_seq : 'a Stdlib.Seq.t -> 'a list
val empty : 'a t
empty
is[]
.
val is_empty : _ t -> bool
is_empty l
returnstrue
iffl = []
.- since
- 0.11
val map : f:('a -> 'b) -> 'a t -> 'b t
map ~f [a0; a1; …; an]
applies functionf
in turn to[a0; a1; …; an]
. Safe version ofList
.map.
val (>|=) : 'a t -> ('a -> 'b) -> 'b t
l >|= f
is the infix version ofmap
with reversed arguments.- since
- 0.5
val append : 'a t -> 'a t -> 'a t
append l1 l2
returns the list that is the concatenation ofl1
andl2
. Safe version ofList
.append.
val cons_maybe : 'a option -> 'a t -> 'a t
cons_maybe (Some x) l
isx :: l
.cons_maybe None l
isl
.- since
- 0.13
val filter : f:('a -> bool) -> 'a t -> 'a t
filter ~f l
returns all the elements of the listl
that satisfy the predicatef
. The order of the elements in the input listl
is preserved. Safe version ofList
.filter.
val fold_right : f:('a -> 'b -> 'b) -> 'a t -> init:'b -> 'b
fold_right ~f [a1; …; an] ~init
isf a1 (f a2 ( … (f an init) … ))
. Safe version ofList
.fold_right.
val fold_while : f:('a -> 'b -> 'a * [ `Stop | `Continue ]) -> init:'a -> 'b t -> 'a
fold_while ~f ~init l
folds until a stop condition via('a, `Stop)
is indicated by the accumulator.- since
- 0.8
val fold_map : f:('acc -> 'a -> 'acc * 'b) -> init:'acc -> 'a list -> 'acc * 'b list
fold_map ~f ~init l
is afold_left
-like function, but it also maps the list to another list.- since
- 0.14
val fold_map_i : f:('acc -> int -> 'a -> 'acc * 'b) -> init:'acc -> 'a list -> 'acc * 'b list
fold_map_i ~f ~init l
is afoldi
-like function, but it also maps the list to another list.- since
- 2.8
val fold_on_map : f:('a -> 'b) -> reduce:('acc -> 'b -> 'acc) -> init:'acc -> 'a list -> 'acc
fold_on_map ~f ~reduce ~init l
combinesmap ~f
andfold_left ~reduce ~init
in one operation.- since
- 2.8
val scan_left : f:('acc -> 'a -> 'acc) -> init:'acc -> 'a list -> 'acc list
scan_left ~f ~init l
returns the list[init; f init x0; f (f init x0) x1; …]
wherex0
,x1
, etc. are the elements ofl
.- since
- 1.2, but only
- since
- 2.2 with labels
val fold_map2 : f:('acc -> 'a -> 'b -> 'acc * 'c) -> init:'acc -> 'a list -> 'b list -> 'acc * 'c list
fold_map2 ~f ~init l1 l2
is tofold_map
whatList.map2
is toList.map
.- raises Invalid_argument
if the lists do not have the same length.
- since
- 0.16
val fold_filter_map : f:('acc -> 'a -> 'acc * 'b option) -> init:'acc -> 'a list -> 'acc * 'b list
fold_filter_map ~f ~init l
is afold_left
-like function, but also generates a list of output in a way similar tofilter_map
.- since
- 0.17
val fold_filter_map_i : f:('acc -> int -> 'a -> 'acc * 'b option) -> init:'acc -> 'a list -> 'acc * 'b list
fold_filter_map_i ~f ~init l
is afoldi
-like function, but also generates a list of output in a way similar tofilter_map
.- since
- 2.8
val fold_flat_map : f:('acc -> 'a -> 'acc * 'b list) -> init:'acc -> 'a list -> 'acc * 'b list
fold_flat_map ~f ~init l
is afold_left
-like function, but it also maps the list to a list of lists that is thenflatten
'd.- since
- 0.14
val fold_flat_map_i : f:('acc -> int -> 'a -> 'acc * 'b list) -> init:'acc -> 'a list -> 'acc * 'b list
fold_flat_map_i ~f ~init l
is afold_left
-like function, but it also maps the list to a list of lists that is thenflatten
'd.- since
- 2.8
val count : f:('a -> bool) -> 'a list -> int
count ~f l
counts how many elements ofl
satisfy predicatef
.- since
- 1.5, but only
- since
- 2.2 with labels
val count_true_false : f:('a -> bool) -> 'a list -> int * int
count_true_false ~f l
returns a pair(int1,int2)
whereint1
is the number of elements inl
that satisfy the predicatef
, andint2
the number of elements that do not satisfyf
.- since
- 2.4
val init : int -> f:(int -> 'a) -> 'a t
init len ~f
isf 0; f 1; …; f (len-1)
.- raises Invalid_argument
if len < 0.
- since
- 0.6
val combine : 'a list -> 'b list -> ('a * 'b) list
combine [a1; …; an] [b1; …; bn]
is[(a1,b1); …; (an,bn)]
. Transform two lists into a list of pairs. LikeList
.combine but tail-recursive.- raises Invalid_argument
if the lists have distinct lengths.
- since
- 1.2, but only
- since
- 2.2 with labels
val combine_gen : 'a list -> 'b list -> ('a * 'b) gen
combine_gen l1 l2
transforms two lists into agen
of pairs. Lazy version ofcombine
. Unlikecombine
, it does not fail if the lists have different lengths; instead, the output has as many pairs as the smallest input list.- since
- 1.2, but only
- since
- 2.2 with labels
val combine_shortest : 'a list -> 'b list -> ('a * 'b) list
combine_shortest [a1; …; am] [b1; …; bn]
is[(a1,b1); …; (am,bm)]
if m <= n. Likecombine
but stops at the shortest list rather than raising.- since
- 3.1
val split : ('a * 'b) t -> 'a t * 'b t
split [(a1,b1); …; (an,bn)]
is([a1; …; an], [b1; …; bn])
. Transform a list of pairs into a pair of lists. A tail-recursive version ofList
.split.- since
- 1.2, but only
- since
- 2.2 with labels
val compare : ('a -> 'a -> int) -> 'a t -> 'a t -> int
compare cmp l1 l2
compares the two listsl1
andl2
using the given comparison functioncmp
.
val compare_lengths : 'a t -> 'b t -> int
compare_lengths l1 l2
compare the lengths of the two listsl1
andl2
. Equivalent tocompare (length l1) (length l2)
but more efficient.- since
- 1.5, but only
- since
- 2.2 with labels
val compare_length_with : 'a t -> int -> int
compare_length_with l x
compares the length of the listl
to an integerx
. Equivalent tocompare (length l) x
but more efficient.- since
- 1.5, but only
- since
- 2.2 with labels
val equal : ('a -> 'a -> bool) -> 'a t -> 'a t -> bool
equal p l1 l2
returnstrue
ifl1
andl2
are equal.
val flat_map : f:('a -> 'b t) -> 'a t -> 'b t
flat_map ~f l
maps and flattens at the same time (safe). Evaluation order is not guaranteed.
val flat_map_i : f:(int -> 'a -> 'b t) -> 'a t -> 'b t
flat_map_i ~f l
maps with index and flattens at the same time (safe). Evaluation order is not guaranteed.- since
- 2.8
val flatten : 'a t t -> 'a t
flatten [l1]; [l2]; …
concatenates a list of lists. Safe version ofList
.flatten.
val product : f:('a -> 'b -> 'c) -> 'a t -> 'b t -> 'c t
product ~f l1 l2
computes the cartesian product of the two lists, with the given combinatorf
.
val fold_product : f:('c -> 'a -> 'b -> 'c) -> init:'c -> 'a t -> 'b t -> 'c
fold_product ~f ~init l1 l2
applies the functionf
with the accumulatorinit
on all the pair of elements ofl1
andl2
. Fold on the cartesian product.
val cartesian_product : 'a t t -> 'a t t
cartesian_product [[l1];[l2]; …; [ln]]
produces the cartesian product of this list of lists, by returning all the ways of picking one element per sublist. NOTE the order of the returned list is unspecified. For example:# cartesian_product [[1;2];[3];[4;5;6]] |> sort = [[1;3;4];[1;3;5];[1;3;6];[2;3;4];[2;3;5];[2;3;6]];; # cartesian_product [[1;2];[];[4;5;6]] = [];; # cartesian_product [[1;2];[3];[4];[5];[6]] |> sort = [[1;3;4;5;6];[2;3;4;5;6]];;
invariant:
cartesian_product l = map_product id l
.- since
- 1.2, but only
- since
- 2.2 with labels
val map_product_l : f:('a -> 'b list) -> 'a list -> 'b list list
map_product_l ~f l
maps each element ofl
to a list of objects of type'b
usingf
. We obtain[l1; l2; …; ln]
wherelength l=n
andli : 'b list
. Then, it returns all the ways of picking exactly one element perli
.- since
- 1.2, but only
- since
- 2.2 with labels
val diagonal : 'a t -> ('a * 'a) t
diagonal l
returns all pairs of distinct positions of the listl
, that is the list ofList.nth i l, List.nth j l
ifi < j
.
val partition_map : f:('a -> [< `Left of 'b | `Right of 'c | `Drop ]) -> 'a list -> 'b list * 'c list
partition_map ~f l
mapsf
onl
and gather results in lists:- if
f x = `Left y
, addsy
to the first list. - if
f x = `Right z
, addsz
to the second list. - if
f x = `Drop
, ignoresx
.
- since
- 0.11
- if
val group_by : ?hash:('a -> int) -> ?eq:('a -> 'a -> bool) -> 'a t -> 'a list t
group_by ?hash ?eq l
groups equal elements, regardless of their order of appearance. precondition: for anyx
andy
, ifeq x y
thenhash x = hash y
must hold.- since
- 2.3
val join : join_row:('a -> 'b -> 'c option) -> 'a t -> 'b t -> 'c t
join ~join_row a b
combines every element ofa
with every element ofb
usingjoin_row
. Ifjoin_row
returnsNone
, then the two elements do not combine. Assume thatb
allows for multiple iterations.- since
- 2.3
val join_by : ?eq:('key -> 'key -> bool) -> ?hash:('key -> int) -> ('a -> 'key) -> ('b -> 'key) -> merge:('key -> 'a -> 'b -> 'c option) -> 'a t -> 'b t -> 'c t
join_by ?eq ?hash key1 key2 ~merge la lb
is a binary operation that takes two sequencesa
andb
, projects their elements resp. withkey1
andkey2
, and combine values(x,y)
from(a,b)
with the samekey
usingmerge
. Ifmerge
returnsNone
, the combination of values is discarded. precondition: for anyx
andy
, ifeq x y
thenhash x = hash y
must hold.- since
- 2.3
val join_all_by : ?eq:('key -> 'key -> bool) -> ?hash:('key -> int) -> ('a -> 'key) -> ('b -> 'key) -> merge:('key -> 'a list -> 'b list -> 'c option) -> 'a t -> 'b t -> 'c t
join_all_by ?eq ?hash key1 key2 ~merge la lb
is a binary operation that takes two sequencesa
andb
, projects their elements resp. withkey1
andkey2
, and, for each keyk
occurring in at least one of them:- compute the list
l1
of elements ofa
that map tok
- compute the list
l2
of elements ofb
that map tok
- call
merge k l1 l2
. Ifmerge
returnsNone
, the combination of values is discarded, otherwise it returnsSome c
andc
is inserted in the result.
- since
- 2.3
- compute the list
val group_join_by : ?eq:('a -> 'a -> bool) -> ?hash:('a -> int) -> ('b -> 'a) -> 'a t -> 'b t -> ('a * 'b list) t
group_join_by ?eq ?hash key la lb
associates to every elementx
of the first sequence, all the elementsy
of the second sequence such thateq x (key y)
. Elements of the first sequences without corresponding values in the second one are mapped to[]
precondition: for anyx
andy
, ifeq x y
thenhash x = hash y
must hold.- since
- 2.3
val sublists_of_len : ?last:('a list -> 'a list option) -> ?offset:int -> len:int -> 'a list -> 'a list list
sublists_of_len ?last ?offset n l
returns sub-lists ofl
that have lengthn
. By default, these sub-lists are non overlapping:sublists_of_len 2 [1;2;3;4;5;6]
returns[1;2]; [3;4]; [5;6]
.Examples:
sublists_of_len 2 [1;2;3;4;5;6] = [[1;2]; [3;4]; [5;6]]
.sublists_of_len 2 ~offset:3 [1;2;3;4;5;6] = [1;2];[4;5]
.sublists_of_len 3 ~last:CCOpt.return [1;2;3;4] = [1;2;3];[4]
.sublists_of_len 2 [1;2;3;4;5] = [[1;2]; [3;4]]
.
- parameter offset
the number of elements skipped between two consecutive sub-lists. By default it is
n
. Ifoffset < n
, the sub-lists will overlap; ifoffset > n
, some elements will not appear at all.
- parameter last
if provided and the last group of elements
g
is such thatlength g < n
,last g
is called. Iflast g = Some g'
,g'
is appended; otherwiseg
is dropped. Iflast = CCOpt.return
, it will simply keep the last group. By default,last = fun _ -> None
, i.e. the last group is dropped if shorter thann
.
- raises Invalid_argument
if
offset <= 0
orn <= 0
. SeeCCList.sublists_of_len
for more details.
- since
- 1.0, but only
- since
- 1.5 with labels
val intersperse : x:'a -> 'a list -> 'a list
intersperse ~x l
inserts the elementx
between adjacent elements of the listl
.- since
- 2.1, but only
- since
- 2.2 with labels
val interleave : 'a list -> 'a list -> 'a list
interleave [x1…xn] [y1…ym]
is[x1,y1,x2,y2,…]
and finishes with the suffix of the longest list.- since
- 2.1, but only
- since
- 2.2 with labels
val pure : 'a -> 'a t
pure x
isreturn x
.
val mguard : bool -> unit t
mguard c
ispure ()
ifc
is true,[]
otherwise. This is useful to define a list by comprehension, e.g.:# let square_even xs = let* x = xs in let* () = mguard (x mod 2 = 0) in return @@ x * x;; val square_even : int list -> int list = <fun> # square_even [1;2;4;3;5;2];; - : int list = [4; 16; 4]
- since
- 3.1
val return : 'a -> 'a t
return x
isx
.
val hd_tl : 'a t -> 'a * 'a t
hd_tl (x :: l)
returnshd, l
.- raises Failure
if the list is empty.
- since
- 0.16
val take_drop : int -> 'a t -> 'a t * 'a t
take_drop n l
returnsl1, l2
such thatl1 @ l2 = l
andlength l1 = min (length l) n
.
val take_while : f:('a -> bool) -> 'a t -> 'a t
take_while ~f l
returns the longest prefix ofl
for whichf
istrue
.- since
- 0.13
val drop_while : f:('a -> bool) -> 'a t -> 'a t
drop_while ~f l
drops the longest prefix ofl
for whichf
istrue
.- since
- 0.13
val take_drop_while : f:('a -> bool) -> 'a t -> 'a t * 'a t
take_drop_while ~f l
=take_while ~f l, drop_while ~f l
.- since
- 1.2, but only
- since
- 2.2 with labels
val last : int -> 'a t -> 'a t
last n l
takes the lastn
elements ofl
(or less ifl
doesn't have that many elements).
val head_opt : 'a t -> 'a option
head_opt l
returnsSome x
(the first element of the listl
) orNone
if the listl
is empty.- since
- 0.20
val tail_opt : 'a t -> 'a t option
tail_opt l
returnsSome l'
(the given listl
without its first element) orNone
if the listl
is empty.- since
- 2.0
val last_opt : 'a t -> 'a option
last_opt l
returnsSome x
(the last element ofl
) orNone
if the listl
is empty.- since
- 0.20
val find_pred : f:('a -> bool) -> 'a t -> 'a option
find_pred ~f l
finds the first element ofl
that satisfiesf
, or returnsNone
if no element satisfiesf
.- since
- 0.11
val find_opt : f:('a -> bool) -> 'a t -> 'a option
find_opt ~f l
is the safe version offind
.- since
- 1.5, but only
- since
- 2.2 with labels
val find_pred_exn : f:('a -> bool) -> 'a t -> 'a
find_pred_exn ~f l
is the unsafe version offind_pred
.- raises Not_found
if no such element is found.
- since
- 0.11
val find_map : f:('a -> 'b option) -> 'a t -> 'b option
find_map ~f l
traversesl
, applyingf
to each element. If for some elementx
,f x = Some y
, thenSome y
is returned. Otherwise the call returnsNone
.- since
- 0.11
val find_mapi : f:(int -> 'a -> 'b option) -> 'a t -> 'b option
find_mapi ~f l
is likefind_map
, but also pass the index to the predicate function.- since
- 0.11
val find_idx : f:('a -> bool) -> 'a t -> (int * 'a) option
find_idx ~f x
returnsSome (i,x)
wherex
is thei
-th element ofl
, andf x
holds. Otherwise returnsNone
.
val remove : eq:('a -> 'a -> bool) -> key:'a -> 'a t -> 'a t
remove ~eq ~key l
removes every instance ofkey
froml
. Tail-recursive.- parameter eq
equality function.
- since
- 0.11
val filter_map : f:('a -> 'b option) -> 'a t -> 'b t
filter_map ~f l
is the sublist ofl
containing only elements for whichf
returnsSome e
. Map and remove elements at the same time.
val keep_some : 'a option t -> 'a t
keep_some l
retains only elements of the formSome x
. Likefilter_map CCFun.id
.- since
- 1.3, but only
- since
- 2.2 with labels
val keep_ok : ('a, _) Stdlib.result t -> 'a t
keep_ok l
retains only elements of the formOk x
.- since
- 1.3, but only
- since
- 2.2 with labels
val all_some : 'a option t -> 'a t option
all_some l
returnsSome l'
if all elements ofl
are of the formSome x
, orNone
otherwise.- since
- 1.3, but only
- since
- 2.2 with labels
val all_ok : ('a, 'err) Stdlib.result t -> ('a t, 'err) Stdlib.result
all_ok l
returnsOk l'
if all elements ofl
are of the formOk x
, orError e
otherwise (with the first error met).- since
- 1.3, but only
- since
- 2.2 with labels
val sorted_merge : cmp:('a -> 'a -> int) -> 'a list -> 'a list -> 'a list
sorted_merge ~cmp l1 l2
merges elements from both sorted list using the given comparison functioncmp
.
val sort_uniq : cmp:('a -> 'a -> int) -> 'a list -> 'a list
sort_uniq ~cmp l
sorts the listl
using the given comparison functioncmp
and remove duplicate elements.
val sorted_merge_uniq : cmp:('a -> 'a -> int) -> 'a list -> 'a list -> 'a list
sorted_merge_uniq ~cmp l1 l2
merges the sorted listsl1
andl2
and removes duplicates.- since
- 0.10
val is_sorted : cmp:('a -> 'a -> int) -> 'a list -> bool
is_sorted ~cmp l
returnstrue
iffl
is sorted (according to given order).- parameter cmp
the comparison function.
- since
- 0.17
val sorted_insert : cmp:('a -> 'a -> int) -> ?uniq:bool -> 'a -> 'a list -> 'a list
sorted_insert ~cmp ?uniq x l
insertsx
intol
such that, ifl
was sorted, thensorted_insert x l
is sorted too.- parameter uniq
if true and
x
is already in sorted position inl
, thenx
is not duplicated. Defaultfalse
(x
will be inserted in any case).
- since
- 0.17
val uniq_succ : eq:('a -> 'a -> bool) -> 'a list -> 'a list
uniq_succ ~eq l
removes duplicate elements that occur one next to the other. Examples:uniq_succ [1;2;1] = [1;2;1]
.uniq_succ [1;1;2] = [1;2]
.- since
- 0.10
val group_succ : eq:('a -> 'a -> bool) -> 'a list -> 'a list list
group_succ ~eq l
groups together consecutive elements that are equal according toeq
.- since
- 0.11
Indices
val mapi : f:(int -> 'a -> 'b) -> 'a t -> 'b t
mapi ~f l
is likemap
, but the functionf
is applied to the index of the element as first argument (counting from 0), and the element itself as second argument.
val iteri : f:(int -> 'a -> unit) -> 'a t -> unit
iteri ~f l
is likeiter
, but the functionf
is applied to the index of the element as first argument (counting from 0), and the element itself as second argument.
val iteri2 : f:(int -> 'a -> 'b -> unit) -> 'a t -> 'b t -> unit
iteri2 ~f l1 l2
appliesf
to the two listsl1
andl2
simultaneously. The integer passed tof
indicates the index of element.- raises Invalid_argument
when lists do not have the same length.
- since
- 2.0, but only
- since
- 2.2 with labels
val foldi : f:('b -> int -> 'a -> 'b) -> init:'b -> 'a t -> 'b
foldi ~f ~init l
is likefold
but it also passes in the index of each element, from0
tolength l - 1
as additional argument to the folded functionf
. Tail-recursive.
val foldi2 : f:('c -> int -> 'a -> 'b -> 'c) -> init:'c -> 'a t -> 'b t -> 'c
foldi2 ~f ~init l1 l2
folds on the two listsl1
andl2
, with index of each element passed to the functionf
. Computesf(… (f init i_0 l1_0 l2_0) …) i_n l1_n l2_n
.- raises Invalid_argument
when lists do not have the same length.
- since
- 2.0, but only
- since
- 2.2 with labels
val get_at_idx : int -> 'a t -> 'a option
get_at_idx i l
returnsSome i-th
element of the given listl
orNone
if the listl
is too short. If the index is negative, it will get element starting from the end of the listl
.
val nth_opt : 'a t -> int -> 'a option
nth_opt l n
returnsSome n-th
element ofl
. Safe version ofnth
.- raises Invalid_argument
if the int is negative.
- since
- 1.5, but only
- since
- 2.2 with labels
val get_at_idx_exn : int -> 'a t -> 'a
get_at_idx_exn i l
gets thei-th
element ofl
, or- raises Not_found
if the index is invalid. The first element has index 0. If the index is negative, it will get element starting from the end of the list.
val set_at_idx : int -> 'a -> 'a t -> 'a t
set_at_idx i x l
replaces thei-th
element withx
(removes the old one), or does nothing if index is too high. If the index is negative, it will set element starting from the end of the list.
Set Operators
Those operations maintain the invariant that the list does not contain duplicates (if it already satisfies it).
val add_nodup : eq:('a -> 'a -> bool) -> 'a -> 'a t -> 'a t
add_nodup ~eq x set
addsx
toset
if it was not already present. Linear time.- since
- 0.11
val remove_one : eq:('a -> 'a -> bool) -> 'a -> 'a t -> 'a t
remove_one ~eq x set
removes one occurrence ofx
fromset
. Linear time.- since
- 0.11
val mem : ?eq:('a -> 'a -> bool) -> 'a -> 'a t -> bool
mem ?eq x l
istrue
iffx
is equal to an element ofl
. A comparator functioneq
can be provided. Linear time.
val subset : eq:('a -> 'a -> bool) -> 'a t -> 'a t -> bool
subset ~eq l1 l2
tests if all elements of the listl1
are contained in the listl2
by applyingeq
.
val uniq : eq:('a -> 'a -> bool) -> 'a t -> 'a t
uniq ~eq l
removes duplicates inl
w.r.t the equality predicateeq
. Complexity is quadratic in the length of the list, but the order of elements is preserved. If you wish for a faster de-duplication but do not care about the order, usesort_uniq
.
Other Constructors
val range_by : step:int -> int -> int -> int t
range_by ~step i j
iterates on integers fromi
toj
included, where the difference between successive elements isstep
. Use a negativestep
for a decreasing list.- raises Invalid_argument
if
step=0
.
- since
- 0.18
val range : int -> int -> int t
range i j
iterates on integers fromi
toj
included. It works both for decreasing and increasing ranges.
val range' : int -> int -> int t
range' i j
is likerange
but the second boundj
is excluded. For instancerange' 0 5 = [0;1;2;3;4]
.
val (--) : int -> int -> int t
i -- j
is the list of integers fromi
toj
included. Infix alias forrange
.
val (--^) : int -> int -> int t
i --^ j
is the list of integers fromi
toj
excluded. Infix alias forrange'
.- since
- 0.17
val replicate : int -> 'a -> 'a t
replicate n x
replicates the given elementx
n
times.
Association Lists
module Assoc : sig ... end
val assoc : eq:('a -> 'a -> bool) -> 'a -> ('a * 'b) t -> 'b
assoc ~eq k alist
returns the valuev
associated with keyk
inalist
. LikeAssoc.get_exn
.- since
- 2.0
val assoc_opt : eq:('a -> 'a -> bool) -> 'a -> ('a * 'b) t -> 'b option
assoc_opt ~eq k alist
returnsSome v
if the given keyk
is present intoalist
, orNone
if not present. LikeAssoc.get
.- since
- 1.5, but only
- since
- 2.0 with labels
val assq_opt : 'a -> ('a * 'b) t -> 'b option
assq_opt k alist
returnsSome v
if the given keyk
is present intoalist
. LikeAssoc.assoc_opt
but use physical equality instead of structural equality to compare keys. Safe version ofassq
.- since
- 1.5, but only
- since
- 2.0 with labels
val mem_assoc : ?eq:('a -> 'a -> bool) -> 'a -> ('a * _) t -> bool
mem_assoc ?eq k alist
returnstrue
iffk
is a key inalist
. LikeAssoc.mem
.- since
- 2.0
References on Lists
- since
- 0.3.3
module Ref : sig ... end
module type MONAD = sig ... end
Conversions
val random : 'a random_gen -> 'a t random_gen
val random_non_empty : 'a random_gen -> 'a t random_gen
val random_len : int -> 'a random_gen -> 'a t random_gen
val random_choose : 'a t -> 'a random_gen
random_choose l
randomly chooses an element in the listl
.- raises Not_found
if the list is empty.
val random_sequence : 'a random_gen t -> 'a t random_gen
val to_string : ?start:string -> ?stop:string -> ?sep:string -> ('a -> string) -> 'a t -> string
to_string ?start ?stop ?sep item_to_string l
printl
to a string usingsep
as a separator between elements ofl
.- since
- 2.7
val to_seq : 'a t -> 'a Stdlib.Seq.t
to_seq l
returns aSeq.t
of the elements of the listl
. Renamed fromto_std_seq
since 3.0.- since
- 3.0
val of_iter : 'a iter -> 'a t
of_iter iter
builds a list from a giveniter
. In the result, elements appear in the same order as they did in the sourceiter
.- since
- 2.8
val of_seq_rev : 'a Stdlib.Seq.t -> 'a t
of_seq_rev seq
builds a list from a givenSeq.t
, in reverse order. Renamed fromof_std_seq_rev
since 3.0.- since
- 3.0
val of_seq : 'a Stdlib.Seq.t -> 'a t
of_seq seq
builds a list from a givenSeq.t
. In the result, elements appear in the same order as they did in the sourceSeq.t
. Renamed fromof_std_seq
since 3.0.- since
- 3.0
Infix Operators
It is convenient to open CCList
.Infix to access the infix operators without cluttering the scope too much.
- since
- 0.16
module Infix : sig ... end
Let operators on OCaml >= 4.08.0, nothing otherwise
- since
- 2.8