From 8c36e7411c7cea26eddd732fb4e199fc514db136 Mon Sep 17 00:00:00 2001 From: c-cube Date: Tue, 25 May 2021 23:20:53 +0000 Subject: [PATCH] deploy: 57e810a8821c8763b2d4c8af426fd643252cf7c2 --- dev/containers/CCList/index.html | 2 +- dev/containers/CCListLabels/index.html | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/dev/containers/CCList/index.html b/dev/containers/CCList/index.html index 062d3ea9..d1f18c39 100644 --- a/dev/containers/CCList/index.html +++ b/dev/containers/CCList/index.html @@ -9,4 +9,4 @@ 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 (<*>) : ('a -> 'b) t -> 'a t -> 'b t

funs <*> l is product (fun f x -> f x) funs l.

val (<$>) : ('a -> 'b) -> 'a t -> 'b t

(<$>) is map.

val return : 'a -> 'a t

return x is x.

val (>>=) : 'a t -> ('a -> 'b t) -> 'b t

l >>= f is flat_map f l.

val take : int -> 'a t -> 'a t

take n l takes the n first elements of the list l, drop the rest.

val drop : int -> 'a t -> 'a t

drop n l drops the n first elements of the list l, keep the rest.

val hd_tl : 'a t -> 'a * 'a t

hd_tl (x :: l) returns hd, 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 returns l1, l2 such that l1 @ l2 = l and length l1 = min (length l) n.

val take_while : ('a -> bool) -> 'a t -> 'a t

take_while f l returns the longest prefix of l for which f is true.

since
0.13
val drop_while : ('a -> bool) -> 'a t -> 'a t

drop_while f l drops the longest prefix of l for which f is true.

since
0.13
val take_drop_while : ('a -> bool) -> 'a t -> 'a t * 'a t

take_drop_while p l = take_while p l, drop_while p l.

since
1.2, but only
since
2.2 with labels
val last : int -> 'a t -> 'a t

last n l takes the last n elements of l (or less if l doesn't have that many elements).

val head_opt : 'a t -> 'a option

head_opt l returns Some x (the first element of the list l) or None if the list l is empty.

since
0.20
val tail_opt : 'a t -> 'a t option

tail_opt l returns Some l' (the given list l without its first element) or None if the list l is empty.

since
2.0
val last_opt : 'a t -> 'a option

last_opt l returns Some x (the last element of l) or None if the list l is empty.

since
0.20
val find_pred : ('a -> bool) -> 'a t -> 'a option

find_pred p l finds the first element of l that satisfies p, or returns None if no element satisfies p.

since
0.11
val find_opt : ('a -> bool) -> 'a t -> 'a option

find_opt p l is the safe version of find.

since
1.5, but only
since
2.2 with labels
val find_pred_exn : ('a -> bool) -> 'a t -> 'a

find_pred_exn p l is the unsafe version of find_pred.

raises Not_found

if no such element is found.

since
0.11
val find_map : ('a -> 'b option) -> 'a t -> 'b option

find_map f l traverses l, applying f to each element. If for some element x, f x = Some y, then Some y is returned. Otherwise the call returns None.

since
0.11
val find_mapi : (int -> 'a -> 'b option) -> 'a t -> 'b option

find_mapi f l is like find_map, but also pass the index to the predicate function.

since
0.11
val find_idx : ('a -> bool) -> 'a t -> (int * 'a) option

find_idx p x returns Some (i,x) where x is the i-th element of l, and p x holds. Otherwise returns None.

val remove : eq:('a -> 'a -> bool) -> key:'a -> 'a t -> 'a t

remove ~eq ~key l removes every instance of key from l. Tail-recursive.

parameter eq

equality function.

since
0.11
val filter_map : ('a -> 'b option) -> 'a t -> 'b t

filter_map f l is the sublist of l containing only elements for which f returns Some 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 form Some x. Like filter_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 form Ok x.

since
1.3, but only
since
2.2 with labels
val all_some : 'a option t -> 'a t option

all_some l returns Some l' if all elements of l are of the form Some x, or None 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 returns Ok l' if all elements of l are of the form Ok x, or Error 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 function cmp.

val sort_uniq : cmp:('a -> 'a -> int) -> 'a list -> 'a list

sort_uniq ~cmp l sorts the list l using the given comparison function cmp 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 lists l1 and l2 and removes duplicates.

since
0.10
val is_sorted : cmp:('a -> 'a -> int) -> 'a list -> bool

is_sorted ~cmp l returns true iff l 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 inserts x into l such that, if l was sorted, then sorted_insert x l is sorted too.

parameter uniq

if true and x is already in sorted position in l, then x is not duplicated. Default false (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 ~eq:(=) [1;2;1] = [1;2;1]. uniq_succ ~eq:(=) [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 to eq.

since
0.11

Indices

val mapi : (int -> 'a -> 'b) -> 'a t -> 'b t

mapi f l is like map, but the function f is applied to the index of the element as first argument (counting from 0), and the element itself as second argument.

val iteri : (int -> 'a -> unit) -> 'a t -> unit

iteri f l is like iter, but the function f is applied to the index of the element as first argument (counting from 0), and the element itself as second argument.

val iteri2 : (int -> 'a -> 'b -> unit) -> 'a t -> 'b t -> unit

iteri2 f l1 l2 applies f to the two lists l1 and l2 simultaneously. The integer passed to f 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 : ('b -> int -> 'a -> 'b) -> 'b -> 'a t -> 'b

foldi f init l is like fold but it also passes in the index of each element, from 0 to length l - 1 as additional argument to the folded function f. Tail-recursive.

val foldi2 : ('c -> int -> 'a -> 'b -> 'c) -> 'c -> 'a t -> 'b t -> 'c

foldi2 f init l1 l2 folds on the two lists l1 and l2, with index of each element passed to the function f. Computes f(… (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 returns Some i-th element of the given list l or None if the list l is too short. If the index is negative, it will get element starting from the end of the list l.

val nth_opt : 'a t -> int -> 'a option

nth_opt l n returns Some n-th element of l. Safe version of nth.

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 the i-th element of l, 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 the i-th element with x (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.

val insert_at_idx : int -> 'a -> 'a t -> 'a t

insert_at_idx i x l inserts x at i-th position, between the two existing elements. If the index is too high, append at the end of the list. If the index is negative, it will insert element starting from the end of the list.

val remove_at_idx : int -> 'a t -> 'a t

remove_at_idx i l removes element at given index i. Does nothing if the index is too high. If the index is negative, it will remove 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 adds x to set 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 of x from set. Linear time.

since
0.11
val mem : ?⁠eq:('a -> 'a -> bool) -> 'a -> 'a t -> bool

mem ?eq x l is true iff x is equal to an element of l. A comparator function eq 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 list l1 are contained in the list l2 by applying eq.

val uniq : eq:('a -> 'a -> bool) -> 'a t -> 'a t

uniq ~eq l removes duplicates in l w.r.t the equality predicate eq. 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, use sort_uniq.

val union : eq:('a -> 'a -> bool) -> 'a t -> 'a t -> 'a t

union ~eq l1 l2 is the union of the lists l1 and l2 w.r.t. the equality predicate eq. Complexity is product of length of inputs.

val inter : eq:('a -> 'a -> bool) -> 'a t -> 'a t -> 'a t

inter ~eq l1 l2 is the intersection of the lists l1 and l2 w.r.t. the equality predicate eq. Complexity is product of length of inputs.

Other Constructors

val range_by : step:int -> int -> int -> int t

range_by ~step i j iterates on integers from i to j included, where the difference between successive elements is step. Use a negative step 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 from i to j included. It works both for decreasing and increasing ranges.

val range' : int -> int -> int t

range' i j is like range but the second bound j is excluded. For instance range' 0 5 = [0;1;2;3;4].

val (--) : int -> int -> int t

i -- j is the list of integers from i to j included. Infix alias for range.

val (--^) : int -> int -> int t

i --^ j is the list of integers from i to j excluded. Infix alias for range'.

since
0.17
val replicate : int -> 'a -> 'a t

replicate n x replicates the given element x n times.

val repeat : int -> 'a t -> 'a t

repeat n l concatenates the list l with itself 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 value v associated with key k in alist. Like Assoc.get_exn.

since
2.0
val assoc_opt : eq:('a -> 'a -> bool) -> 'a -> ('a * 'b) t -> 'b option

assoc_opt ~eq k alist returns Some v if the given key k is present into alist, or None if not present. Like Assoc.get.

since
1.5, but only
since
2.0 with labels
val assq_opt : 'a -> ('a * 'b) t -> 'b option

assq_opt k alist returns Some v if the given key k is present into alist. Like Assoc.assoc_opt but use physical equality instead of structural equality to compare keys. Safe version of assq.

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 returns true iff k is a key in alist. Like Assoc.mem.

since
2.0
val remove_assoc : eq:('a -> 'a -> bool) -> 'a -> ('a * 'b) t -> ('a * 'b) t

remove_assoc ~eq k alist returns the alist without the first pair with key k, if any. Like Assoc.remove.

since
2.0

References on Lists

since
0.3.3
module Ref : sig ... end
module type MONAD = sig ... end
module Traverse : functor (M : 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 list l.

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 prints l to a string using sep as a separator between elements of l.

since
2.7
val to_iter : 'a t -> 'a iter

to_iter l returns a iter of the elements of the list l.

since
2.8
val to_seq : 'a t -> 'a Stdlib.Seq.t

to_seq l returns a Seq.t of the elements of the list l. Renamed from to_std_seq since 3.0.

since
3.0
val of_iter : 'a iter -> 'a t

of_iter iter builds a list from a given iter. In the result, elements appear in the same order as they did in the source iter.

since
2.8
val of_seq_rev : 'a Stdlib.Seq.t -> 'a t

of_seq_rev seq builds a list from a given Seq.t, in reverse order. Renamed from to_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 given Seq.t. In the result, elements appear in the same order as they did in the source Seq.t. Renamed from of_std_seq since 3.0.

since
3.0
val to_gen : 'a t -> 'a gen

to_gen l returns a gen of the elements of the list l.

val of_gen : 'a gen -> 'a t

of_gen gen builds a list from a given gen. In the result, elements appear in the same order as they did in the source gen.

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
include CCShimsMkLet_.S with type 'a t_let := 'a list
type 'a t_let
val let+ : 'a t_let -> ('a -> 'b) -> 'b t_let
val and+ : 'a t_let -> 'b t_let -> ('a * 'b) t_let
val let* : 'a t_let -> ('a -> 'b t_let) -> 'b t_let
val and* : 'a t_let -> 'b t_let -> ('a * 'b) t_let

IO

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 ppf l prints the contents of a list.

\ No newline at end of file +- : int list = [4; 16; 4]
since
3.1
val (<*>) : ('a -> 'b) t -> 'a t -> 'b t

funs <*> l is product (fun f x -> f x) funs l.

val (<$>) : ('a -> 'b) -> 'a t -> 'b t

(<$>) is map.

val return : 'a -> 'a t

return x is x.

val (>>=) : 'a t -> ('a -> 'b t) -> 'b t

l >>= f is flat_map f l.

val take : int -> 'a t -> 'a t

take n l takes the n first elements of the list l, drop the rest.

val drop : int -> 'a t -> 'a t

drop n l drops the n first elements of the list l, keep the rest.

val hd_tl : 'a t -> 'a * 'a t

hd_tl (x :: l) returns hd, 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 returns l1, l2 such that l1 @ l2 = l and length l1 = min (length l) n.

val take_while : ('a -> bool) -> 'a t -> 'a t

take_while f l returns the longest prefix of l for which f is true.

since
0.13
val drop_while : ('a -> bool) -> 'a t -> 'a t

drop_while f l drops the longest prefix of l for which f is true.

since
0.13
val take_drop_while : ('a -> bool) -> 'a t -> 'a t * 'a t

take_drop_while p l = take_while p l, drop_while p l.

since
1.2, but only
since
2.2 with labels
val last : int -> 'a t -> 'a t

last n l takes the last n elements of l (or less if l doesn't have that many elements).

val head_opt : 'a t -> 'a option

head_opt l returns Some x (the first element of the list l) or None if the list l is empty.

since
0.20
val tail_opt : 'a t -> 'a t option

tail_opt l returns Some l' (the given list l without its first element) or None if the list l is empty.

since
2.0
val last_opt : 'a t -> 'a option

last_opt l returns Some x (the last element of l) or None if the list l is empty.

since
0.20
val find_pred : ('a -> bool) -> 'a t -> 'a option

find_pred p l finds the first element of l that satisfies p, or returns None if no element satisfies p.

since
0.11
val find_opt : ('a -> bool) -> 'a t -> 'a option

find_opt p l is the safe version of find.

since
1.5, but only
since
2.2 with labels
val find_pred_exn : ('a -> bool) -> 'a t -> 'a

find_pred_exn p l is the unsafe version of find_pred.

raises Not_found

if no such element is found.

since
0.11
val find_map : ('a -> 'b option) -> 'a t -> 'b option

find_map f l traverses l, applying f to each element. If for some element x, f x = Some y, then Some y is returned. Otherwise the call returns None.

since
0.11
val find_mapi : (int -> 'a -> 'b option) -> 'a t -> 'b option

find_mapi f l is like find_map, but also pass the index to the predicate function.

since
0.11
val find_idx : ('a -> bool) -> 'a t -> (int * 'a) option

find_idx p x returns Some (i,x) where x is the i-th element of l, and p x holds. Otherwise returns None.

val remove : eq:('a -> 'a -> bool) -> key:'a -> 'a t -> 'a t

remove ~eq ~key l removes every instance of key from l. Tail-recursive.

parameter eq

equality function.

since
0.11
val filter_map : ('a -> 'b option) -> 'a t -> 'b t

filter_map f l is the sublist of l containing only elements for which f returns Some 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 form Some x. Like filter_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 form Ok x.

since
1.3, but only
since
2.2 with labels
val all_some : 'a option t -> 'a t option

all_some l returns Some l' if all elements of l are of the form Some x, or None 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 returns Ok l' if all elements of l are of the form Ok x, or Error e otherwise (with the first error met).

since
1.3, but only
since
2.2 with labels
val sorted_mem : cmp:('a -> 'a -> int) -> 'a -> 'a list -> bool

sorted_mem ~cmp x l and mem x l give the same result for any sorted list l, but potentially more efficiently.

since
NEXT_RELEASE
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 function cmp.

val sorted_diff : cmp:('a -> 'a -> int) -> 'a list -> 'a list -> 'a list

sorted_diff ~cmp l1 l2 returns the elements in l1 that are not in l2. Both lists are assumed to be sorted with respect to cmp and duplicate elements in the input lists are treated individually; for example, sorted_diff ~cmp [1;1;1;2;2;3] [1;2;2] would be [1;1;3]. It is the left inverse of sorted_merge; that is, sorted_diff ~cmp (sorted_merge ~cmp l1 l2) l2 is always equal to l1 for sorted lists l1 and l2.

since
NEXT_RELEASE
val sort_uniq : cmp:('a -> 'a -> int) -> 'a list -> 'a list

sort_uniq ~cmp l sorts the list l using the given comparison function cmp 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 lists l1 and l2 and removes duplicates.

since
0.10
val sorted_diff_uniq : cmp:('a -> 'a -> int) -> 'a list -> 'a list -> 'a list

sorted_diff_uniq ~cmp l1 l2 collects the elements in l1 that are not in l2 and then remove duplicates. Both lists are assumed to be sorted with respect to cmp and duplicate elements in the input lists are treated individually; for example, sorted_diff_uniq ~cmp [1;1;1;2;2] [1;2;2;2] would be [1]. sorted_diff_uniq ~cmp l1 l2 and uniq_succ ~eq (sorted_diff ~cmp l1 l2) always give the same result for sorted l1 and l2 and compatible cmp and eq.

since
NEXT_RELEASE
val is_sorted : cmp:('a -> 'a -> int) -> 'a list -> bool

is_sorted ~cmp l returns true iff l 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 inserts x into l such that, if l was sorted, then sorted_insert x l is sorted too.

parameter uniq

if true and x is already in sorted position in l, then x is not duplicated. Default false (x will be inserted in any case).

since
0.17
val sorted_remove : cmp:('a -> 'a -> int) -> ?⁠all:bool -> 'a -> 'a list -> 'a list

sorted_remove ~cmp x l removes x from a sorted list l such that the return value is sorted too. By default, it is the left inverse of sorted_insert; that is, sorted_remove ~cmp x (sorted_insert ~cmp x l) is equal to l for any sorted list l.

parameter all

if true then all occurrences of x will be removed. Otherwise, only the first x will be removed (if any). Default false (only the first will be removed).

since
NEXT_RELEASE
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 ~eq:(=) [1;2;1] = [1;2;1]. uniq_succ ~eq:(=) [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 to eq.

since
0.11

Indices

val mapi : (int -> 'a -> 'b) -> 'a t -> 'b t

mapi f l is like map, but the function f is applied to the index of the element as first argument (counting from 0), and the element itself as second argument.

val iteri : (int -> 'a -> unit) -> 'a t -> unit

iteri f l is like iter, but the function f is applied to the index of the element as first argument (counting from 0), and the element itself as second argument.

val iteri2 : (int -> 'a -> 'b -> unit) -> 'a t -> 'b t -> unit

iteri2 f l1 l2 applies f to the two lists l1 and l2 simultaneously. The integer passed to f 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 : ('b -> int -> 'a -> 'b) -> 'b -> 'a t -> 'b

foldi f init l is like fold but it also passes in the index of each element, from 0 to length l - 1 as additional argument to the folded function f. Tail-recursive.

val foldi2 : ('c -> int -> 'a -> 'b -> 'c) -> 'c -> 'a t -> 'b t -> 'c

foldi2 f init l1 l2 folds on the two lists l1 and l2, with index of each element passed to the function f. Computes f(… (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 returns Some i-th element of the given list l or None if the list l is too short. If the index is negative, it will get element starting from the end of the list l.

val nth_opt : 'a t -> int -> 'a option

nth_opt l n returns Some n-th element of l. Safe version of nth.

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 the i-th element of l, 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 the i-th element with x (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.

val insert_at_idx : int -> 'a -> 'a t -> 'a t

insert_at_idx i x l inserts x at i-th position, between the two existing elements. If the index is too high, append at the end of the list. If the index is negative, it will insert element starting from the end of the list.

val remove_at_idx : int -> 'a t -> 'a t

remove_at_idx i l removes element at given index i. Does nothing if the index is too high. If the index is negative, it will remove 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 adds x to set 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 of x from set. Linear time.

since
0.11
val mem : ?⁠eq:('a -> 'a -> bool) -> 'a -> 'a t -> bool

mem ?eq x l is true iff x is equal to an element of l. A comparator function eq 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 list l1 are contained in the list l2 by applying eq.

val uniq : eq:('a -> 'a -> bool) -> 'a t -> 'a t

uniq ~eq l removes duplicates in l w.r.t the equality predicate eq. 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, use sort_uniq.

val union : eq:('a -> 'a -> bool) -> 'a t -> 'a t -> 'a t

union ~eq l1 l2 is the union of the lists l1 and l2 w.r.t. the equality predicate eq. Complexity is product of length of inputs.

val inter : eq:('a -> 'a -> bool) -> 'a t -> 'a t -> 'a t

inter ~eq l1 l2 is the intersection of the lists l1 and l2 w.r.t. the equality predicate eq. Complexity is product of length of inputs.

Other Constructors

val range_by : step:int -> int -> int -> int t

range_by ~step i j iterates on integers from i to j included, where the difference between successive elements is step. Use a negative step 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 from i to j included. It works both for decreasing and increasing ranges.

val range' : int -> int -> int t

range' i j is like range but the second bound j is excluded. For instance range' 0 5 = [0;1;2;3;4].

val (--) : int -> int -> int t

i -- j is the list of integers from i to j included. Infix alias for range.

val (--^) : int -> int -> int t

i --^ j is the list of integers from i to j excluded. Infix alias for range'.

since
0.17
val replicate : int -> 'a -> 'a t

replicate n x replicates the given element x n times.

val repeat : int -> 'a t -> 'a t

repeat n l concatenates the list l with itself 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 value v associated with key k in alist. Like Assoc.get_exn.

since
2.0
val assoc_opt : eq:('a -> 'a -> bool) -> 'a -> ('a * 'b) t -> 'b option

assoc_opt ~eq k alist returns Some v if the given key k is present into alist, or None if not present. Like Assoc.get.

since
1.5, but only
since
2.0 with labels
val assq_opt : 'a -> ('a * 'b) t -> 'b option

assq_opt k alist returns Some v if the given key k is present into alist. Like Assoc.assoc_opt but use physical equality instead of structural equality to compare keys. Safe version of assq.

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 returns true iff k is a key in alist. Like Assoc.mem.

since
2.0
val remove_assoc : eq:('a -> 'a -> bool) -> 'a -> ('a * 'b) t -> ('a * 'b) t

remove_assoc ~eq k alist returns the alist without the first pair with key k, if any. Like Assoc.remove.

since
2.0

References on Lists

since
0.3.3
module Ref : sig ... end
module type MONAD = sig ... end
module Traverse : functor (M : 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 list l.

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 prints l to a string using sep as a separator between elements of l.

since
2.7
val to_iter : 'a t -> 'a iter

to_iter l returns a iter of the elements of the list l.

since
2.8
val to_seq : 'a t -> 'a Stdlib.Seq.t

to_seq l returns a Seq.t of the elements of the list l. Renamed from to_std_seq since 3.0.

since
3.0
val of_iter : 'a iter -> 'a t

of_iter iter builds a list from a given iter. In the result, elements appear in the same order as they did in the source iter.

since
2.8
val of_seq_rev : 'a Stdlib.Seq.t -> 'a t

of_seq_rev seq builds a list from a given Seq.t, in reverse order. Renamed from to_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 given Seq.t. In the result, elements appear in the same order as they did in the source Seq.t. Renamed from of_std_seq since 3.0.

since
3.0
val to_gen : 'a t -> 'a gen

to_gen l returns a gen of the elements of the list l.

val of_gen : 'a gen -> 'a t

of_gen gen builds a list from a given gen. In the result, elements appear in the same order as they did in the source gen.

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
include CCShimsMkLet_.S with type 'a t_let := 'a list
type 'a t_let
val let+ : 'a t_let -> ('a -> 'b) -> 'b t_let
val and+ : 'a t_let -> 'b t_let -> ('a * 'b) t_let
val let* : 'a t_let -> ('a -> 'b t_let) -> 'b t_let
val and* : 'a t_let -> 'b t_let -> ('a * 'b) t_let

IO

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 ppf l prints the contents of a list.

\ No newline at end of file diff --git a/dev/containers/CCListLabels/index.html b/dev/containers/CCListLabels/index.html index b556b695..3e37504c 100644 --- a/dev/containers/CCListLabels/index.html +++ b/dev/containers/CCListLabels/index.html @@ -9,4 +9,4 @@ 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 (<*>) : ('a -> 'b) t -> 'a t -> 'b t

funs <*> l is product (fun f x -> f x) funs l.

val (<$>) : ('a -> 'b) -> 'a t -> 'b t

(<$>) is map.

val return : 'a -> 'a t

return x is x.

val (>>=) : 'a t -> ('a -> 'b t) -> 'b t

l >>= f is flat_map f l.

val take : int -> 'a t -> 'a t

take n l takes the n first elements of the list l, drop the rest.

val drop : int -> 'a t -> 'a t

drop n l drops the n first elements of the list l, keep the rest.

val hd_tl : 'a t -> 'a * 'a t

hd_tl (x :: l) returns hd, 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 returns l1, l2 such that l1 @ l2 = l and length l1 = min (length l) n.

val take_while : f:('a -> bool) -> 'a t -> 'a t

take_while ~f l returns the longest prefix of l for which f is true.

since
0.13
val drop_while : f:('a -> bool) -> 'a t -> 'a t

drop_while ~f l drops the longest prefix of l for which f is true.

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 last n elements of l (or less if l doesn't have that many elements).

val head_opt : 'a t -> 'a option

head_opt l returns Some x (the first element of the list l) or None if the list l is empty.

since
0.20
val tail_opt : 'a t -> 'a t option

tail_opt l returns Some l' (the given list l without its first element) or None if the list l is empty.

since
2.0
val last_opt : 'a t -> 'a option

last_opt l returns Some x (the last element of l) or None if the list l is empty.

since
0.20
val find_pred : f:('a -> bool) -> 'a t -> 'a option

find_pred ~f l finds the first element of l that satisfies f, or returns None if no element satisfies f.

since
0.11
val find_opt : f:('a -> bool) -> 'a t -> 'a option

find_opt ~f l is the safe version of find.

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 of find_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 traverses l, applying f to each element. If for some element x, f x = Some y, then Some y is returned. Otherwise the call returns None.

since
0.11
val find_mapi : f:(int -> 'a -> 'b option) -> 'a t -> 'b option

find_mapi ~f l is like find_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 returns Some (i,x) where x is the i-th element of l, and f x holds. Otherwise returns None.

val remove : eq:('a -> 'a -> bool) -> key:'a -> 'a t -> 'a t

remove ~eq ~key l removes every instance of key from l. 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 of l containing only elements for which f returns Some 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 form Some x. Like filter_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 form Ok x.

since
1.3, but only
since
2.2 with labels
val all_some : 'a option t -> 'a t option

all_some l returns Some l' if all elements of l are of the form Some x, or None 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 returns Ok l' if all elements of l are of the form Ok x, or Error 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 function cmp.

val sort_uniq : cmp:('a -> 'a -> int) -> 'a list -> 'a list

sort_uniq ~cmp l sorts the list l using the given comparison function cmp 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 lists l1 and l2 and removes duplicates.

since
0.10
val is_sorted : cmp:('a -> 'a -> int) -> 'a list -> bool

is_sorted ~cmp l returns true iff l 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 inserts x into l such that, if l was sorted, then sorted_insert x l is sorted too.

parameter uniq

if true and x is already in sorted position in l, then x is not duplicated. Default false (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 to eq.

since
0.11

Indices

val mapi : f:(int -> 'a -> 'b) -> 'a t -> 'b t

mapi ~f l is like map, but the function f 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 like iter, but the function f 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 applies f to the two lists l1 and l2 simultaneously. The integer passed to f 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 like fold but it also passes in the index of each element, from 0 to length l - 1 as additional argument to the folded function f. 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 lists l1 and l2, with index of each element passed to the function f. Computes f(… (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 returns Some i-th element of the given list l or None if the list l is too short. If the index is negative, it will get element starting from the end of the list l.

val nth_opt : 'a t -> int -> 'a option

nth_opt l n returns Some n-th element of l. Safe version of nth.

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 the i-th element of l, 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 the i-th element with x (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.

val insert_at_idx : int -> 'a -> 'a t -> 'a t

insert_at_idx i x l inserts x at i-th position, between the two existing elements. If the index is too high, append at the end of the list. If the index is negative, it will insert element starting from the end of the list.

val remove_at_idx : int -> 'a t -> 'a t

remove_at_idx i l removes element at given index i. Does nothing if the index is too high. If the index is negative, it will remove 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 adds x to set 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 of x from set. Linear time.

since
0.11
val mem : ?⁠eq:('a -> 'a -> bool) -> 'a -> 'a t -> bool

mem ?eq x l is true iff x is equal to an element of l. A comparator function eq 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 list l1 are contained in the list l2 by applying eq.

val uniq : eq:('a -> 'a -> bool) -> 'a t -> 'a t

uniq ~eq l removes duplicates in l w.r.t the equality predicate eq. 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, use sort_uniq.

val union : eq:('a -> 'a -> bool) -> 'a t -> 'a t -> 'a t

union ~eq l1 l2 is the union of the lists l1 and l2 w.r.t. the equality predicate eq. Complexity is product of length of inputs.

val inter : eq:('a -> 'a -> bool) -> 'a t -> 'a t -> 'a t

inter ~eq l1 l2 is the intersection of the lists l1 and l2 w.r.t. the equality predicate eq. Complexity is product of length of inputs.

Other Constructors

val range_by : step:int -> int -> int -> int t

range_by ~step i j iterates on integers from i to j included, where the difference between successive elements is step. Use a negative step 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 from i to j included. It works both for decreasing and increasing ranges.

val range' : int -> int -> int t

range' i j is like range but the second bound j is excluded. For instance range' 0 5 = [0;1;2;3;4].

val (--) : int -> int -> int t

i -- j is the list of integers from i to j included. Infix alias for range.

val (--^) : int -> int -> int t

i --^ j is the list of integers from i to j excluded. Infix alias for range'.

since
0.17
val replicate : int -> 'a -> 'a t

replicate n x replicates the given element x n times.

val repeat : int -> 'a t -> 'a t

repeat n l concatenates the list l with itself 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 value v associated with key k in alist. Like Assoc.get_exn.

since
2.0
val assoc_opt : eq:('a -> 'a -> bool) -> 'a -> ('a * 'b) t -> 'b option

assoc_opt ~eq k alist returns Some v if the given key k is present into alist, or None if not present. Like Assoc.get.

since
1.5, but only
since
2.0 with labels
val assq_opt : 'a -> ('a * 'b) t -> 'b option

assq_opt k alist returns Some v if the given key k is present into alist. Like Assoc.assoc_opt but use physical equality instead of structural equality to compare keys. Safe version of assq.

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 returns true iff k is a key in alist. Like Assoc.mem.

since
2.0
val remove_assoc : eq:('a -> 'a -> bool) -> 'a -> ('a * 'b) t -> ('a * 'b) t

remove_assoc ~eq k alist returns the alist without the first pair with key k, if any. Like Assoc.remove.

since
2.0

References on Lists

since
0.3.3
module Ref : sig ... end
module type MONAD = sig ... end
module Traverse : functor (M : 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 list l.

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 print l to a string using sep as a separator between elements of l.

since
2.7
val to_iter : 'a t -> 'a iter

to_iter l returns a iter of the elements of the list l.

since
2.8
val to_seq : 'a t -> 'a Stdlib.Seq.t

to_seq l returns a Seq.t of the elements of the list l. Renamed from to_std_seq since 3.0.

since
3.0
val of_iter : 'a iter -> 'a t

of_iter iter builds a list from a given iter. In the result, elements appear in the same order as they did in the source iter.

since
2.8
val of_seq_rev : 'a Stdlib.Seq.t -> 'a t

of_seq_rev seq builds a list from a given Seq.t, in reverse order. Renamed from of_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 given Seq.t. In the result, elements appear in the same order as they did in the source Seq.t. Renamed from of_std_seq since 3.0.

since
3.0
val to_gen : 'a t -> 'a gen

to_gen l returns a gen of the elements of the list l.

val of_gen : 'a gen -> 'a t

of_gen gen builds a list from a given gen. In the result, elements appear in the same order as they did in the source gen.

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
include CCShimsMkLet_.S with type 'a t_let := 'a list
type 'a t_let
val let+ : 'a t_let -> ('a -> 'b) -> 'b t_let
val and+ : 'a t_let -> 'b t_let -> ('a * 'b) t_let
val let* : 'a t_let -> ('a -> 'b t_let) -> 'b t_let
val and* : 'a t_let -> 'b t_let -> ('a * 'b) t_let

IO

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 ppf l prints the contents of a list.

\ No newline at end of file +- : int list = [4; 16; 4]
since
3.1
val (<*>) : ('a -> 'b) t -> 'a t -> 'b t

funs <*> l is product (fun f x -> f x) funs l.

val (<$>) : ('a -> 'b) -> 'a t -> 'b t

(<$>) is map.

val return : 'a -> 'a t

return x is x.

val (>>=) : 'a t -> ('a -> 'b t) -> 'b t

l >>= f is flat_map f l.

val take : int -> 'a t -> 'a t

take n l takes the n first elements of the list l, drop the rest.

val drop : int -> 'a t -> 'a t

drop n l drops the n first elements of the list l, keep the rest.

val hd_tl : 'a t -> 'a * 'a t

hd_tl (x :: l) returns hd, 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 returns l1, l2 such that l1 @ l2 = l and length l1 = min (length l) n.

val take_while : f:('a -> bool) -> 'a t -> 'a t

take_while ~f l returns the longest prefix of l for which f is true.

since
0.13
val drop_while : f:('a -> bool) -> 'a t -> 'a t

drop_while ~f l drops the longest prefix of l for which f is true.

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 last n elements of l (or less if l doesn't have that many elements).

val head_opt : 'a t -> 'a option

head_opt l returns Some x (the first element of the list l) or None if the list l is empty.

since
0.20
val tail_opt : 'a t -> 'a t option

tail_opt l returns Some l' (the given list l without its first element) or None if the list l is empty.

since
2.0
val last_opt : 'a t -> 'a option

last_opt l returns Some x (the last element of l) or None if the list l is empty.

since
0.20
val find_pred : f:('a -> bool) -> 'a t -> 'a option

find_pred ~f l finds the first element of l that satisfies f, or returns None if no element satisfies f.

since
0.11
val find_opt : f:('a -> bool) -> 'a t -> 'a option

find_opt ~f l is the safe version of find.

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 of find_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 traverses l, applying f to each element. If for some element x, f x = Some y, then Some y is returned. Otherwise the call returns None.

since
0.11
val find_mapi : f:(int -> 'a -> 'b option) -> 'a t -> 'b option

find_mapi ~f l is like find_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 returns Some (i,x) where x is the i-th element of l, and f x holds. Otherwise returns None.

val remove : eq:('a -> 'a -> bool) -> key:'a -> 'a t -> 'a t

remove ~eq ~key l removes every instance of key from l. 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 of l containing only elements for which f returns Some 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 form Some x. Like filter_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 form Ok x.

since
1.3, but only
since
2.2 with labels
val all_some : 'a option t -> 'a t option

all_some l returns Some l' if all elements of l are of the form Some x, or None 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 returns Ok l' if all elements of l are of the form Ok x, or Error e otherwise (with the first error met).

since
1.3, but only
since
2.2 with labels
val sorted_mem : cmp:('a -> 'a -> int) -> 'a -> 'a list -> bool

sorted_mem ~cmp x l and mem x l give the same result for any sorted list l, but potentially more efficiently.

since
NEXT_RELEASE
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 function cmp.

val sorted_diff : cmp:('a -> 'a -> int) -> 'a list -> 'a list -> 'a list

sorted_diff ~cmp l1 l2 returns the elements in l1 that are not in l2. Both lists are assumed to be sorted with respect to cmp and duplicate elements in the input lists are treated individually; for example, sorted_diff ~cmp [1;1;1;2;2;3] [1;2;2] would be [1;1;3]. It is the left inverse of sorted_merge; that is, sorted_diff ~cmp (sorted_merge ~cmp l1 l2) l2 is always equal to l1 for sorted lists l1 and l2.

since
NEXT_RELEASE
val sort_uniq : cmp:('a -> 'a -> int) -> 'a list -> 'a list

sort_uniq ~cmp l sorts the list l using the given comparison function cmp 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 lists l1 and l2 and removes duplicates.

since
0.10
val sorted_diff_uniq : cmp:('a -> 'a -> int) -> 'a list -> 'a list -> 'a list

sorted_diff_uniq ~cmp l1 l2 collects the elements in l1 that are not in l2 and then remove duplicates. Both lists are assumed to be sorted with respect to cmp and duplicate elements in the input lists are treated individually; for example, sorted_diff_uniq ~cmp [1;1;1;2;2] [1;2;2;2] would be [1]. sorted_diff_uniq ~cmp l1 l2 and uniq_succ ~eq (sorted_diff ~cmp l1 l2) always give the same result for sorted l1 and l2 and compatible cmp and eq.

since
NEXT_RELEASE
val is_sorted : cmp:('a -> 'a -> int) -> 'a list -> bool

is_sorted ~cmp l returns true iff l 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 inserts x into l such that, if l was sorted, then sorted_insert x l is sorted too.

parameter uniq

if true and x is already in sorted position in l, then x is not duplicated. Default false (x will be inserted in any case).

since
0.17
val sorted_remove : cmp:('a -> 'a -> int) -> ?⁠all:bool -> 'a -> 'a list -> 'a list

sorted_remove ~cmp x l removes x from a sorted list l such that the return value is sorted too. By default, it is the left inverse of sorted_insert; that is, sorted_remove ~cmp x (sorted_insert ~cmp x l) is equal to l for any sorted list l.

parameter all

if true then all occurrences of x will be removed. Otherwise, only the first x will be removed (if any). Default false (only the first will be removed).

since
NEXT_RELEASE
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 to eq.

since
0.11

Indices

val mapi : f:(int -> 'a -> 'b) -> 'a t -> 'b t

mapi ~f l is like map, but the function f 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 like iter, but the function f 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 applies f to the two lists l1 and l2 simultaneously. The integer passed to f 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 like fold but it also passes in the index of each element, from 0 to length l - 1 as additional argument to the folded function f. 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 lists l1 and l2, with index of each element passed to the function f. Computes f(… (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 returns Some i-th element of the given list l or None if the list l is too short. If the index is negative, it will get element starting from the end of the list l.

val nth_opt : 'a t -> int -> 'a option

nth_opt l n returns Some n-th element of l. Safe version of nth.

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 the i-th element of l, 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 the i-th element with x (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.

val insert_at_idx : int -> 'a -> 'a t -> 'a t

insert_at_idx i x l inserts x at i-th position, between the two existing elements. If the index is too high, append at the end of the list. If the index is negative, it will insert element starting from the end of the list.

val remove_at_idx : int -> 'a t -> 'a t

remove_at_idx i l removes element at given index i. Does nothing if the index is too high. If the index is negative, it will remove 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 adds x to set 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 of x from set. Linear time.

since
0.11
val mem : ?⁠eq:('a -> 'a -> bool) -> 'a -> 'a t -> bool

mem ?eq x l is true iff x is equal to an element of l. A comparator function eq 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 list l1 are contained in the list l2 by applying eq.

val uniq : eq:('a -> 'a -> bool) -> 'a t -> 'a t

uniq ~eq l removes duplicates in l w.r.t the equality predicate eq. 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, use sort_uniq.

val union : eq:('a -> 'a -> bool) -> 'a t -> 'a t -> 'a t

union ~eq l1 l2 is the union of the lists l1 and l2 w.r.t. the equality predicate eq. Complexity is product of length of inputs.

val inter : eq:('a -> 'a -> bool) -> 'a t -> 'a t -> 'a t

inter ~eq l1 l2 is the intersection of the lists l1 and l2 w.r.t. the equality predicate eq. Complexity is product of length of inputs.

Other Constructors

val range_by : step:int -> int -> int -> int t

range_by ~step i j iterates on integers from i to j included, where the difference between successive elements is step. Use a negative step 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 from i to j included. It works both for decreasing and increasing ranges.

val range' : int -> int -> int t

range' i j is like range but the second bound j is excluded. For instance range' 0 5 = [0;1;2;3;4].

val (--) : int -> int -> int t

i -- j is the list of integers from i to j included. Infix alias for range.

val (--^) : int -> int -> int t

i --^ j is the list of integers from i to j excluded. Infix alias for range'.

since
0.17
val replicate : int -> 'a -> 'a t

replicate n x replicates the given element x n times.

val repeat : int -> 'a t -> 'a t

repeat n l concatenates the list l with itself 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 value v associated with key k in alist. Like Assoc.get_exn.

since
2.0
val assoc_opt : eq:('a -> 'a -> bool) -> 'a -> ('a * 'b) t -> 'b option

assoc_opt ~eq k alist returns Some v if the given key k is present into alist, or None if not present. Like Assoc.get.

since
1.5, but only
since
2.0 with labels
val assq_opt : 'a -> ('a * 'b) t -> 'b option

assq_opt k alist returns Some v if the given key k is present into alist. Like Assoc.assoc_opt but use physical equality instead of structural equality to compare keys. Safe version of assq.

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 returns true iff k is a key in alist. Like Assoc.mem.

since
2.0
val remove_assoc : eq:('a -> 'a -> bool) -> 'a -> ('a * 'b) t -> ('a * 'b) t

remove_assoc ~eq k alist returns the alist without the first pair with key k, if any. Like Assoc.remove.

since
2.0

References on Lists

since
0.3.3
module Ref : sig ... end
module type MONAD = sig ... end
module Traverse : functor (M : 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 list l.

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 print l to a string using sep as a separator between elements of l.

since
2.7
val to_iter : 'a t -> 'a iter

to_iter l returns a iter of the elements of the list l.

since
2.8
val to_seq : 'a t -> 'a Stdlib.Seq.t

to_seq l returns a Seq.t of the elements of the list l. Renamed from to_std_seq since 3.0.

since
3.0
val of_iter : 'a iter -> 'a t

of_iter iter builds a list from a given iter. In the result, elements appear in the same order as they did in the source iter.

since
2.8
val of_seq_rev : 'a Stdlib.Seq.t -> 'a t

of_seq_rev seq builds a list from a given Seq.t, in reverse order. Renamed from of_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 given Seq.t. In the result, elements appear in the same order as they did in the source Seq.t. Renamed from of_std_seq since 3.0.

since
3.0
val to_gen : 'a t -> 'a gen

to_gen l returns a gen of the elements of the list l.

val of_gen : 'a gen -> 'a t

of_gen gen builds a list from a given gen. In the result, elements appear in the same order as they did in the source gen.

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
include CCShimsMkLet_.S with type 'a t_let := 'a list
type 'a t_let
val let+ : 'a t_let -> ('a -> 'b) -> 'b t_let
val and+ : 'a t_let -> 'b t_let -> ('a * 'b) t_let
val let* : 'a t_let -> ('a -> 'b t_let) -> 'b t_let
val and* : 'a t_let -> 'b t_let -> ('a * 'b) t_let

IO

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 ppf l prints the contents of a list.

\ No newline at end of file