package containers

  1. Overview
  2. Docs
A modular, clean and powerful extension of the OCaml standard library

Install

Dune Dependency

Authors

Maintainers

Sources

containers-3.15.tbz
sha256=92143ceb4785ae5f8a07f3ab4ab9f6f32d31ead0536e9be4fdb818dd3c677e58
sha512=5fa80189d0e177af2302b48e72b70299d51fc36ac2019e1cbf389ff6a7f4705b10089405b5a719b3e4845b0d1349a47a967f865dc2e4e3f0d5a0167ef6c31431

doc/containers/CCListLabels/index.html

Module CCListLabelsSource

Complements to ListLabels

Sourcetype 'a iter = ('a -> unit) -> unit

Fast internal iterator.

  • since 2.8
Sourcetype 'a gen = unit -> 'a option
Sourcetype 'a printer = Format.formatter -> 'a -> unit
Sourcetype 'a random_gen = Random.State.t -> 'a
Sourceval length : 'a list -> int

Return the length (number of elements) of the given list.

Sourceval hd : 'a list -> 'a

Return the first element of the given list.

  • raises Failure

    if the list is empty.

Sourceval tl : 'a list -> 'a list

Return the given list without its first element.

  • raises Failure

    if the list is empty.

Sourceval nth : 'a list -> int -> 'a

Return the n-th element of the given list. The first element (head of the list) is at position 0.

  • raises Failure

    if the list is too short.

Sourceval rev : 'a list -> 'a list

List reversal.

Sourceval rev_append : 'a list -> 'a list -> 'a list

rev_append l1 l2 reverses l1 and concatenates it with l2. This is equivalent to (rev l1) @ l2.

Sourceval concat : 'a list list -> 'a list

Concatenate a list of lists. The elements of the argument are all concatenated together (in the same order) to give the result. Not tail-recursive (length of the argument + length of the longest sub-list).

Comparison

Iterators

Sourceval iter : f:('a -> unit) -> 'a list -> unit

iter ~f [a1; ...; an] applies function f in turn to [a1; ...; an]. It is equivalent to f a1; f a2; ...; f an.

Sourceval rev_map : f:('a -> 'b) -> 'a list -> 'b list

rev_map ~f l gives the same result as rev (map f l), but is more efficient.

Sourceval concat_map : f:('a -> 'b list) -> 'a list -> 'b list

concat_map ~f l gives the same result as concat (map f l). Tail-recursive.

  • since 4.10
Sourceval fold_left_map : f:('acc -> 'a -> 'acc * 'b) -> init:'acc -> 'a list -> 'acc * 'b list

fold_left_map is a combination of fold_left and map that threads an accumulator through calls to f.

  • since 4.11
Sourceval fold_left : f:('acc -> 'a -> 'acc) -> init:'acc -> 'a list -> 'acc

fold_left ~f ~init [b1; ...; bn] is f (... (f (f init b1) b2) ...) bn.

Iterators on two lists

Sourceval iter2 : f:('a -> 'b -> unit) -> 'a list -> 'b list -> unit

iter2 ~f [a1; ...; an] [b1; ...; bn] calls in turn f a1 b1; ...; f an bn.

  • raises Invalid_argument

    if the two lists are determined to have different lengths.

Sourceval map2 : f:('a -> 'b -> 'c) -> 'a list -> 'b list -> 'c list

map2 ~f [a1; ...; an] [b1; ...; bn] is [f a1 b1; ...; f an bn].

  • raises Invalid_argument

    if the two lists are determined to have different lengths.

Sourceval rev_map2 : f:('a -> 'b -> 'c) -> 'a list -> 'b list -> 'c list

rev_map2 ~f l1 l2 gives the same result as rev (map2 f l1 l2), but is more efficient.

Sourceval fold_left2 : f:('acc -> 'a -> 'b -> 'acc) -> init:'acc -> 'a list -> 'b list -> 'acc

fold_left2 ~f ~init [a1; ...; an] [b1; ...; bn] is f (... (f (f init a1 b1) a2 b2) ...) an bn.

  • raises Invalid_argument

    if the two lists are determined to have different lengths.

Sourceval fold_right2 : f:('a -> 'b -> 'acc -> 'acc) -> 'a list -> 'b list -> init:'acc -> 'acc

fold_right2 ~f [a1; ...; an] [b1; ...; bn] ~init is f a1 b1 (f a2 b2 (... (f an bn init) ...)).

  • raises Invalid_argument

    if the two lists are determined to have different lengths. Not tail-recursive.

List scanning

Sourceval for_all : f:('a -> bool) -> 'a list -> bool

for_all ~f [a1; ...; an] checks if all elements of the list satisfy the predicate f. That is, it returns (f a1) && (f a2) && ... && (f an) for a non-empty list and true if the list is empty.

Sourceval exists : f:('a -> bool) -> 'a list -> bool

exists ~f [a1; ...; an] checks if at least one element of the list satisfies the predicate f. That is, it returns (f a1) || (f a2) || ... || (f an) for a non-empty list and false if the list is empty.

Sourceval for_all2 : f:('a -> 'b -> bool) -> 'a list -> 'b list -> bool

Same as for_all, but for a two-argument predicate.

  • raises Invalid_argument

    if the two lists are determined to have different lengths.

Sourceval exists2 : f:('a -> 'b -> bool) -> 'a list -> 'b list -> bool

Same as exists, but for a two-argument predicate.

  • raises Invalid_argument

    if the two lists are determined to have different lengths.

Sourceval memq : 'a -> set:'a list -> bool

Same as mem, but uses physical equality instead of structural equality to compare list elements.

List searching

Sourceval find : f:('a -> bool) -> 'a list -> 'a

find ~f l returns the first element of the list l that satisfies the predicate f.

  • raises Not_found

    if there is no value that satisfies f in the list l.

Sourceval find_index : f:('a -> bool) -> 'a list -> int option

find_index ~f xs returns Some i, where i is the index of the first element of the list xs that satisfies f x, if there is such an element.

It returns None if there is no such element.

  • since 5.1
Sourceval find_all : f:('a -> bool) -> 'a list -> 'a list

find_all is another name for filter.

Sourceval filteri : f:(int -> 'a -> bool) -> 'a list -> 'a list

Same as filter, but the predicate is applied to the index of the element as first argument (counting from 0), and the element itself as second argument.

  • since 4.11

List manipulation

Sourceval partition : f:('a -> bool) -> 'a list -> 'a list * 'a list

partition ~f l returns a pair of lists (l1, l2), where l1 is the list of all the elements of l that satisfy the predicate f, and l2 is the list of all the elements of l that do not satisfy f. The order of the elements in the input list is preserved.

Association lists

Sourceval assq : 'a -> ('a * 'b) list -> 'b

Same as assoc, but uses physical equality instead of structural equality to compare keys.

Sourceval mem_assq : 'a -> map:('a * 'b) list -> bool

Same as mem_assoc, but uses physical equality instead of structural equality to compare keys.

Sourceval remove_assq : 'a -> ('a * 'b) list -> ('a * 'b) list

Same as remove_assoc, but uses physical equality instead of structural equality to compare keys. Not tail-recursive.

Lists of pairs

Sorting

Sourceval sort : cmp:('a -> 'a -> int) -> 'a list -> 'a list

Sort a list in increasing order according to a comparison function. The comparison function must return 0 if its arguments compare as equal, a positive integer if the first is greater, and a negative integer if the first is smaller (see Array.sort for a complete specification). For example, Stdlib.compare is a suitable comparison function. The resulting list is sorted in increasing order. sort is guaranteed to run in constant heap space (in addition to the size of the result list) and logarithmic stack space.

The current implementation uses Merge Sort. It runs in constant heap space and logarithmic stack space.

Sourceval stable_sort : cmp:('a -> 'a -> int) -> 'a list -> 'a list

Same as sort, but the sorting algorithm is guaranteed to be stable (i.e. elements that compare equal are kept in their original order).

The current implementation uses Merge Sort. It runs in constant heap space and logarithmic stack space.

Sourceval fast_sort : cmp:('a -> 'a -> int) -> 'a list -> 'a list

Same as sort or stable_sort, whichever is faster on typical input.

Sourceval merge : cmp:('a -> 'a -> int) -> 'a list -> 'a list -> 'a list

Merge two lists: Assuming that l1 and l2 are sorted according to the comparison function cmp, merge ~cmp l1 l2 will return a sorted list containing all the elements of l1 and l2. If several elements compare equal, the elements of l1 will be before the elements of l2. Not tail-recursive (sum of the lengths of the arguments).

Lists and Sequences

Sourcetype 'a t = 'a list
Sourceval empty : 'a t

empty is [].

Sourceval is_empty : _ t -> bool

is_empty l returns true iff l = [].

  • since 0.11
Sourceval map : f:('a -> 'b) -> 'a t -> 'b t

map ~f [a0; a1; …; an] applies function f in turn to [a0; a1; …; an]. Safe version of List.map.

Sourceval cons : 'a -> 'a t -> 'a t

cons x l is x::l.

  • since 0.12
Sourceval append : 'a t -> 'a t -> 'a t

append l1 l2 returns the list that is the concatenation of l1 and l2. Safe version of List.append.

Sourceval cons' : 'a t -> 'a -> 'a t

cons' l x is the same as x :: l. This is convenient for fold functions such as List.fold_left or Array.fold_left.

  • since 3.3
Sourceval cons_maybe : 'a option -> 'a t -> 'a t

cons_maybe (Some x) l is x :: l. cons_maybe None l is l.

  • since 0.13
Sourceval cons_when : bool -> 'a -> 'a t -> 'a t

cons_when true x l is x :: l. cons_when false x l is l.

  • since 3.13.1
Sourceval filter : f:('a -> bool) -> 'a t -> 'a t

filter ~f l returns all the elements of the list l that satisfy the predicate f. The order of the elements in the input list l is preserved. Safe version of List.filter.

Sourceval fold_right : f:('a -> 'b -> 'b) -> 'a t -> init:'b -> 'b

fold_right ~f [a1; …; an] ~init is f a1 (f a2 ( … (f an init) … )). Safe version of List.fold_right.

Sourceval 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
Sourceval fold_map : f:('acc -> 'a -> 'acc * 'b) -> init:'acc -> 'a list -> 'acc * 'b list

fold_map ~f ~init l is a fold_left-like function, but it also maps the list to another list.

  • since 0.14
Sourceval fold_map_i : f:('acc -> int -> 'a -> 'acc * 'b) -> init:'acc -> 'a list -> 'acc * 'b list

fold_map_i ~f ~init l is a foldi-like function, but it also maps the list to another list.

  • since 2.8
Sourceval fold_on_map : f:('a -> 'b) -> reduce:('acc -> 'b -> 'acc) -> init:'acc -> 'a list -> 'acc

fold_on_map ~f ~reduce ~init l combines map ~f and fold_left ~reduce ~init in one operation.

  • since 2.8
Sourceval 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; …] where x0, x1, etc. are the elements of l.

  • since 1.2, but only
  • since 2.2 with labels
Sourceval reduce : f:('a -> 'a -> 'a) -> 'a list -> 'a option

reduce f (hd::tl) returns Some (fold_left f hd tl). If l is empty, then None is returned.

  • since 3.2
Sourceval reduce_exn : f:('a -> 'a -> 'a) -> 'a list -> 'a

reduce_exn is the unsafe version of reduce.

  • since 3.2
Sourceval fold_map2 : f:('acc -> 'a -> 'b -> 'acc * 'c) -> init:'acc -> 'a list -> 'b list -> 'acc * 'c list

fold_map2 ~f ~init l1 l2 is to fold_map what List.map2 is to List.map.

  • since 0.16
Sourceval fold_filter_map : f:('acc -> 'a -> 'acc * 'b option) -> init:'acc -> 'a list -> 'acc * 'b list

fold_filter_map ~f ~init l is a fold_left-like function, but also generates a list of output in a way similar to filter_map.

  • since 0.17
Sourceval 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 a foldi-like function, but also generates a list of output in a way similar to filter_map.

  • since 2.8
Sourceval fold_flat_map : f:('acc -> 'a -> 'acc * 'b list) -> init:'acc -> 'a list -> 'acc * 'b list

fold_flat_map ~f ~init l is a fold_left-like function, but it also maps the list to a list of lists that is then flatten'd.

  • since 0.14
Sourceval 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 a fold_left-like function, but it also maps the list to a list of lists that is then flatten'd.

  • since 2.8
Sourceval unfold : f:('seed -> ('b * 'seed) option) -> init:'seed -> 'b list

unfold ~f ~init builds up a list from a seed value. When f produces Some (next_seed, value), value is added to the output list and next_seed is used in the next call to f. However, when f produces None, list production ends. NOTE if f never produces None, then a stack overflow will occur. Therefore, great care must be taken to ensure that f will produce None.

  • since 3.13
Sourceval count : f:('a -> bool) -> 'a list -> int

count ~f l counts how many elements of l satisfy predicate f.

  • since 1.5, but only
  • since 2.2 with labels
Sourceval count_true_false : f:('a -> bool) -> 'a list -> int * int

count_true_false ~f l returns a pair (int1,int2) where int1 is the number of elements in l that satisfy the predicate f, and int2 the number of elements that do not satisfy f.

  • since 2.4
Sourceval init : int -> f:(int -> 'a) -> 'a t

init len ~f is f 0; f 1; …; f (len-1).

  • since 0.6
Sourceval 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. Like List.combine but tail-recursive.

  • since 1.2, but only
  • since 2.2 with labels
Sourceval combine_gen : 'a list -> 'b list -> ('a * 'b) gen

combine_gen l1 l2 transforms two lists into a gen of pairs. Lazy version of combine. Unlike combine, 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
Sourceval combine_shortest : 'a list -> 'b list -> ('a * 'b) list

combine_shortest [a1; …; am] [b1; …; bn] is [(a1,b1); …; (am,bm)] if m <= n. Like combine but stops at the shortest list rather than raising.

  • since 3.1
Sourceval 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 of List.split.

  • since 1.2, but only
  • since 2.2 with labels
Sourceval compare : ('a -> 'a -> int) -> 'a t -> 'a t -> int

compare cmp l1 l2 compares the two lists l1 and l2 using the given comparison function cmp.

Sourceval compare_lengths : 'a t -> 'b t -> int

compare_lengths l1 l2 compare the lengths of the two lists l1 and l2. Equivalent to compare (length l1) (length l2) but more efficient.

  • since 1.5, but only
  • since 2.2 with labels
Sourceval compare_length_with : 'a t -> int -> int

compare_length_with l x compares the length of the list l to an integer x. Equivalent to compare (length l) x but more efficient.

  • since 1.5, but only
  • since 2.2 with labels
Sourceval equal : ('a -> 'a -> bool) -> 'a t -> 'a t -> bool

equal p l1 l2 returns true if l1 and l2 are equal.

Sourceval 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.

Sourceval 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
Sourceval flatten : 'a t t -> 'a t

flatten [l1]; [l2]; … concatenates a list of lists. Safe version of List.flatten.

Sourceval 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 combinator f.

Sourceval fold_product : f:('c -> 'a -> 'b -> 'c) -> init:'c -> 'a t -> 'b t -> 'c

fold_product ~f ~init l1 l2 applies the function f with the accumulator init on all the pair of elements of l1 and l2. Fold on the cartesian product.

Sourceval 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
Sourceval map_product_l : f:('a -> 'b list) -> 'a list -> 'b list list

map_product_l ~f l maps each element of l to a list of objects of type 'b using f. We obtain [l1; l2; …; ln] where length l=n and li : 'b list. Then, it returns all the ways of picking exactly one element per li.

  • since 1.2, but only
  • since 2.2 with labels
Sourceval diagonal : 'a t -> ('a * 'a) t

diagonal l returns all pairs of distinct positions of the list l, that is the list of List.nth i l, List.nth j l if i < j.

Sourceval partition_map_either : f:('a -> ('b, 'c) CCEither.t) -> 'a list -> 'b list * 'c list

partition_map_either ~f l maps f on l and gather results in lists:

  • if f x = Left y, adds y to the first list.
  • if f x = Right z, adds z to the second list.
  • since 3.3
Sourceval partition_filter_map : f:('a -> [< `Left of 'b | `Right of 'c | `Drop ]) -> 'a list -> 'b list * 'c list

partition_filter_map ~f l maps f on l and gather results in lists:

  • if f x = `Left y, adds y to the first list.
  • if f x = `Right z, adds z to the second list.
  • if f x = `Drop, ignores x.
  • since 0.11
Sourceval partition_map : f:('a -> [< `Left of 'b | `Right of 'c | `Drop ]) -> 'a list -> 'b list * 'c list
Sourceval 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 any x and y, if eq x y then hash x = hash y must hold.

  • since 2.3
Sourceval join : join_row:('a -> 'b -> 'c option) -> 'a t -> 'b t -> 'c t

join ~join_row a b combines every element of a with every element of b using join_row. If join_row returns None, then the two elements do not combine. Assume that b allows for multiple iterations.

  • since 2.3
Sourceval 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 sequences a and b, projects their elements resp. with key1 and key2, and combine values (x,y) from (a,b) with the same key using merge. If merge returns None, the combination of values is discarded. precondition: for any x and y, if eq x y then hash x = hash y must hold.

  • since 2.3
Sourceval 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 sequences a and b, projects their elements resp. with key1 and key2, and, for each key k occurring in at least one of them:

  • compute the list l1 of elements of a that map to k
  • compute the list l2 of elements of b that map to k
  • call merge k l1 l2. If merge returns None, the combination of values is discarded, otherwise it returns Some c and c is inserted in the result.
  • since 2.3
Sourceval 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 element x of the first sequence, all the elements y of the second sequence such that eq x (key y). Elements of the first sequences without corresponding values in the second one are mapped to [] precondition: for any x and y, if eq x y then hash x = hash y must hold.

  • since 2.3
Sourceval 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 of l that have length n. 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:CCOption.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. If offset < n, the sub-lists will overlap; if offset > n, some elements will not appear at all.

  • parameter last

    if provided and the last group of elements g is such that length g < n, last g is called. If last g = Some g', g' is appended; otherwise g is dropped. If last = CCOption.return, it will simply keep the last group. By default, last = fun _ -> None, i.e. the last group is dropped if shorter than n.

  • since 1.0, but only
  • since 1.5 with labels
Sourceval chunks : int -> 'a list -> 'a list list

chunks n l returns consecutives chunks of size at most n from l. Each item of l will occur in exactly one chunk. Only the last chunk might be of length smaller than n. Invariant: (chunks n l |> List.flatten) = l.

  • since 3.2
Sourceval intersperse : x:'a -> 'a list -> 'a list

intersperse ~x l inserts the element x between adjacent elements of the list l.

  • since 2.1, but only
  • since 2.2 with labels
Sourceval 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
Sourceval pure : 'a -> 'a t

pure x is return x.

Sourceval mguard : bool -> unit t

mguard c is pure () if c 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
Sourceval return : 'a -> 'a t

return x is x.

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

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

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

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

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

hd_tl (x :: l) returns x, l.

  • raises Failure

    if the list is empty.

  • since 0.16
Sourceval 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.

Sourceval 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
Sourceval 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
Sourceval 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
Sourceval 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).

Sourceval 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
Sourceval 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
Sourceval 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
Sourceval 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
Sourceval 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
Sourceval 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
Sourceval 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
Sourceval 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
Sourceval 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.

Sourceval 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
Sourceval 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.

Sourceval 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
Sourceval keep_ok : ('a, _) 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
Sourceval 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
Sourceval all_ok : ('a, 'err) result t -> ('a t, 'err) 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
Sourceval split_result : ('ok, 'error) result list -> 'ok list * 'error list

Split a list of results into Oks and Errors.

  • since 3.14
Sourceval 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 3.5
Sourceval 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.

Sourceval 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 3.5
Sourceval 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.

Sourceval 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
Sourceval 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 3.5
Sourceval 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
Sourceval 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
Sourceval 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 3.5
Sourceval 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
Sourceval 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

Sourceval 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.

Sourceval 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.

Sourceval 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.

  • since 2.0, but only
  • since 2.2 with labels
Sourceval 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.

Sourceval 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 .

  • since 2.0, but only
  • since 2.2 with labels
Sourceval 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.

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

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

  • since 1.5, but only
  • since 2.2 with labels
Sourceval 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.

Sourceval 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.

Sourceval 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.

Sourceval 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).

Sourceval 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
Sourceval 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
Sourceval 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.

Sourceval 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.

Sourceval 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.

Sourceval 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.

Sourceval 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

Sourceval 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.

  • since 0.18
Sourceval range : int -> int -> int t

range i j iterates on integers from i to j included. It works both for decreasing and increasing ranges.

Sourceval 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].

Sourceval replicate : int -> 'a -> 'a t

replicate n x replicates the given element x n times.

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

repeat n l concatenates the list l with itself n times.

Association Lists

Sourcemodule Assoc : sig ... end
Sourceval 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
Sourceval 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
Sourceval 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
Sourceval 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
Sourceval 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
Sourcemodule Ref : sig ... end
Sourcemodule type MONAD = sig ... end
Sourcemodule Traverse (M : MONAD) : sig ... end

Conversions

Sourceval random : 'a random_gen -> 'a t random_gen
Sourceval random_non_empty : 'a random_gen -> 'a t random_gen
Sourceval random_len : int -> 'a random_gen -> 'a t random_gen
Sourceval random_choose : 'a t -> 'a random_gen

random_choose l randomly chooses an element in the list l.

Sourceval random_sequence : 'a random_gen t -> 'a t random_gen
Sourceval 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
Sourceval to_iter : 'a t -> 'a iter

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

  • since 2.8
Sourceval to_seq : 'a t -> 'a 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
Sourceval 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
Sourceval of_seq_rev : 'a 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
Sourceval of_seq : 'a 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
Sourceval to_gen : 'a t -> 'a gen

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

Sourceval 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
Sourcemodule Infix : module type of CCList.Infix
include module type of Infix
Sourceval (>|=) : 'a CCList.t -> ('a -> 'b) -> 'b CCList.t

l >|= f is the infix version of map with reversed arguments.

Sourceval (@) : 'a CCList.t -> 'a CCList.t -> 'a CCList.t

l1 @ l2 concatenates two lists l1 and l2. As append.

Sourceval (<*>) : ('a -> 'b) CCList.t -> 'a CCList.t -> 'b CCList.t

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

Sourceval (<$>) : ('a -> 'b) -> 'a CCList.t -> 'b CCList.t

f <$> l is like map.

Sourceval (>>=) : 'a CCList.t -> ('a -> 'b CCList.t) -> 'b CCList.t

l >>= f is flat_map f l.

Sourceval (--) : int -> int -> int CCList.t

i -- j is the infix alias for range. Bounds included.

Sourceval (--^) : int -> int -> int CCList.t

i --^ j is the infix alias for range'. Second bound j excluded.

  • since 0.17
Sourceval (let+) : 'a CCList.t -> ('a -> 'b) -> 'b CCList.t
Sourceval (and+) : 'a CCList.t -> 'b CCList.t -> ('a * 'b) CCList.t
Sourceval (let*) : 'a CCList.t -> ('a -> 'b CCList.t) -> 'b CCList.t
Sourceval (and*) : 'a CCList.t -> 'b CCList.t -> ('a * 'b) CCList.t
Sourceval (and&) : 'a list -> 'b list -> ('a * 'b) list

(and&) is combine_shortest. It allows to perform a synchronized product between two lists, stopping gently at the shortest. Usable both with let+ and let*.

    # let f xs ys zs =
        let+ x = xs
        and& y = ys
        and& z = zs in
        x + y + z;;
    val f : int list -> int list -> int list -> int list = <fun>
    # f [1;2] [5;6;7] [10;10];;
    - : int list = [16; 18]
  • since 3.1

IO

Sourceval 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.

OCaml

Innovation. Community. Security.