package containers

  1. Overview
  2. Docs
Legend:
Library
Module
Module type
Parameter
Class
Class type

Complements to list

type 'a sequence = ('a -> unit) -> unit
  • deprecated

    use 'a iter instead

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

Fast internal iterator.

  • since 2.8
type 'a gen = unit -> 'a option
type 'a klist = unit -> [ `Nil | `Cons of 'a * 'a klist ]
type 'a printer = Format.formatter -> 'a -> unit
type 'a random_gen = Random.State.t -> 'a
include module type of List
val length : 'a list -> int

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

val hd : 'a list -> 'a

Return the first element of the given list. Raise Failure "hd" if the list is empty.

val tl : 'a list -> 'a list

Return the given list without its first element. Raise Failure "tl" if the list is empty.

val 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. Raise Failure "nth" if the list is too short. Raise Invalid_argument "List.nth" if n is negative.

val rev : 'a list -> 'a list

List reversal.

val rev_append : 'a list -> 'a list -> 'a list

List.rev_append l1 l2 reverses l1 and concatenates it to l2. This is equivalent to List.rev l1 @ l2, but rev_append is tail-recursive and more efficient.

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

Iterators

val iter : ('a -> unit) -> 'a list -> unit

List.iter f [a1; ...; an] applies function f in turn to a1; ...; an. It is equivalent to begin f a1; f a2; ...; f an; () end.

val rev_map : ('a -> 'b) -> 'a list -> 'b list

List.rev_map f l gives the same result as List.rev (List.map f l), but is tail-recursive and more efficient.

val fold_left : ('a -> 'b -> 'a) -> 'a -> 'b list -> 'a

List.fold_left f a [b1; ...; bn] is f (... (f (f a b1) b2) ...) bn.

Iterators on two lists

val iter2 : ('a -> 'b -> unit) -> 'a list -> 'b list -> unit

List.iter2 f [a1; ...; an] [b1; ...; bn] calls in turn f a1 b1; ...; f an bn. Raise Invalid_argument if the two lists are determined to have different lengths.

val map2 : ('a -> 'b -> 'c) -> 'a list -> 'b list -> 'c list

List.map2 f [a1; ...; an] [b1; ...; bn] is [f a1 b1; ...; f an bn]. Raise Invalid_argument if the two lists are determined to have different lengths. Not tail-recursive.

val rev_map2 : ('a -> 'b -> 'c) -> 'a list -> 'b list -> 'c list

List.rev_map2 f l1 l2 gives the same result as List.rev (List.map2 f l1 l2), but is tail-recursive and more efficient.

val fold_left2 : ('a -> 'b -> 'c -> 'a) -> 'a -> 'b list -> 'c list -> 'a

List.fold_left2 f a [b1; ...; bn] [c1; ...; cn] is f (... (f (f a b1 c1) b2 c2) ...) bn cn. Raise Invalid_argument if the two lists are determined to have different lengths.

val fold_right2 : ('a -> 'b -> 'c -> 'c) -> 'a list -> 'b list -> 'c -> 'c

List.fold_right2 f [a1; ...; an] [b1; ...; bn] c is f a1 b1 (f a2 b2 (... (f an bn c) ...)). Raise Invalid_argument if the two lists are determined to have different lengths. Not tail-recursive.

List scanning

val for_all : ('a -> bool) -> 'a list -> bool

for_all p [a1; ...; an] checks if all elements of the list satisfy the predicate p. That is, it returns (p a1) && (p a2) && ... && (p an).

val exists : ('a -> bool) -> 'a list -> bool

exists p [a1; ...; an] checks if at least one element of the list satisfies the predicate p. That is, it returns (p a1) || (p a2) || ... || (p an).

val for_all2 : ('a -> 'b -> bool) -> 'a list -> 'b list -> bool

Same as List.for_all, but for a two-argument predicate. Raise Invalid_argument if the two lists are determined to have different lengths.

val exists2 : ('a -> 'b -> bool) -> 'a list -> 'b list -> bool

Same as List.exists, but for a two-argument predicate. Raise Invalid_argument if the two lists are determined to have different lengths.

val memq : 'a -> 'a list -> bool

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

List searching

val find : ('a -> bool) -> 'a list -> 'a

find p l returns the first element of the list l that satisfies the predicate p. Raise Not_found if there is no value that satisfies p in the list l.

val find_all : ('a -> bool) -> 'a list -> 'a list

find_all is another name for List.filter.

val partition : ('a -> bool) -> 'a list -> 'a list * 'a list

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

Association lists

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

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

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

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

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

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

Lists of pairs

Sorting

val sort : ('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, Pervasives.compare is a suitable comparison function. The resulting list is sorted in increasing order. List.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.

val stable_sort : ('a -> 'a -> int) -> 'a list -> 'a list

Same as List.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.

val fast_sort : ('a -> 'a -> int) -> 'a list -> 'a list

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

val merge : ('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).

Iterators
type 'a t = 'a list
val empty : 'a t

empty is [].

val is_empty : _ t -> bool

is_empty l returns true iff l = [].

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

Safe version of List.map.

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

Infix version of map with reversed arguments.

  • since 0.5
val cons : 'a -> 'a t -> 'a t

cons x l is x::l.

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

Safe version of List.append. Concatenate two lists.

val 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
val (@) : 'a t -> 'a t -> 'a t

Like append. Concatenate two lists.

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

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

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

Safe version of fold_right. fold_right f [a1; ...; an] b is f a1 (f a2 (... (f an b) ...)).

val fold_while : ('a -> 'b -> 'a * [ `Stop | `Continue ]) -> 'a -> 'b t -> 'a

Fold until a stop condition via ('a, `Stop) is indicated by the accumulator.

  • since 0.8
val fold_map : ('acc -> 'a -> 'acc * 'b) -> '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
val fold_map_i : ('acc -> int -> 'a -> 'acc * 'b) -> '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
val fold_on_map : f:('a -> 'b) -> reduce:('acc -> 'b -> 'acc) -> '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
val scan_left : ('acc -> 'a -> 'acc) -> '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
val fold_map2 : ('acc -> 'a -> 'b -> 'acc * 'c) -> 'acc -> 'a list -> 'b list -> 'acc * 'c list

fold_map2 is to fold_map what List.map2 is to List.map.

  • raises Invalid_argument

    if the lists do not have the same length.

  • since 0.16
val fold_filter_map : ('acc -> 'a -> 'acc * 'b option) -> '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
val fold_filter_map_i : ('acc -> int -> 'a -> 'acc * 'b option) -> '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
val fold_flat_map : ('acc -> 'a -> 'acc * 'b list) -> 'acc -> 'a list -> 'acc * 'b list

fold_flat_map f acc 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
val fold_flat_map_i : ('acc -> int -> 'a -> 'acc * 'b list) -> 'acc -> 'a list -> 'acc * 'b list

fold_flat_map_i f acc 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
val count : ('a -> bool) -> 'a list -> int

count p l counts how many elements of l satisfy predicate p.

  • since 1.5, but only
  • since 2.2 with labels
val count_true_false : ('a -> bool) -> 'a list -> int * int
  • since 2.4
val init : int -> (int -> 'a) -> 'a t

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

  • raises Invalid_argument

    if len < 0.

  • since 0.6
val combine : 'a list -> 'b list -> ('a * 'b) list

Like List.combine but tail-recursive. Transform a pair of lists into a list of pairs: combine [a1; ...; an] [b1; ...; bn] is [(a1,b1); ...; (an,bn)].

  • raises Invalid_argument

    if the lists have distinct lengths.

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

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
val split : ('a * 'b) t -> 'a t * 'b t

A tail-recursive version of List.split. Transform a list of pairs into a pair of lists: split [(a1,b1); ...; (an,bn)] is ([a1; ...; an], [b1; ...; bn]).

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

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

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

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

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

Map and flatten at the same time (safe). Evaluation order is not guaranteed.

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

Map with index and flatten at the same time (safe). Evaluation order is not guaranteed.

  • since 2.8
val flatten : 'a t t -> 'a t

Safe flatten. Concatenate a list of lists.

val product : ('a -> 'b -> 'c) -> 'a t -> 'b t -> 'c t

Cartesian product of the two lists, with the given combinator.

val fold_product : ('c -> 'a -> 'b -> 'c) -> 'c -> 'a t -> 'b t -> 'c

Fold on the cartesian product.

val cartesian_product : 'a t t -> 'a t t

Produce the cartesian product of this list of lists, by returning all the ways of picking one element per sublist. NOTE the order of the returned list is unspecified. For example:

# cartesian_product [[1;2];[3];[4;5;6]] |> sort =
[[1;3;4];[1;3;5];[1;3;6];[2;3;4];[2;3;5];[2;3;6]];;
# cartesian_product [[1;2];[];[4;5;6]] = [];;
# cartesian_product [[1;2];[3];[4];[5];[6]] |> sort =
[[1;3;4;5;6];[2;3;4;5;6]];;

invariant: cartesian_product l = map_product id l.

  • since 1.2, but only
  • since 2.2 with labels
val map_product_l : ('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
val diagonal : 'a t -> ('a * 'a) t

All pairs of distinct positions of the list. list_diagonal l will return the list of List.nth i l, List.nth j l if i < j.

val partition_map : ('a -> [< `Left of 'b | `Right of 'c | `Drop ]) -> 'a list -> 'b list * 'c list

partition_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
val group_by : ?hash:('a -> int) -> ?eq:('a -> 'a -> bool) -> 'a t -> 'a list t

Group 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
val 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
val join_by : ?eq:('key -> 'key -> bool) -> ?hash:('key -> int) -> ('a -> 'key) -> ('b -> 'key) -> merge:('key -> 'a -> 'b -> 'c option) -> 'a t -> 'b t -> 'c t

join key1 key2 ~merge 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
val join_all_by : ?eq:('key -> 'key -> bool) -> ?hash:('key -> int) -> ('a -> 'key) -> ('b -> 'key) -> merge:('key -> 'a list -> 'b list -> 'c option) -> 'a t -> 'b t -> 'c t

join_all_by key1 key2 ~merge 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
val group_join_by : ?eq:('a -> 'a -> bool) -> ?hash:('a -> int) -> ('b -> 'a) -> 'a t -> 'b t -> ('a * 'b list) t

group_join_by key2 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
val sublists_of_len : ?last:('a list -> 'a list option) -> ?offset:int -> int -> 'a list -> 'a list list

sublists_of_len 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:CCOpt.return [1;2;3;4] = [1;2;3];[4].
  • sublists_of_len 2 [1;2;3;4;5] = [[1;2]; [3;4]].
  • parameter offset

    the number of elements skipped between two consecutive sub-lists. By default it is n. 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 = CCOpt.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
val intersperse : 'a -> 'a list -> 'a list

Insert the first argument between every element of the list.

  • since 2.1, but only
  • since 2.2 with labels
val interleave : 'a list -> 'a list -> 'a list

interleave [x1…xn] [y1…ym] is x1,y1,x2,y2,… and finishes with the suffix of the longest list.

  • since 2.1, but only
  • since 2.2 with labels
val pure : 'a -> 'a t

pure is return.

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 the n first elements, drop the rest.

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

Drop the n first elements, 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

First element.

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

Return the given list without its first element.

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

Last element.

  • 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

Safe version of find.

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

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

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 ~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, _) 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) 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
val sorted_merge : cmp:('a -> 'a -> int) -> 'a list -> 'a list -> 'a list

Merge elements from both sorted list.

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

Sort the list and remove duplicate elements.

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

sorted_merge_uniq 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 l returns true iff l is sorted (according to given order).

  • parameter cmp

    the comparison function (default Pervasives.compare).

  • since 0.17
val sorted_insert : cmp:('a -> 'a -> int) -> ?uniq:bool -> 'a -> 'a list -> 'a list

sorted_insert 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 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 : (int -> 'a -> 'b) -> 'a t -> 'b t

Like map, but the function 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

Like iter, but the function 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

Iter on two lists.

  • 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

Like fold but it also passes in the index of each element to the folded function. Tail-recursive.

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

Fold on two lists, with index.

  • 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 by index in the list. If the index is negative, it will get element starting from the end of the list.

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

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 the i-th element, or

  • raises Not_found

    if the index is invalid. 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 i-th element (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 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 element at given index. 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 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 x set removes one occurrence of x from set. Linear time.

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

Membership to the list. Linear time.

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

Test for inclusion.

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

Remove duplicates w.r.t the equality predicate. 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

List union. Complexity is product of length of inputs.

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

List intersection. 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

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

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

Infix alias for range.

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

Infix alias for range'.

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

Replicate the given element n times.

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

Concatenate the list with itself n times.

Association Lists

module Assoc : sig ... end
val assoc : eq:('a -> 'a -> bool) -> 'a -> ('a * 'b) t -> 'b

Like Assoc.get_exn.

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

Like Assoc.get.

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

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

Like Assoc.mem.

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

Like Assoc.remove.

  • since 2.0

References on Lists

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

Randomly choose an element in the list.

  • 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

Return a iter of the elements of the list.

  • since 2.8
val to_std_seq : 'a t -> 'a Seq.t

Return a Seq.t of the elements of the list.

  • since 2.8
val to_seq : 'a t -> 'a sequence
  • deprecated use to_iter or to_std_seq
val of_iter : 'a iter -> 'a t

Build 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_std_seq_rev : 'a Seq.t -> 'a t

Build a list from a given Seq.t, in reverse order.

  • since 2.8
val of_std_seq : 'a Seq.t -> 'a t

Build a list from a given Seq.t. In the result, elements appear in the same order as they did in the source seq.

  • since 2.8
val of_seq : 'a sequence -> 'a t
  • deprecated use of_iter or of_std_seq
val to_gen : 'a t -> 'a gen

Return a gen of the elements of the list.

val of_gen : 'a gen -> 'a t

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

val to_klist : 'a t -> 'a klist

Return a klist of the elements of the list.

val of_klist : 'a klist -> 'a t

Build a list from a given klist. In the result, elements appear in the same order as they did in the source klist.

Infix Operators

It is convenient to openCCList.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

IO

val pp : ?start:string -> ?stop:string -> ?sep:string -> 'a printer -> 'a t printer

Print the contents of a list.

OCaml

Innovation. Community. Security.