package containers

  1. Overview
  2. Docs
Legend:
Library
Module
Module type
Parameter
Class
Class type
include module type of struct include List end
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 iteri : (int -> 'a -> unit) -> 'a list -> unit

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

  • since 4.00.0
val mapi : (int -> 'a -> 'b) -> 'a list -> 'b list

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

  • since 4.00.0
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 mem : 'a -> 'a list -> bool

mem a l is true if and only if a is equal to an element of l.

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_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 assoc : 'a -> ('a * 'b) list -> 'b

assoc a l returns the value associated with key a in the list of pairs l. That is, assoc a [ ...; (a,b); ...] = b if (a,b) is the leftmost binding of a in list l. Raise Not_found if there is no value associated with a in the list l.

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

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

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

Same as List.assoc, but simply return true if a binding exists, and false if no bindings exist for the given key.

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_assoc : 'a -> ('a * 'b) list -> ('a * 'b) list

remove_assoc a l returns the list of pairs l without the first pair with key a, if any. Not tail-recursive.

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

Transform a pair of lists into a list of pairs: combine [a1; ...; an] [b1; ...; bn] is [(a1,b1); ...; (an,bn)]. Raise Invalid_argument if the two lists have different lengths. Not tail-recursive.

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

include module type of struct include CCList end

complements to list

type 'a t = 'a list
val empty : 'a t
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 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 append

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

Safe version of List.filter

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

Safe version of fold_right

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

Similar to Array.init

  • since 0.6
val compare : ('a -> 'a -> int) -> 'a t -> 'a t -> int
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 flatten : 'a t t -> 'a t

Safe flatten

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 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 pure : 'a -> 'a t
val (<*>) : ('a -> 'b) t -> 'a t -> 'b t
val (<$>) : ('a -> 'b) -> 'a t -> 'b t
val return : 'a -> 'a t
val (>>=) : 'a t -> ('a -> 'b t) -> 'b t
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 split : int -> 'a t -> 'a t * 'a t

split n l returns l1, l2 such that l1 @ l2 = l and length l1 = min (length l) n

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 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_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 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 : ('a -> 'b option) -> 'a list -> 'b option
  • deprecated

    in favor of find_map, for the name is too confusing

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 findi : (int -> 'a -> 'b option) -> 'a t -> 'b option
  • deprecated

    in favor of find_mapi, name is too confusing

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

find 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) -> x:'a -> 'a t -> 'a t

remove ~x l removes every instance of x from l. Tailrec.

  • parameter eq

    equality function

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

Map and remove elements at the same time

val sorted_merge : ?cmp:('a -> 'a -> int) -> 'a list -> 'a list -> 'a list

merges 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 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

module Idx = CCList.Idx

Set Operators

module Set = CCList.Set

Other Constructors

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

Same as 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 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 = CCList.Assoc

Zipper

module Zipper = CCList.Zipper

References on Lists

  • since 0.3.3
module Ref = CCList.Ref
module type MONAD = CCList.MONAD
module Traverse = CCList.Traverse

Conversions

type 'a sequence = ('a -> unit) -> unit
type 'a gen = unit -> 'a option
type 'a klist = unit -> [ `Nil | `Cons of 'a * 'a klist ]
type 'a printer = Buffer.t -> 'a -> unit
type 'a formatter = Format.formatter -> 'a -> unit
type 'a random_gen = Random.State.t -> 'a
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_seq : 'a t -> 'a sequence
val of_seq : 'a sequence -> 'a t
val to_gen : 'a t -> 'a gen
val of_gen : 'a gen -> 'a t
val to_klist : 'a t -> 'a klist
val of_klist : 'a klist -> 'a t

IO

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

Innovation. Community. Security.