From 1da2cdf96ea8cdd09f880cec78e493888f0f0e0f Mon Sep 17 00:00:00 2001 From: FardaleM Date: Fri, 12 Nov 2021 16:42:58 +0000 Subject: [PATCH] deploy: a5822f72733011e6cfd410a523d04fda06ee902a --- dev/containers/CCArray/Infix/index.html | 2 +- dev/containers/CCArray/index.html | 6 +++--- dev/containers/CCArrayLabels/Infix/index.html | 2 +- dev/containers/CCArrayLabels/index.html | 6 +++--- dev/containers/CCFun/index.html | 2 +- dev/containers/CCInt/index.html | 2 +- dev/containers/CCList/Infix/index.html | 4 ++-- dev/containers/CCList/index.html | 13 ++++++++++--- dev/containers/CCListLabels/Infix/index.html | 4 ++-- dev/containers/CCListLabels/index.html | 13 ++++++++++--- dev/containers/CCOpt/Infix/index.html | 2 +- dev/containers/CCOpt/index.html | 2 +- dev/containers/CCOption/Infix/index.html | 2 +- dev/containers/CCOption/index.html | 2 +- dev/containers/CCParse/Infix/index.html | 2 +- dev/containers/CCParse/index.html | 2 +- dev/containers/CCRandom/index.html | 2 +- dev/containers/CCResult/Infix/index.html | 2 +- dev/containers/CCResult/index.html | 2 +- dev/containers/CCVector/index.html | 2 +- 20 files changed, 44 insertions(+), 30 deletions(-) diff --git a/dev/containers/CCArray/Infix/index.html b/dev/containers/CCArray/Infix/index.html index 6f1dbabc..70089d96 100644 --- a/dev/containers/CCArray/Infix/index.html +++ b/dev/containers/CCArray/Infix/index.html @@ -1,2 +1,2 @@ -Infix (containers.CCArray.Infix)

Module CCArray.Infix

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

a >>= f is the infix version of flat_map.

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

a >>| f is the infix version of map.

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

a >|= f is the infix version of map.

  • since 0.8
val (--) : int -> int -> int t

x -- y creates an array containing integers in the range x .. y. Bounds included.

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

x --^ y creates an array containing integers in the range x .. y. Right bound excluded.

  • since 0.17

Let operators on OCaml >= 4.08.0, nothing otherwise

  • since 2.8
include CCShimsMkLet_.S with type 'a t_let := 'a array
val let+ : 'a array -> ('a -> 'b) -> 'b array
val and+ : 'a array -> 'b array -> ('a * 'b) array
val let* : 'a array -> ('a -> 'b array) -> 'b array
val and* : 'a array -> 'b array -> ('a * 'b) array
\ No newline at end of file +Infix (containers.CCArray.Infix)

Module CCArray.Infix

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

a >>= f is the infix version of flat_map.

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

a >>| f is the infix version of map.

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

a >|= f is the infix version of map.

  • since 0.8
val (--) : int -> int -> int t

x -- y creates an array containing integers in the range x .. y. Bounds included.

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

x --^ y creates an array containing integers in the range x .. y. Right bound excluded.

  • since 0.17

Let operators on OCaml >= 4.08.0, nothing otherwise

  • since 2.8
val let+ : 'a array -> ('a -> 'b) -> 'b array
val and+ : 'a array -> 'b array -> ('a * 'b) array
val let* : 'a array -> ('a -> 'b array) -> 'b array
val and* : 'a array -> 'b array -> ('a * 'b) array
\ No newline at end of file diff --git a/dev/containers/CCArray/index.html b/dev/containers/CCArray/index.html index 186bf252..268c9a1e 100644 --- a/dev/containers/CCArray/index.html +++ b/dev/containers/CCArray/index.html @@ -1,4 +1,4 @@ -CCArray (containers.CCArray)

Module CCArray

Array utils

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

Fast internal iterator.

  • since 2.8
type 'a gen = unit -> 'a option
type 'a equal = 'a -> 'a -> bool
type 'a ord = 'a -> 'a -> int
type 'a random_gen = Stdlib.Random.State.t -> 'a
type 'a printer = Stdlib.Format.formatter -> 'a -> unit

Arrays

include module type of CCShimsArray_
include module type of struct include Stdlib.Array end
type !'a t = 'a array
val length : 'a array -> int
val get : 'a array -> int -> 'a
val set : 'a array -> int -> 'a -> unit
val make : int -> 'a -> 'a array
val create : int -> 'a -> 'a array
val create_float : int -> float array
val make_float : int -> float array
val init : int -> (int -> 'a) -> 'a array
val make_matrix : int -> int -> 'a -> 'a array array
val create_matrix : int -> int -> 'a -> 'a array array
val append : 'a array -> 'a array -> 'a array
val concat : 'a array list -> 'a array
val sub : 'a array -> int -> int -> 'a array
val copy : 'a array -> 'a array
val fill : 'a array -> int -> int -> 'a -> unit
val blit : 'a array -> int -> 'a array -> int -> int -> unit
val to_list : 'a array -> 'a list
val of_list : 'a list -> 'a array
val iter : ('a -> unit) -> 'a array -> unit
val iteri : (int -> 'a -> unit) -> 'a array -> unit
val map : ('a -> 'b) -> 'a array -> 'b array
val mapi : (int -> 'a -> 'b) -> 'a array -> 'b array
val fold_left : ('a -> 'b -> 'a) -> 'a -> 'b array -> 'a
val fold_right : ('b -> 'a -> 'a) -> 'b array -> 'a -> 'a
val iter2 : ('a -> 'b -> unit) -> 'a array -> 'b array -> unit
val map2 : ('a -> 'b -> 'c) -> 'a array -> 'b array -> 'c array
val for_all : ('a -> bool) -> 'a array -> bool
val exists : ('a -> bool) -> 'a array -> bool
val memq : 'a -> 'a array -> bool
val sort : ('a -> 'a -> int) -> 'a array -> unit
val stable_sort : ('a -> 'a -> int) -> 'a array -> unit
val fast_sort : ('a -> 'a -> int) -> 'a array -> unit
val to_seqi : 'a array -> (int * 'a) Stdlib.Seq.t
val of_seq : 'a Stdlib.Seq.t -> 'a array
val unsafe_get : 'a array -> int -> 'a
val unsafe_set : 'a array -> int -> 'a -> unit
module Floatarray = Stdlib__array.Floatarray
val empty : 'a t

empty is the empty array, physically equal to [||].

val equal : 'a equal -> 'a t equal

equal eq a1 a2 is true if the lengths of a1 and a2 are the same and if their corresponding elements test equal, using eq.

val compare : 'a ord -> 'a t ord

compare cmp a1 a2 compares arrays a1 and a2 using the function comparison cmp.

val swap : 'a t -> int -> int -> unit

swap a i j swaps elements at indices i and j.

  • since 1.4
val get_safe : 'a t -> int -> 'a option

get_safe a i returns Some a.(i) if i is a valid index.

  • since 0.18
val fold : ('a -> 'b -> 'a) -> 'a -> 'b t -> 'a

fold f init a computes f (… (f (f init a.(0)) a.(1)) …) a.(n-1), where n is the length of the array a. Same as Array.fold_left

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

foldi f init a is just like fold, but it also passes in the index of each element as the second argument to the folded function f.

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

fold_while f init a folds left on array a until a stop condition via ('a, `Stop) is indicated by the accumulator.

  • since 0.8
val fold_map : ('acc -> 'a -> 'acc * 'b) -> 'acc -> 'a t -> 'acc * 'b t

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

  • since 1.2, but only
  • since 2.1 with labels
val scan_left : ('acc -> 'a -> 'acc) -> 'acc -> 'a t -> 'acc t

scan_left f init a returns the array [|init; f init x0; f (f init a.(0)) a.(1); …|] .

  • since 1.2, but only
  • since 2.1 with labels
val reverse_in_place : 'a t -> unit

reverse_in_place a reverses the array a in place.

val sorted : ('a -> 'a -> int) -> 'a t -> 'a array

sorted f a makes a copy of a and sorts it with f.

  • since 1.0
val sort_indices : ('a -> 'a -> int) -> 'a t -> int array

sort_indices f a returns a new array b, with the same length as a, such that b.(i) is the index at which the i-th element of sorted f a appears in a. a is not modified.

In other words, map (fun i -> a.(i)) (sort_indices f a) = sorted f a. sort_indices yields the inverse permutation of sort_ranking.

  • since 1.0
val sort_ranking : ('a -> 'a -> int) -> 'a t -> int array

sort_ranking f a returns a new array b, with the same length as a, such that b.(i) is the index at which the i-th element of a appears in sorted f a. a is not modified.

In other words, map (fun i -> (sorted f a).(i)) (sort_ranking f a) = a. sort_ranking yields the inverse permutation of sort_indices.

In the absence of duplicate elements in a, we also have lookup_exn a.(i) (sorted a) = (sorted_ranking a).(i).

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

mem ~eq x a return true if x is present in a. Linear time.

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

find_map f a returns Some y if there is an element x such that f x = Some y. Otherwise returns None.

  • since 1.3, but only
  • since 2.1 with labels
val find_map_i : (int -> 'a -> 'b option) -> 'a t -> 'b option

find_map_i f a is like find_map, but the index of the element is also passed to the predicate function f.

  • since 1.3, but only
  • since 2.1 with labels
val find_idx : ('a -> bool) -> 'a t -> (int * 'a) option

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

  • since 0.3.4
val lookup : cmp:'a ord -> 'a -> 'a t -> int option

lookup ~cmp key a lookups the index of some key key in a sorted array a. Undefined behavior if the array a is not sorted wrt ~cmp. Complexity: O(log (n)) (dichotomic search).

  • returns

    None if the key key is not present, or Some i (i the index of the key) otherwise.

val lookup_exn : cmp:'a ord -> 'a -> 'a t -> int

lookup_exn ~cmp key a is like lookup, but

  • raises Not_found

    if the key key is not present.

val bsearch : cmp:('a -> 'a -> int) -> -'a -> 'a t -> [ `All_lower | `All_bigger | `Just_after of int | `Empty | `At of int ]

bsearch ~cmp key a finds the index of the object key in the array a, provided a is sorted using cmp. If the array is not sorted, the result is not specified (may raise Invalid_argument).

Complexity: O(log n) where n is the length of the array a (dichotomic search).

  • returns
    • `At i if cmp a.(i) key = 0 (for some i).
    • `All_lower if all elements of a are lower than key.
    • `All_bigger if all elements of a are bigger than key.
    • `Just_after i if a.(i) < key < a.(i+1).
    • `Empty if the array a is empty.
  • raises Invalid_argument

    if the array is found to be unsorted w.r.t cmp.

  • since 0.13
val for_all2 : ('a -> 'b -> bool) -> 'a t -> 'b t -> bool

for_all2 f [|a1; …; an|] [|b1; …; bn|] is true if each pair of elements ai bi satisfies the predicate f. That is, it returns (f a1 b1) && (f a2 b2) && … && (f an bn).

  • raises Invalid_argument

    if arrays have distinct lengths. Allow different types.

  • since 0.20
val exists2 : ('a -> 'b -> bool) -> 'a t -> 'b t -> bool

exists2 f [|a1; …; an|] [|b1; …; bn|] is true if any pair of elements ai bi satisfies the predicate f. That is, it returns (f a1 b1) || (f a2 b2) || … || (f an bn).

  • raises Invalid_argument

    if arrays have distinct lengths. Allow different types.

  • since 0.20
val fold2 : ('acc -> 'a -> 'b -> 'acc) -> 'acc -> 'a t -> 'b t -> 'acc

fold2 f init a b fold on two arrays a and b stepwise. It computes f (… (f init a1 b1) …) an bn.

  • raises Invalid_argument

    if a and b have distinct lengths.

  • since 0.20
val shuffle : 'a t -> unit

shuffle a randomly shuffles the array a, in place.

val shuffle_with : Stdlib.Random.State.t -> 'a t -> unit

shuffle_with rs a randomly shuffles the array a (like shuffle) but a specialized random state rs is used to control the random numbers being produced during shuffling (for reproducibility).

val random_choose : 'a t -> 'a random_gen

random_choose a rs randomly chooses an element of a.

  • raises Not_found

    if the array/slice is empty.

val to_string : ?sep:string -> ('a -> string) -> 'a array -> string

to_string ~sep item_to_string a print a to a string using sep as a separator between elements of a.

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

to_iter a returns an iter of the elements of an array a. The input array a is shared with the sequence and modification of it will result in modification of the iterator.

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

to_seq a returns a Seq.t of the elements of an array a. The input array a is shared with the sequence and modification of it will result in modification of the sequence. Renamed from to_std_seq since 3.0.

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

to_gen a returns a gen of the elements of an array a.

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 pp_item ppf a formats the array a on ppf. Each element is formatted with pp_item, pp_start is called at the beginning, pp_stop is called at the end, pp_sep is called between each elements. By defaults pp_start and pp_stop does nothing and pp_sep defaults to (fun out -> Format.fprintf out ",@ ").

val pp_i : ?pp_start:unit printer -> ?pp_stop:unit printer -> ?pp_sep:unit printer -> (int -> 'a printer) -> 'a t printer

pp_i ~pp_start ~pp_stop ~pp_sep pp_item ppf a prints the array a on ppf. The printing function pp_item is giving both index and element. pp_start is called at the beginning, pp_stop is called at the end, pp_sep is called between each elements. By defaults pp_start and pp_stop does nothing and pp_sep defaults to (fun out -> Format.fprintf out ",@ ").

val rev : 'a t -> 'a t

rev a copies the array a and reverses it in place.

  • since 0.20
val filter : ('a -> bool) -> 'a t -> 'a t

filter f a filters elements out of the array a. Only the elements satisfying the given predicate f will be kept.

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

filter_map f [|a1; …; an|] calls (f a1) … (f an) and returns an array b consisting of all elements bi such as f ai = Some bi. When f returns None, the corresponding element of a is discarded.

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

monoid_product f a b passes all combinaisons of tuples from the two arrays a and b to the function f.

  • since 2.8
val flat_map : ('a -> 'b t) -> 'a t -> 'b array

flat_map f a transforms each element of a into an array, then flattens.

val except_idx : 'a t -> int -> 'a list

except_idx a i removes the element of a at given index i, and returns the list of the other elements.

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

Generic Functions

module type MONO_ARRAY = sig ... end
val sort_generic : (module MONO_ARRAY with type elt = 'elt and type t = 'arr) -> cmp:('elt -> 'elt -> int) --> 'arr -> unit

sort_generic (module M) ~cmp a sorts the array a, without allocating (eats stack space though). Performance might be lower than Array.sort.

  • since 0.14

Infix Operators

It is convenient to open CCArray.Infix to access the infix operators without cluttering the scope too much.

module Infix : sig ... end
include module type of Infix
val (>>=) : 'a t -> ('a -> 'b t) -> 'b t

a >>= f is the infix version of flat_map.

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

a >>| f is the infix version of map.

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

a >|= f is the infix version of map.

  • since 0.8
val (--) : int -> int -> int t

x -- y creates an array containing integers in the range x .. y. Bounds included.

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

x --^ y creates an array containing integers in the range x .. y. Right bound excluded.

  • since 0.17

Let operators on OCaml >= 4.08.0, nothing otherwise

  • since 2.8
include CCShimsMkLet_.S with type 'a t_let := 'a array
val let+ : 'a array -> ('a -> 'b) -> 'b array
val and+ : 'a array -> 'b array -> ('a * 'b) array
val let* : 'a array -> ('a -> 'b array) -> 'b array
val and* : 'a array -> 'b array -> ('a * 'b) array
\ No newline at end of file +CCArray (containers.CCArray)

Module CCArray

Array utils

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

Fast internal iterator.

  • since 2.8
type 'a gen = unit -> 'a option
type 'a equal = 'a -> 'a -> bool
type 'a ord = 'a -> 'a -> int
type 'a random_gen = Stdlib.Random.State.t -> 'a
type 'a printer = Stdlib.Format.formatter -> 'a -> unit

Arrays

include module type of struct include Stdlib.Array end
type !'a t = 'a array
val length : 'a array -> int
val get : 'a array -> int -> 'a
val set : 'a array -> int -> 'a -> unit
val make : int -> 'a -> 'a array
val create : int -> 'a -> 'a array
val create_float : int -> float array
val make_float : int -> float array
val init : int -> (int -> 'a) -> 'a array
val make_matrix : int -> int -> 'a -> 'a array array
val create_matrix : int -> int -> 'a -> 'a array array
val append : 'a array -> 'a array -> 'a array
val concat : 'a array list -> 'a array
val sub : 'a array -> int -> int -> 'a array
val copy : 'a array -> 'a array
val fill : 'a array -> int -> int -> 'a -> unit
val blit : 'a array -> int -> 'a array -> int -> int -> unit
val to_list : 'a array -> 'a list
val of_list : 'a list -> 'a array
val iter : ('a -> unit) -> 'a array -> unit
val iteri : (int -> 'a -> unit) -> 'a array -> unit
val map : ('a -> 'b) -> 'a array -> 'b array
val mapi : (int -> 'a -> 'b) -> 'a array -> 'b array
val fold_left : ('a -> 'b -> 'a) -> 'a -> 'b array -> 'a
val fold_right : ('b -> 'a -> 'a) -> 'b array -> 'a -> 'a
val iter2 : ('a -> 'b -> unit) -> 'a array -> 'b array -> unit
val map2 : ('a -> 'b -> 'c) -> 'a array -> 'b array -> 'c array
val for_all : ('a -> bool) -> 'a array -> bool
val exists : ('a -> bool) -> 'a array -> bool
val memq : 'a -> 'a array -> bool
val sort : ('a -> 'a -> int) -> 'a array -> unit
val stable_sort : ('a -> 'a -> int) -> 'a array -> unit
val fast_sort : ('a -> 'a -> int) -> 'a array -> unit
val to_seqi : 'a array -> (int * 'a) Stdlib.Seq.t
val of_seq : 'a Stdlib.Seq.t -> 'a array
val unsafe_get : 'a array -> int -> 'a
val unsafe_set : 'a array -> int -> 'a -> unit
module Floatarray = Stdlib__array.Floatarray
val empty : 'a t

empty is the empty array, physically equal to [||].

val equal : 'a equal -> 'a t equal

equal eq a1 a2 is true if the lengths of a1 and a2 are the same and if their corresponding elements test equal, using eq.

val compare : 'a ord -> 'a t ord

compare cmp a1 a2 compares arrays a1 and a2 using the function comparison cmp.

val swap : 'a t -> int -> int -> unit

swap a i j swaps elements at indices i and j.

  • since 1.4
val get_safe : 'a t -> int -> 'a option

get_safe a i returns Some a.(i) if i is a valid index.

  • since 0.18
val fold : ('a -> 'b -> 'a) -> 'a -> 'b t -> 'a

fold f init a computes f (… (f (f init a.(0)) a.(1)) …) a.(n-1), where n is the length of the array a. Same as Array.fold_left

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

foldi f init a is just like fold, but it also passes in the index of each element as the second argument to the folded function f.

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

fold_while f init a folds left on array a until a stop condition via ('a, `Stop) is indicated by the accumulator.

  • since 0.8
val fold_map : ('acc -> 'a -> 'acc * 'b) -> 'acc -> 'a t -> 'acc * 'b t

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

  • since 1.2, but only
  • since 2.1 with labels
val scan_left : ('acc -> 'a -> 'acc) -> 'acc -> 'a t -> 'acc t

scan_left f init a returns the array [|init; f init x0; f (f init a.(0)) a.(1); …|] .

  • since 1.2, but only
  • since 2.1 with labels
val reverse_in_place : 'a t -> unit

reverse_in_place a reverses the array a in place.

val sorted : ('a -> 'a -> int) -> 'a t -> 'a array

sorted f a makes a copy of a and sorts it with f.

  • since 1.0
val sort_indices : ('a -> 'a -> int) -> 'a t -> int array

sort_indices f a returns a new array b, with the same length as a, such that b.(i) is the index at which the i-th element of sorted f a appears in a. a is not modified.

In other words, map (fun i -> a.(i)) (sort_indices f a) = sorted f a. sort_indices yields the inverse permutation of sort_ranking.

  • since 1.0
val sort_ranking : ('a -> 'a -> int) -> 'a t -> int array

sort_ranking f a returns a new array b, with the same length as a, such that b.(i) is the index at which the i-th element of a appears in sorted f a. a is not modified.

In other words, map (fun i -> (sorted f a).(i)) (sort_ranking f a) = a. sort_ranking yields the inverse permutation of sort_indices.

In the absence of duplicate elements in a, we also have lookup_exn a.(i) (sorted a) = (sorted_ranking a).(i).

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

mem ~eq x a return true if x is present in a. Linear time.

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

find_map f a returns Some y if there is an element x such that f x = Some y. Otherwise returns None.

  • since 1.3, but only
  • since 2.1 with labels
val find_map_i : (int -> 'a -> 'b option) -> 'a t -> 'b option

find_map_i f a is like find_map, but the index of the element is also passed to the predicate function f.

  • since 1.3, but only
  • since 2.1 with labels
val find_idx : ('a -> bool) -> 'a t -> (int * 'a) option

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

  • since 0.3.4
val lookup : cmp:'a ord -> 'a -> 'a t -> int option

lookup ~cmp key a lookups the index of some key key in a sorted array a. Undefined behavior if the array a is not sorted wrt ~cmp. Complexity: O(log (n)) (dichotomic search).

  • returns

    None if the key key is not present, or Some i (i the index of the key) otherwise.

val lookup_exn : cmp:'a ord -> 'a -> 'a t -> int

lookup_exn ~cmp key a is like lookup, but

  • raises Not_found

    if the key key is not present.

val bsearch : cmp:('a -> 'a -> int) -> +'a -> 'a t -> [ `All_lower | `All_bigger | `Just_after of int | `Empty | `At of int ]

bsearch ~cmp key a finds the index of the object key in the array a, provided a is sorted using cmp. If the array is not sorted, the result is not specified (may raise Invalid_argument).

Complexity: O(log n) where n is the length of the array a (dichotomic search).

  • returns
    • `At i if cmp a.(i) key = 0 (for some i).
    • `All_lower if all elements of a are lower than key.
    • `All_bigger if all elements of a are bigger than key.
    • `Just_after i if a.(i) < key < a.(i+1).
    • `Empty if the array a is empty.
  • raises Invalid_argument

    if the array is found to be unsorted w.r.t cmp.

  • since 0.13
val for_all2 : ('a -> 'b -> bool) -> 'a t -> 'b t -> bool

for_all2 f [|a1; …; an|] [|b1; …; bn|] is true if each pair of elements ai bi satisfies the predicate f. That is, it returns (f a1 b1) && (f a2 b2) && … && (f an bn).

  • raises Invalid_argument

    if arrays have distinct lengths. Allow different types.

  • since 0.20
val exists2 : ('a -> 'b -> bool) -> 'a t -> 'b t -> bool

exists2 f [|a1; …; an|] [|b1; …; bn|] is true if any pair of elements ai bi satisfies the predicate f. That is, it returns (f a1 b1) || (f a2 b2) || … || (f an bn).

  • raises Invalid_argument

    if arrays have distinct lengths. Allow different types.

  • since 0.20
val fold2 : ('acc -> 'a -> 'b -> 'acc) -> 'acc -> 'a t -> 'b t -> 'acc

fold2 f init a b fold on two arrays a and b stepwise. It computes f (… (f init a1 b1) …) an bn.

  • raises Invalid_argument

    if a and b have distinct lengths.

  • since 0.20
val shuffle : 'a t -> unit

shuffle a randomly shuffles the array a, in place.

val shuffle_with : Stdlib.Random.State.t -> 'a t -> unit

shuffle_with rs a randomly shuffles the array a (like shuffle) but a specialized random state rs is used to control the random numbers being produced during shuffling (for reproducibility).

val random_choose : 'a t -> 'a random_gen

random_choose a rs randomly chooses an element of a.

  • raises Not_found

    if the array/slice is empty.

of_list_map f l applies the function f to all the elements of the list l and builds an array with the results returned by f. The result is equivalent to map f (Array.of_list l).

val to_string : ?sep:string -> ('a -> string) -> 'a array -> string

to_string ~sep item_to_string a print a to a string using sep as a separator between elements of a.

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

to_iter a returns an iter of the elements of an array a. The input array a is shared with the sequence and modification of it will result in modification of the iterator.

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

to_seq a returns a Seq.t of the elements of an array a. The input array a is shared with the sequence and modification of it will result in modification of the sequence. Renamed from to_std_seq since 3.0.

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

to_gen a returns a gen of the elements of an array a.

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 pp_item ppf a formats the array a on ppf. Each element is formatted with pp_item, pp_start is called at the beginning, pp_stop is called at the end, pp_sep is called between each elements. By defaults pp_start and pp_stop does nothing and pp_sep defaults to (fun out -> Format.fprintf out ",@ ").

val pp_i : ?pp_start:unit printer -> ?pp_stop:unit printer -> ?pp_sep:unit printer -> (int -> 'a printer) -> 'a t printer

pp_i ~pp_start ~pp_stop ~pp_sep pp_item ppf a prints the array a on ppf. The printing function pp_item is giving both index and element. pp_start is called at the beginning, pp_stop is called at the end, pp_sep is called between each elements. By defaults pp_start and pp_stop does nothing and pp_sep defaults to (fun out -> Format.fprintf out ",@ ").

val rev : 'a t -> 'a t

rev a copies the array a and reverses it in place.

  • since 0.20
val filter : ('a -> bool) -> 'a t -> 'a t

filter f a filters elements out of the array a. Only the elements satisfying the given predicate f will be kept.

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

filter_map f [|a1; …; an|] calls (f a1) … (f an) and returns an array b consisting of all elements bi such as f ai = Some bi. When f returns None, the corresponding element of a is discarded.

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

monoid_product f a b passes all combinaisons of tuples from the two arrays a and b to the function f.

  • since 2.8
val flat_map : ('a -> 'b t) -> 'a t -> 'b array

flat_map f a transforms each element of a into an array, then flattens.

val except_idx : 'a t -> int -> 'a list

except_idx a i removes the element of a at given index i, and returns the list of the other elements.

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

Generic Functions

module type MONO_ARRAY = sig ... end
val sort_generic : (module MONO_ARRAY with type elt = 'elt and type t = 'arr) -> cmp:('elt -> 'elt -> int) +-> 'arr -> unit

sort_generic (module M) ~cmp a sorts the array a, without allocating (eats stack space though). Performance might be lower than Array.sort.

  • since 0.14

Infix Operators

It is convenient to open CCArray.Infix to access the infix operators without cluttering the scope too much.

module Infix : sig ... end
include module type of Infix
val (>>=) : 'a t -> ('a -> 'b t) -> 'b t

a >>= f is the infix version of flat_map.

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

a >>| f is the infix version of map.

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

a >|= f is the infix version of map.

  • since 0.8
val (--) : int -> int -> int t

x -- y creates an array containing integers in the range x .. y. Bounds included.

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

x --^ y creates an array containing integers in the range x .. y. Right bound excluded.

  • since 0.17

Let operators on OCaml >= 4.08.0, nothing otherwise

  • since 2.8
val let+ : 'a array -> ('a -> 'b) -> 'b array
val and+ : 'a array -> 'b array -> ('a * 'b) array
val let* : 'a array -> ('a -> 'b array) -> 'b array
val and* : 'a array -> 'b array -> ('a * 'b) array
\ No newline at end of file diff --git a/dev/containers/CCArrayLabels/Infix/index.html b/dev/containers/CCArrayLabels/Infix/index.html index 057c3ee3..51faa0b0 100644 --- a/dev/containers/CCArrayLabels/Infix/index.html +++ b/dev/containers/CCArrayLabels/Infix/index.html @@ -1,2 +1,2 @@ -Infix (containers.CCArrayLabels.Infix)

Module CCArrayLabels.Infix

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

a >>= f is the infix version of flat_map.

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

a >>| f is the infix version of map.

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

a >|= f is the infix version of map.

  • since 0.8
val (--) : int -> int -> int t

x -- y creates an array containing integers in the range x .. y. Bounds included.

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

x --^ y creates an array containing integers in the range x .. y. Right bound excluded.

  • since 0.17

Let operators on OCaml >= 4.08.0, nothing otherwise

  • since 2.8
include CCShimsMkLet_.S with type 'a t_let := 'a array
val let+ : 'a array -> ('a -> 'b) -> 'b array
val and+ : 'a array -> 'b array -> ('a * 'b) array
val let* : 'a array -> ('a -> 'b array) -> 'b array
val and* : 'a array -> 'b array -> ('a * 'b) array
\ No newline at end of file +Infix (containers.CCArrayLabels.Infix)

Module CCArrayLabels.Infix

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

a >>= f is the infix version of flat_map.

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

a >>| f is the infix version of map.

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

a >|= f is the infix version of map.

  • since 0.8
val (--) : int -> int -> int t

x -- y creates an array containing integers in the range x .. y. Bounds included.

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

x --^ y creates an array containing integers in the range x .. y. Right bound excluded.

  • since 0.17

Let operators on OCaml >= 4.08.0, nothing otherwise

  • since 2.8
val let+ : 'a array -> ('a -> 'b) -> 'b array
val and+ : 'a array -> 'b array -> ('a * 'b) array
val let* : 'a array -> ('a -> 'b array) -> 'b array
val and* : 'a array -> 'b array -> ('a * 'b) array
\ No newline at end of file diff --git a/dev/containers/CCArrayLabels/index.html b/dev/containers/CCArrayLabels/index.html index 8a0284e2..db1f2169 100644 --- a/dev/containers/CCArrayLabels/index.html +++ b/dev/containers/CCArrayLabels/index.html @@ -1,5 +1,5 @@ -CCArrayLabels (containers.CCArrayLabels)

Module CCArrayLabels

Array utils (Labeled version of CCArray)

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

Fast internal iterator.

  • since 2.8
type 'a gen = unit -> 'a option
type 'a equal = 'a -> 'a -> bool
type 'a ord = 'a -> 'a -> int
type 'a random_gen = Stdlib.Random.State.t -> 'a
type 'a printer = Stdlib.Format.formatter -> 'a -> unit

Arrays

include module type of CCShimsArrayLabels_
include module type of Stdlib.ArrayLabels with module Floatarray = Stdlib.Array.Floatarray
type !'a t = 'a array
val length : 'a array -> int
val get : 'a array -> int -> 'a
val set : 'a array -> int -> 'a -> unit
val make : int -> 'a -> 'a array
val create : int -> 'a -> 'a array
val create_float : int -> float array
val make_float : int -> float array
val init : int -> f:(int -> 'a) -> 'a array
val make_matrix : dimx:int -> dimy:int -> 'a -> 'a array array
val create_matrix : dimx:int -> dimy:int -> 'a -> 'a array array
val append : 'a array -> 'a array -> 'a array
val concat : 'a array list -> 'a array
val sub : 'a array -> pos:int -> len:int -> 'a array
val copy : 'a array -> 'a array
val fill : 'a array -> pos:int -> len:int -> 'a -> unit
val blit : src:'a array -> src_pos:int -> dst:'a array -> dst_pos:int -> len:int -> -unit
val to_list : 'a array -> 'a list
val of_list : 'a list -> 'a array
val iter : f:('a -> unit) -> 'a array -> unit
val iteri : f:(int -> 'a -> unit) -> 'a array -> unit
val map : f:('a -> 'b) -> 'a array -> 'b array
val mapi : f:(int -> 'a -> 'b) -> 'a array -> 'b array
val fold_left : f:('a -> 'b -> 'a) -> init:'a -> 'b array -> 'a
val fold_right : f:('b -> 'a -> 'a) -> 'b array -> init:'a -> 'a
val for_all : f:('a -> bool) -> 'a array -> bool
val exists : f:('a -> bool) -> 'a array -> bool
val memq : 'a -> set:'a array -> bool
val sort : cmp:('a -> 'a -> int) -> 'a array -> unit
val stable_sort : cmp:('a -> 'a -> int) -> 'a array -> unit
val fast_sort : cmp:('a -> 'a -> int) -> 'a array -> unit
val to_seqi : 'a array -> (int * 'a) Stdlib.Seq.t
val of_seq : 'a Stdlib.Seq.t -> 'a array
val unsafe_get : 'a array -> int -> 'a
val unsafe_set : 'a array -> int -> 'a -> unit
module Floatarray : sig ... end
val empty : 'a t

empty is the empty array, physically equal to ||.

val equal : 'a equal -> 'a t equal

equal eq a1 a2 is true if the lengths of a1 and a2 are the same and if their corresponding elements test equal, using eq.

val compare : 'a ord -> 'a t ord

compare cmp a1 a2 compares arrays a1 and a2 using the function comparison cmp.

val swap : 'a t -> int -> int -> unit

swap a i j swaps elements at indices i and j.

  • since 1.4
val get_safe : 'a t -> int -> 'a option

get_safe a i returns Some a.(i) if i is a valid index.

  • since 0.18
val fold : f:('a -> 'b -> 'a) -> init:'a -> 'b t -> 'a

fold ~f ~init a computes f (… (f (f init a.(0)) a.(1)) …) a.(n-1), where n is the length of the array a. Same as ArrayLabels.fold_left

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

foldi ~f ~init a is just like fold, but it also passes in the index of each element as the second argument to the folded function f.

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

fold_while ~f ~init a folds left on array a until a stop condition via ('a, `Stop) is indicated by the accumulator.

  • since 0.8
val fold_map : f:('acc -> 'a -> 'acc * 'b) -> init:'acc -> 'a t -> 'acc * 'b t

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

  • since 1.2, but only
  • since 2.1 with labels
val scan_left : f:('acc -> 'a -> 'acc) -> init:'acc -> 'a t -> 'acc t

scan_left ~f ~init a returns the array [|init; f init x0; f (f init a.(0)) a.(1); …|] .

  • since 1.2, but only
  • since 2.1 with labels
val reverse_in_place : 'a t -> unit

reverse_in_place a reverses the array a in place.

val sorted : f:('a -> 'a -> int) -> 'a t -> 'a array

sorted ~f a makes a copy of a and sorts it with f.

  • since 1.0
val sort_indices : f:('a -> 'a -> int) -> 'a t -> int array

sort_indices ~f a returns a new array b, with the same length as a, such that b.(i) is the index at which the i-th element of sorted f a appears in a. a is not modified.

In other words, map (fun i -> a.(i)) (sort_indices f a) = sorted f a. sort_indices yields the inverse permutation of sort_ranking.

  • since 1.0
val sort_ranking : f:('a -> 'a -> int) -> 'a t -> int array

sort_ranking ~f a returns a new array b, with the same length as a, such that b.(i) is the index at which the i-th element of a appears in sorted f a. a is not modified.

In other words, map (fun i -> (sorted f a).(i)) (sort_ranking f a) = a. sort_ranking yields the inverse permutation of sort_indices.

In the absence of duplicate elements in a, we also have lookup_exn a.(i) (sorted a) = (sorted_ranking a).(i).

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

mem ~eq x a return true if x is present in a. Linear time.

  • since 3.0
val find_map : f:('a -> 'b option) -> 'a t -> 'b option

find_map ~f a returns Some y if there is an element x such that f x = Some y. Otherwise returns None.

  • since 1.3, but only
  • since 2.1 with labels
val find_map_i : f:(int -> 'a -> 'b option) -> 'a t -> 'b option

find_map_i ~f a is like find_map, but the index of the element is also passed to the predicate function f.

  • since 1.3, but only
  • since 2.1 with labels
val find_idx : f:('a -> bool) -> 'a t -> (int * 'a) option

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

  • since 0.3.4
val lookup : cmp:'a ord -> key:'a -> 'a t -> int option

lookup ~cmp ~key a lookups the index of some key key in a sorted array a. Undefined behavior if the array a is not sorted wrt cmp. Complexity: O(log (n)) (dichotomic search).

  • returns

    None if the key key is not present, or Some i (i the index of the key) otherwise.

val lookup_exn : cmp:'a ord -> key:'a -> 'a t -> int

lookup_exn ~cmp ~key a is like lookup, but

  • raises Not_found

    if the key key is not present.

val bsearch : cmp:('a -> 'a -> int) -> key:'a -> +CCArrayLabels (containers.CCArrayLabels)

Module CCArrayLabels

Array utils (Labeled version of CCArray)

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

Fast internal iterator.

  • since 2.8
type 'a gen = unit -> 'a option
type 'a equal = 'a -> 'a -> bool
type 'a ord = 'a -> 'a -> int
type 'a random_gen = Stdlib.Random.State.t -> 'a
type 'a printer = Stdlib.Format.formatter -> 'a -> unit

Arrays

include module type of Stdlib.ArrayLabels with module Floatarray = Stdlib.Array.Floatarray
type !'a t = 'a array
val length : 'a array -> int
val get : 'a array -> int -> 'a
val set : 'a array -> int -> 'a -> unit
val make : int -> 'a -> 'a array
val create : int -> 'a -> 'a array
val create_float : int -> float array
val make_float : int -> float array
val init : int -> f:(int -> 'a) -> 'a array
val make_matrix : dimx:int -> dimy:int -> 'a -> 'a array array
val create_matrix : dimx:int -> dimy:int -> 'a -> 'a array array
val append : 'a array -> 'a array -> 'a array
val concat : 'a array list -> 'a array
val sub : 'a array -> pos:int -> len:int -> 'a array
val copy : 'a array -> 'a array
val fill : 'a array -> pos:int -> len:int -> 'a -> unit
val blit : src:'a array -> src_pos:int -> dst:'a array -> dst_pos:int -> len:int -> +unit
val to_list : 'a array -> 'a list
val of_list : 'a list -> 'a array
val iter : f:('a -> unit) -> 'a array -> unit
val iteri : f:(int -> 'a -> unit) -> 'a array -> unit
val map : f:('a -> 'b) -> 'a array -> 'b array
val mapi : f:(int -> 'a -> 'b) -> 'a array -> 'b array
val fold_left : f:('a -> 'b -> 'a) -> init:'a -> 'b array -> 'a
val fold_right : f:('b -> 'a -> 'a) -> 'b array -> init:'a -> 'a
val for_all : f:('a -> bool) -> 'a array -> bool
val exists : f:('a -> bool) -> 'a array -> bool
val memq : 'a -> set:'a array -> bool
val sort : cmp:('a -> 'a -> int) -> 'a array -> unit
val stable_sort : cmp:('a -> 'a -> int) -> 'a array -> unit
val fast_sort : cmp:('a -> 'a -> int) -> 'a array -> unit
val to_seqi : 'a array -> (int * 'a) Stdlib.Seq.t
val of_seq : 'a Stdlib.Seq.t -> 'a array
val unsafe_get : 'a array -> int -> 'a
val unsafe_set : 'a array -> int -> 'a -> unit
module Floatarray : sig ... end
val empty : 'a t

empty is the empty array, physically equal to ||.

val equal : 'a equal -> 'a t equal

equal eq a1 a2 is true if the lengths of a1 and a2 are the same and if their corresponding elements test equal, using eq.

val compare : 'a ord -> 'a t ord

compare cmp a1 a2 compares arrays a1 and a2 using the function comparison cmp.

val swap : 'a t -> int -> int -> unit

swap a i j swaps elements at indices i and j.

  • since 1.4
val get_safe : 'a t -> int -> 'a option

get_safe a i returns Some a.(i) if i is a valid index.

  • since 0.18
val fold : f:('a -> 'b -> 'a) -> init:'a -> 'b t -> 'a

fold ~f ~init a computes f (… (f (f init a.(0)) a.(1)) …) a.(n-1), where n is the length of the array a. Same as ArrayLabels.fold_left

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

foldi ~f ~init a is just like fold, but it also passes in the index of each element as the second argument to the folded function f.

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

fold_while ~f ~init a folds left on array a until a stop condition via ('a, `Stop) is indicated by the accumulator.

  • since 0.8
val fold_map : f:('acc -> 'a -> 'acc * 'b) -> init:'acc -> 'a t -> 'acc * 'b t

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

  • since 1.2, but only
  • since 2.1 with labels
val scan_left : f:('acc -> 'a -> 'acc) -> init:'acc -> 'a t -> 'acc t

scan_left ~f ~init a returns the array [|init; f init x0; f (f init a.(0)) a.(1); …|] .

  • since 1.2, but only
  • since 2.1 with labels
val reverse_in_place : 'a t -> unit

reverse_in_place a reverses the array a in place.

val sorted : f:('a -> 'a -> int) -> 'a t -> 'a array

sorted ~f a makes a copy of a and sorts it with f.

  • since 1.0
val sort_indices : f:('a -> 'a -> int) -> 'a t -> int array

sort_indices ~f a returns a new array b, with the same length as a, such that b.(i) is the index at which the i-th element of sorted f a appears in a. a is not modified.

In other words, map (fun i -> a.(i)) (sort_indices f a) = sorted f a. sort_indices yields the inverse permutation of sort_ranking.

  • since 1.0
val sort_ranking : f:('a -> 'a -> int) -> 'a t -> int array

sort_ranking ~f a returns a new array b, with the same length as a, such that b.(i) is the index at which the i-th element of a appears in sorted f a. a is not modified.

In other words, map (fun i -> (sorted f a).(i)) (sort_ranking f a) = a. sort_ranking yields the inverse permutation of sort_indices.

In the absence of duplicate elements in a, we also have lookup_exn a.(i) (sorted a) = (sorted_ranking a).(i).

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

mem ~eq x a return true if x is present in a. Linear time.

  • since 3.0
val find_map : f:('a -> 'b option) -> 'a t -> 'b option

find_map ~f a returns Some y if there is an element x such that f x = Some y. Otherwise returns None.

  • since 1.3, but only
  • since 2.1 with labels
val find_map_i : f:(int -> 'a -> 'b option) -> 'a t -> 'b option

find_map_i ~f a is like find_map, but the index of the element is also passed to the predicate function f.

  • since 1.3, but only
  • since 2.1 with labels
val find_idx : f:('a -> bool) -> 'a t -> (int * 'a) option

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

  • since 0.3.4
val lookup : cmp:'a ord -> key:'a -> 'a t -> int option

lookup ~cmp ~key a lookups the index of some key key in a sorted array a. Undefined behavior if the array a is not sorted wrt cmp. Complexity: O(log (n)) (dichotomic search).

  • returns

    None if the key key is not present, or Some i (i the index of the key) otherwise.

val lookup_exn : cmp:'a ord -> key:'a -> 'a t -> int

lookup_exn ~cmp ~key a is like lookup, but

  • raises Not_found

    if the key key is not present.

val bsearch : cmp:('a -> 'a -> int) -> key:'a -> 'a t -> [ `All_lower | `All_bigger | `Just_after of int | `Empty | `At of int ]

bsearch ~cmp ~key a finds the index of the object key in the array a, provided a is sorted using cmp. If the array is not sorted, the result is not specified (may raise Invalid_argument).

Complexity: O(log n) where n is the length of the array a (dichotomic search).

  • returns
    • `At i if cmp a.(i) key = 0 (for some i).
    • `All_lower if all elements of a are lower than key.
    • `All_bigger if all elements of a are bigger than key.
    • `Just_after i if a.(i) < key < a.(i+1).
    • `Empty if the array a is empty.
  • raises Invalid_argument

    if the array is found to be unsorted w.r.t cmp.

  • since 0.13
val for_all2 : f:('a -> 'b -> bool) -> 'a t -> 'b t -> bool

for_all2 ~f [|a1; …; an|] [|b1; …; bn|] is true if each pair of elements ai bi satisfies the predicate f. That is, it returns (f a1 b1) && (f a2 b2) && … && (f an bn).

  • raises Invalid_argument

    if arrays have distinct lengths. Allow different types.

  • since 0.20
val exists2 : f:('a -> 'b -> bool) -> 'a t -> 'b t -> bool

exists2 ~f [|a1; …; an|] [|b1; …; bn|] is true if any pair of elements ai bi satisfies the predicate f. That is, it returns (f a1 b1) || (f a2 b2) || … || (f an bn).

  • raises Invalid_argument

    if arrays have distinct lengths. Allow different types.

  • since 0.20
val fold2 : f:('acc -> 'a -> 'b -> 'acc) -> init:'acc -> 'a t -> 'b t -> 'acc

fold2 ~f ~init a b fold on two arrays a and b stepwise. It computes f (… (f init a1 b1) …) an bn.

  • raises Invalid_argument

    if a and b have distinct lengths.

  • since 0.20
val iter2 : f:('a -> 'b -> unit) -> 'a t -> 'b t -> unit

iter2 ~f a b iterates on the two arrays a and b stepwise. It is equivalent to f a0 b0; …; f a.(length a - 1) b.(length b - 1); ().

  • raises Invalid_argument

    if a and b have distinct lengths.

  • since 0.20
val shuffle : 'a t -> unit

shuffle a randomly shuffles the array a, in place.

val shuffle_with : Stdlib.Random.State.t -> 'a t -> unit

shuffle_with rs a randomly shuffles the array a (like shuffle) but a specialized random state rs is used to control the random numbers being produced during shuffling (for reproducibility).

val random_choose : 'a t -> 'a random_gen

random_choose a rs randomly chooses an element of a.

  • raises Not_found

    if the array/slice is empty.

val to_string : ?sep:string -> ('a -> string) -> 'a array -> string

to_string ~sep item_to_string a print a to a string using sep as a separator between elements of a.

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

to_iter a returns an iter of the elements of an array a. The input array a is shared with the sequence and modification of it will result in modification of the iterator.

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

to_seq a returns a Seq.t of the elements of an array a. The input array a is shared with the sequence and modification of it will result in modification of the sequence. Renamed from to_std_seq since 3.0.

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

to_gen a returns a gen of the elements of an array a.

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 pp_item ppf a formats the array a on ppf. Each element is formatted with pp_item, pp_start is called at the beginning, pp_stop is called at the end, pp_sep is called between each elements. By defaults pp_start and pp_stop does nothing and pp_sep defaults to (fun out -> Format.fprintf out ",@ ").

val pp_i : ?pp_start:unit printer -> ?pp_stop:unit printer -> ?pp_sep:unit printer -> (int -> 'a printer) -> 'a t printer

pp_i ~pp_start ~pp_stop ~pp_sep pp_item ppf a prints the array a on ppf. The printing function pp_item is giving both index and element. pp_start is called at the beginning, pp_stop is called at the end, pp_sep is called between each elements. By defaults pp_start and pp_stop does nothing and pp_sep defaults to (fun out -> Format.fprintf out ",@ ").

val map2 : f:('a -> 'b -> 'c) -> 'a t -> 'b t -> 'c t

map2 ~f a b applies function f to all elements of a and b, and builds an array with the results returned by f: [| f a.(0) b.(0); …; f a.(length a - 1) b.(length b - 1)|].

  • raises Invalid_argument

    if a and b have distinct lengths.

  • since 0.20
val rev : 'a t -> 'a t

rev a copies the array a and reverses it in place.

  • since 0.20
val filter : f:('a -> bool) -> 'a t -> 'a t

filter ~f a filters elements out of the array a. Only the elements satisfying the given predicate f will be kept.

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

filter_map ~f [|a1; …; an|] calls (f a1) … (f an) and returns an array b consisting of all elements bi such as f ai = Some bi. When f returns None, the corresponding element of a is discarded.

val monoid_product : f:('a -> 'b -> 'c) -> 'a t -> 'b t -> 'c t

monoid_product ~f a b passes all combinaisons of tuples from the two arrays a and b to the function f.

  • since 2.8
val flat_map : f:('a -> 'b t) -> 'a t -> 'b array

flat_map ~f a transforms each element of a into an array, then flattens.

val except_idx : 'a t -> int -> 'a list

except_idx a i removes the element of a at given index i, and returns the list of the other elements.

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

Generic Functions

module type MONO_ARRAY = sig ... end
val sort_generic : (module MONO_ARRAY with type elt = 'elt and type t = 'arr) -> cmp:('elt -> 'elt -> int) --> 'arr -> unit

sort_generic (module M) ~cmp a sorts the array a, without allocating (eats stack space though). Performance might be lower than Array.sort.

  • since 0.14

Infix Operators

It is convenient to open CCArray.Infix to access the infix operators without cluttering the scope too much.

  • since 2.7
module Infix : sig ... end
include module type of Infix
val (>>=) : 'a t -> ('a -> 'b t) -> 'b t

a >>= f is the infix version of flat_map.

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

a >>| f is the infix version of map.

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

a >|= f is the infix version of map.

  • since 0.8
val (--) : int -> int -> int t

x -- y creates an array containing integers in the range x .. y. Bounds included.

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

x --^ y creates an array containing integers in the range x .. y. Right bound excluded.

  • since 0.17

Let operators on OCaml >= 4.08.0, nothing otherwise

  • since 2.8
include CCShimsMkLet_.S with type 'a t_let := 'a array
val let+ : 'a array -> ('a -> 'b) -> 'b array
val and+ : 'a array -> 'b array -> ('a * 'b) array
val let* : 'a array -> ('a -> 'b array) -> 'b array
val and* : 'a array -> 'b array -> ('a * 'b) array
\ No newline at end of file +->
'arr -> unit

sort_generic (module M) ~cmp a sorts the array a, without allocating (eats stack space though). Performance might be lower than Array.sort.

  • since 0.14

Infix Operators

It is convenient to open CCArray.Infix to access the infix operators without cluttering the scope too much.

module Infix : sig ... end
include module type of Infix
val (>>=) : 'a t -> ('a -> 'b t) -> 'b t

a >>= f is the infix version of flat_map.

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

a >>| f is the infix version of map.

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

a >|= f is the infix version of map.

  • since 0.8
val (--) : int -> int -> int t

x -- y creates an array containing integers in the range x .. y. Bounds included.

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

x --^ y creates an array containing integers in the range x .. y. Right bound excluded.

  • since 0.17

Let operators on OCaml >= 4.08.0, nothing otherwise

  • since 2.8
val let+ : 'a array -> ('a -> 'b) -> 'b array
val and+ : 'a array -> 'b array -> ('a * 'b) array
val let* : 'a array -> ('a -> 'b array) -> 'b array
val and* : 'a array -> 'b array -> ('a * 'b) array
\ No newline at end of file diff --git a/dev/containers/CCFun/index.html b/dev/containers/CCFun/index.html index 0765564d..43f496d3 100644 --- a/dev/containers/CCFun/index.html +++ b/dev/containers/CCFun/index.html @@ -1,4 +1,4 @@ -CCFun (containers.CCFun)

Module CCFun

Basic operations on Functions

include module type of CCShimsFun_
include module type of Stdlib.Fun
val id : 'a -> 'a
val const : 'a -> 'b -> 'a
val flip : ('a -> 'b -> 'c) -> 'b -> 'a -> 'c
val negate : ('a -> bool) -> 'a -> bool
val protect : finally:(unit -> unit) -> (unit -> 'a) -> 'a
exception Finally_raised of exn
val compose : ('a -> 'b) -> ('b -> 'c) -> 'a -> 'c

compose f g x is g (f x). Composition.

val compose_binop : ('a -> 'b) -> ('b -> 'b -> 'c) -> 'a -> 'a -> 'c

compose_binop f g is fun x y -> g (f x) (f y). Example (partial order): List.sort (compose_binop fst CCInt.compare) [1, true; 2, false; 1, false].

  • since 0.6
val curry : (('a * 'b) -> 'c) -> 'a -> 'b -> 'c

curry f x y is f (x,y). Convert a function which accepts a pair of arguments into a function which accepts two arguments.

val uncurry : ('a -> 'b -> 'c) -> ('a * 'b) -> 'c

uncurry f (x,y) is f x y. Convert a function which accepts a two arguments into a function which accepts a pair of arguments.

val tap : ('a -> _) -> 'a -> 'a

tap f x evaluates f x, discards it, then returns x. Useful in a pipeline, for instance:

CCArray.(1 -- 10)
+CCFun (containers.CCFun)

Module CCFun

Basic operations on Functions

include module type of Stdlib.Fun
val id : 'a -> 'a
val const : 'a -> 'b -> 'a
val flip : ('a -> 'b -> 'c) -> 'b -> 'a -> 'c
val negate : ('a -> bool) -> 'a -> bool
val protect : finally:(unit -> unit) -> (unit -> 'a) -> 'a
exception Finally_raised of exn
val compose : ('a -> 'b) -> ('b -> 'c) -> 'a -> 'c

compose f g x is g (f x). Composition.

val compose_binop : ('a -> 'b) -> ('b -> 'b -> 'c) -> 'a -> 'a -> 'c

compose_binop f g is fun x y -> g (f x) (f y). Example (partial order): List.sort (compose_binop fst CCInt.compare) [1, true; 2, false; 1, false].

  • since 0.6
val curry : (('a * 'b) -> 'c) -> 'a -> 'b -> 'c

curry f x y is f (x,y). Convert a function which accepts a pair of arguments into a function which accepts two arguments.

val uncurry : ('a -> 'b -> 'c) -> ('a * 'b) -> 'c

uncurry f (x,y) is f x y. Convert a function which accepts a two arguments into a function which accepts a pair of arguments.

val tap : ('a -> _) -> 'a -> 'a

tap f x evaluates f x, discards it, then returns x. Useful in a pipeline, for instance:

CCArray.(1 -- 10)
 |> tap CCArray.shuffle
 |> tap @@ CCArray.sort Stdlib.compare
val lexicographic : ('a -> 'a -> int) -> ('a -> 'a -> int) -> 'a -> 'a -> int

Lexicographic combination of comparison functions.

val finally : h:(unit -> _) -> f:(unit -> 'a) -> 'a

finally ~h f calls f () and returns its result. If it raises, the same exception is raised; in any case, h () is called after f () terminates. If h () raises an exception, then this exception will be passed on and any exception that may have been raised by f () is lost.

val finally1 : h:(unit -> _) -> ('a -> 'b) -> 'a -> 'b

finally1 ~h f x is the same as f x, but after the computation, h () is called whether f x rose an exception or not. If h () raises an exception, then this exception will be passed on and any exception that may have been raised by f () is lost.

  • since 0.16
val finally2 : h:(unit -> _) -> ('a -> 'b -> 'c) -> 'a -> 'b -> 'c

finally2 ~h f x y is the same as f x y, but after the computation, h () is called whether f x y rose an exception or not. If h () raises an exception, then this exception will be passed on and any exception that may have been raised by f () is lost.

  • since 0.16
val opaque_identity : 'a -> 'a

opaque_identity x is like x, but prevents Flambda from using x's definition for optimizing it. (flambda is an optimization/inlining pass in OCaml >= 4.03).

  • since 0.18
val iterate : int -> ('a -> 'a) -> 'a -> 'a

iterate n f is f iterated n times. That is to say, iterate 0 f x is x, iterate 1 f x is f x, iterate 2 f x is f (f x), etc.

  • since 2.1

Infix

Infix operators.

module Infix : sig ... end
include module type of Infix
val (|>) : 'a -> ('a -> 'b) -> 'b

x |> f is the same as f x. A 'pipe' operator.

val (@@) : ('a -> 'b) -> 'a -> 'b

f @@ x is the same as f x, but right-associative.

  • since 0.5
val (%>) : ('a -> 'b) -> ('b -> 'c) -> 'a -> 'c

(f %> g) x or (%>) f g x is g (f x). Alias to compose.

val (%) : ('b -> 'c) -> ('a -> 'b) -> 'a -> 'c

(f % g) x or (%) f g x is f (g x). Mathematical composition.

Monad

Functions with a fixed domain are monads in their codomain.

module Monad (X : sig ... end) : sig ... end
\ No newline at end of file diff --git a/dev/containers/CCInt/index.html b/dev/containers/CCInt/index.html index 0c145b90..b7f94ce1 100644 --- a/dev/containers/CCInt/index.html +++ b/dev/containers/CCInt/index.html @@ -1,2 +1,2 @@ -CCInt (containers.CCInt)

Module CCInt

Basic Int functions

include module type of CCShimsInt_
include module type of struct include Stdlib.Int end
type t = int
val zero : t

zero is the integer 0.

  • since 3.0
val one : t

one is the integer 1.

  • since 3.0
val minus_one : t

minus_one is the integer -1.

  • since 3.0
val add : t -> t -> t

add x y is x + y.

  • since 3.0
val sub : t -> t -> t

sub x y is x - y.

  • since 3.0
val mul : t -> t -> t

mul x y is x * y.

  • since 3.0
val div : t -> t -> t

div x y is x / y

  • since 3.0
val succ : t -> t

succ x is x + 1.

  • since 3.0
val pred : t -> t

pred x is x - 1.

  • since 3.0
val abs : t -> t

abs x is the absolute value of x. It is x if x is positive and neg x otherwise.

  • since 3.0
val max_int : t

max_int is the maximum integer.

  • since 3.0
val min_int : t

min_int is the minimum integer.

  • since 3.0
val compare : t -> t -> int

compare x y is the comparison function for integers with the same specification as Stdlib.compare.

val equal : t -> t -> bool

equal x y is true iff x and y are equal. Equality function for integers.

val hash : t -> int

hash x computes the hash of x.

val sign : t -> int

sign x return 0 if x = 0, -1 if x < 0 and 1 if x > 0. Same as compare x 0.

val neg : t -> t

neg x is - x. Unary negation.

  • since 0.5
val pow : t -> t -> t

pow base exponent returns base raised to the power of exponent. pow x y = x^y for positive integers x and y. Raises Invalid_argument if x = y = 0 or y < 0.

  • since 0.11
val floor_div : t -> t -> t

floor_div x n is integer division rounding towards negative infinity. It satisfies x = m * floor_div x n + rem x n.

  • since 1.2
val rem : t -> t -> t

rem x n is the remainder of dividing x by n, with the same sign as n.

  • since 1.2
type 'a printer = Stdlib.Format.formatter -> 'a -> unit
type 'a random_gen = Stdlib.Random.State.t -> 'a
type 'a iter = ('a -> unit) -> unit
val random : int -> t random_gen
val random_small : t random_gen
val random_range : int -> int -> t random_gen
val pp : t printer

pp ppf x prints the integer x on ppf.

val to_float : t -> float

to_float is the same as float_of_int

  • since 3.0
val to_string : t -> string

to_string x returns the string representation of the integer x, in signed decimal.

  • since 0.13
val of_string : string -> t option

of_string s converts the given string s into an integer. Safe version of of_string_exn.

  • since 0.13
val of_string_exn : string -> t

of_string_exn s converts the given string s to an integer. Alias to int_of_string.

  • raises Failure

    in case of failure.

  • since 3.0
val of_float : float -> t

of_float x converts the given floating-point number x to an integer. Alias to int_of_float.

  • since 3.0
val pp_binary : t printer

pp_binary ppf x prints x on ppf. Print as "0b00101010".

  • since 0.20
val to_string_binary : t -> string

to_string_binary x returns the string representation of the integer x, in binary.

  • since 0.20
val min : t -> t -> t

min x y returns the minimum of the two integers x and y.

  • since 0.17
val max : t -> t -> t

max x y returns the maximum of the two integers x and y.

  • since 0.17
val range_by : step:t -> t -> t -> t iter

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 1.2
val range : t -> t -> t iter

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

  • since 1.2
val range' : t -> t -> t iter

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

  • since 1.2
val popcount : t -> int

Number of bits set to 1

  • since 3.0
val logand : t -> t -> t

logand is the same as (land).

  • since 3.0
val logor : t -> t -> t

logand is the same as (lor).

  • since 3.0
val logxor : t -> t -> t

logxor is the same as (lxor).

  • since 3.0
val lognot : t -> t

logand is the same as lnot.

  • since 3.0
val shift_left : t -> int -> t

shift_left is the same as (lsl).

  • since 3.0
val shift_right : t -> int -> t

shift_right is the same as (asr).

  • since 3.0
val shift_right_logical : t -> int -> t

shift_right_logical is the same as (lsr).

  • since 3.0

Infix Operators

  • since 0.17
module Infix : sig ... end
include module type of Infix
val (=) : t -> t -> bool
  • since 0.17
val (<>) : t -> t -> bool
  • since 0.17
val (<) : t -> t -> bool
  • since 0.17
val (>) : t -> t -> bool
  • since 0.17
val (<=) : t -> t -> bool
  • since 0.17
val (>=) : t -> t -> bool
  • since 0.17
val (--) : t -> t -> t iter

Alias to range.

  • since 1.2
val (--^) : t -> t -> t iter

Alias to range'.

  • since 1.2
val (+) : t -> t -> t
  • since 2.1
val (-) : t -> t -> t
  • since 2.1
val (~-) : t -> t
  • since 2.1
val (*) : t -> t -> t
  • since 2.1
val (/) : t -> t -> t
  • since 2.1
val (**) : t -> t -> t
  • since 2.4
val (mod) : t -> t -> t
  • since 2.1
val (land) : t -> t -> t
  • since 2.1
val (lor) : t -> t -> t
  • since 2.1
val (lxor) : t -> t -> t
  • since 2.1
val lnot : t -> t
  • since 2.1
val (lsl) : t -> int -> t
  • since 2.1
val (lsr) : t -> int -> t
  • since 2.1
val (asr) : t -> int -> t
  • since 2.1
\ No newline at end of file +CCInt (containers.CCInt)

Module CCInt

Basic Int functions

include module type of struct include Stdlib.Int end
type t = int
val zero : t

zero is the integer 0.

  • since 3.0
val one : t

one is the integer 1.

  • since 3.0
val minus_one : t

minus_one is the integer -1.

  • since 3.0
val add : t -> t -> t

add x y is x + y.

  • since 3.0
val sub : t -> t -> t

sub x y is x - y.

  • since 3.0
val mul : t -> t -> t

mul x y is x * y.

  • since 3.0
val div : t -> t -> t

div x y is x / y

  • since 3.0
val succ : t -> t

succ x is x + 1.

  • since 3.0
val pred : t -> t

pred x is x - 1.

  • since 3.0
val abs : t -> t

abs x is the absolute value of x. It is x if x is positive and neg x otherwise.

  • since 3.0
val max_int : t

max_int is the maximum integer.

  • since 3.0
val min_int : t

min_int is the minimum integer.

  • since 3.0
val compare : t -> t -> int

compare x y is the comparison function for integers with the same specification as Stdlib.compare.

val equal : t -> t -> bool

equal x y is true iff x and y are equal. Equality function for integers.

val hash : t -> int

hash x computes the hash of x.

val sign : t -> int

sign x return 0 if x = 0, -1 if x < 0 and 1 if x > 0. Same as compare x 0.

val neg : t -> t

neg x is - x. Unary negation.

  • since 0.5
val pow : t -> t -> t

pow base exponent returns base raised to the power of exponent. pow x y = x^y for positive integers x and y. Raises Invalid_argument if x = y = 0 or y < 0.

  • since 0.11
val floor_div : t -> t -> t

floor_div x n is integer division rounding towards negative infinity. It satisfies x = m * floor_div x n + rem x n.

  • since 1.2
val rem : t -> t -> t

rem x n is the remainder of dividing x by n, with the same sign as n.

  • since 1.2
type 'a printer = Stdlib.Format.formatter -> 'a -> unit
type 'a random_gen = Stdlib.Random.State.t -> 'a
type 'a iter = ('a -> unit) -> unit
val random : int -> t random_gen
val random_small : t random_gen
val random_range : int -> int -> t random_gen
val pp : t printer

pp ppf x prints the integer x on ppf.

val to_float : t -> float

to_float is the same as float_of_int

  • since 3.0
val to_string : t -> string

to_string x returns the string representation of the integer x, in signed decimal.

  • since 0.13
val of_string : string -> t option

of_string s converts the given string s into an integer. Safe version of of_string_exn.

  • since 0.13
val of_string_exn : string -> t

of_string_exn s converts the given string s to an integer. Alias to int_of_string.

  • raises Failure

    in case of failure.

  • since 3.0
val of_float : float -> t

of_float x converts the given floating-point number x to an integer. Alias to int_of_float.

  • since 3.0
val pp_binary : t printer

pp_binary ppf x prints x on ppf. Print as "0b00101010".

  • since 0.20
val to_string_binary : t -> string

to_string_binary x returns the string representation of the integer x, in binary.

  • since 0.20
val min : t -> t -> t

min x y returns the minimum of the two integers x and y.

  • since 0.17
val max : t -> t -> t

max x y returns the maximum of the two integers x and y.

  • since 0.17
val range_by : step:t -> t -> t -> t iter

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 1.2
val range : t -> t -> t iter

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

  • since 1.2
val range' : t -> t -> t iter

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

  • since 1.2
val popcount : t -> int

Number of bits set to 1

  • since 3.0
val logand : t -> t -> t

logand is the same as (land).

  • since 3.0
val logor : t -> t -> t

logand is the same as (lor).

  • since 3.0
val logxor : t -> t -> t

logxor is the same as (lxor).

  • since 3.0
val lognot : t -> t

logand is the same as lnot.

  • since 3.0
val shift_left : t -> int -> t

shift_left is the same as (lsl).

  • since 3.0
val shift_right : t -> int -> t

shift_right is the same as (asr).

  • since 3.0
val shift_right_logical : t -> int -> t

shift_right_logical is the same as (lsr).

  • since 3.0

Infix Operators

  • since 0.17
module Infix : sig ... end
include module type of Infix
val (=) : t -> t -> bool
  • since 0.17
val (<>) : t -> t -> bool
  • since 0.17
val (<) : t -> t -> bool
  • since 0.17
val (>) : t -> t -> bool
  • since 0.17
val (<=) : t -> t -> bool
  • since 0.17
val (>=) : t -> t -> bool
  • since 0.17
val (--) : t -> t -> t iter

Alias to range.

  • since 1.2
val (--^) : t -> t -> t iter

Alias to range'.

  • since 1.2
val (+) : t -> t -> t
  • since 2.1
val (-) : t -> t -> t
  • since 2.1
val (~-) : t -> t
  • since 2.1
val (*) : t -> t -> t
  • since 2.1
val (/) : t -> t -> t
  • since 2.1
val (**) : t -> t -> t
  • since 2.4
val (mod) : t -> t -> t
  • since 2.1
val (land) : t -> t -> t
  • since 2.1
val (lor) : t -> t -> t
  • since 2.1
val (lxor) : t -> t -> t
  • since 2.1
val lnot : t -> t
  • since 2.1
val (lsl) : t -> int -> t
  • since 2.1
val (lsr) : t -> int -> t
  • since 2.1
val (asr) : t -> int -> t
  • since 2.1
\ No newline at end of file diff --git a/dev/containers/CCList/Infix/index.html b/dev/containers/CCList/Infix/index.html index 3ff3f324..8de11025 100644 --- a/dev/containers/CCList/Infix/index.html +++ b/dev/containers/CCList/Infix/index.html @@ -1,9 +1,9 @@ -Infix (containers.CCList.Infix)

Module CCList.Infix

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

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

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

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

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

f <$> l is like map.

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

l >>= f is flat_map f l.

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

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

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

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

  • since 0.17

Let operators on OCaml >= 4.08.0, nothing otherwise

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

Module CCList.Infix

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

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

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

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

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

f <$> l is like map.

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

l >>= f is flat_map f l.

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

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

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

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

  • since 0.17

Let operators on OCaml >= 4.08.0, nothing otherwise

  • since 2.8
val let+ : 'a list -> ('a -> 'b) -> 'b list
val and+ : 'a list -> 'b list -> ('a * 'b) list
val let* : 'a list -> ('a -> 'b list) -> 'b list
val and* : 'a list -> 'b list -> ('a * 'b) list
val 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
\ No newline at end of file +- : int list = [16; 18]
  • since 3.1
\ No newline at end of file diff --git a/dev/containers/CCList/index.html b/dev/containers/CCList/index.html index 5fa46640..9505c176 100644 --- a/dev/containers/CCList/index.html +++ b/dev/containers/CCList/index.html @@ -1,5 +1,5 @@ -CCList (containers.CCList)

Module CCList

Complements to List

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

Fast internal iterator.

  • since 2.8
type 'a gen = unit -> 'a option
type 'a printer = Stdlib.Format.formatter -> 'a -> unit
type 'a random_gen = Stdlib.Random.State.t -> 'a
include module type of Stdlib.List
val length : 'a list -> int
val cons : 'a -> 'a list -> 'a list
val hd : 'a list -> 'a
val tl : 'a list -> 'a list
val nth : 'a list -> int -> 'a
val rev : 'a list -> 'a list
val rev_append : 'a list -> 'a list -> 'a list
val concat : 'a list list -> 'a list
val iter : ('a -> unit) -> 'a list -> unit
val rev_map : ('a -> 'b) -> 'a list -> 'b list
val concat_map : ('a -> 'b list) -> 'a list -> 'b list
val fold_left_map : ('a -> 'b -> 'a * 'c) -> 'a -> 'b list -> 'a * 'c list
val fold_left : ('a -> 'b -> 'a) -> 'a -> 'b list -> 'a
val iter2 : ('a -> 'b -> unit) -> 'a list -> 'b list -> unit
val map2 : ('a -> 'b -> 'c) -> 'a list -> 'b list -> 'c list
val rev_map2 : ('a -> 'b -> 'c) -> 'a list -> 'b list -> 'c list
val fold_left2 : ('a -> 'b -> 'c -> 'a) -> 'a -> 'b list -> 'c list -> 'a
val fold_right2 : ('a -> 'b -> 'c -> 'c) -> 'a list -> 'b list -> 'c -> 'c
val for_all : ('a -> bool) -> 'a list -> bool
val exists : ('a -> bool) -> 'a list -> bool
val for_all2 : ('a -> 'b -> bool) -> 'a list -> 'b list -> bool
val exists2 : ('a -> 'b -> bool) -> 'a list -> 'b list -> bool
val memq : 'a -> 'a list -> bool
val find : ('a -> bool) -> 'a list -> 'a
val find_all : ('a -> bool) -> 'a list -> 'a list
val filteri : (int -> 'a -> bool) -> 'a list -> 'a list
val partition : ('a -> bool) -> 'a list -> 'a list * 'a list
val assq : 'a -> ('a * 'b) list -> 'b
val mem_assq : 'a -> ('a * 'b) list -> bool
val remove_assq : 'a -> ('a * 'b) list -> ('a * 'b) list
val sort : ('a -> 'a -> int) -> 'a list -> 'a list
val stable_sort : ('a -> 'a -> int) -> 'a list -> 'a list
val fast_sort : ('a -> 'a -> int) -> 'a list -> 'a list
val merge : ('a -> 'a -> int) -> 'a list -> 'a list -> 'a list
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

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

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

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

  • since 0.5
val 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.

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

l1 @ l2 is like append l1 l2. Concatenate the two lists l1 and l2.

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

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 l is preserved. Safe version of List.filter.

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

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

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

fold_while f init l folds until a stop condition via ('a, `Stop) is indicated by the accumulator.

  • since 0.8
val fold_map : ('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 reduce : ('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
val reduce_exn : ('a -> 'a -> 'a) -> 'a list -> 'a

reduce_exn is the unsafe version of reduce.

  • raises Invalid_argument

    if the given list is empty.

  • since 3.2
val fold_map2 : ('acc -> 'a -> 'b -> 'acc * 'c) -> '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.

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

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

  • 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

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

  • raises Invalid_argument

    if the lists have distinct lengths.

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

combine_gen l1 l2 transforms two lists into 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
val 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
val split : ('a * 'b) t -> 'a t * 'b t

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

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

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

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

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

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

flat_map f l maps and flattens at the same time (safe). Evaluation order is not guaranteed.

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

flat_map_i f l maps with index and flattens at the same time (safe). Evaluation order is not guaranteed.

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

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

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

product comb l1 l2 computes the cartesian product of the two lists, with the given combinator comb.

val fold_product : ('c -> 'a -> 'b -> 'c) -> '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.

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

cartesian_product [[l1]; [l2]; …; [ln]] produces the cartesian product of this list of lists, by returning all the ways of picking one element per sublist. NOTE the order of the returned list is unspecified. For example:

# cartesian_product [[1;2];[3];[4;5;6]] |> sort =
+CCList (containers.CCList)

Module CCList

Complements to List

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

Fast internal iterator.

  • since 2.8
type 'a gen = unit -> 'a option
type 'a printer = Stdlib.Format.formatter -> 'a -> unit
type 'a random_gen = Stdlib.Random.State.t -> 'a
include module type of Stdlib.List
val length : 'a list -> int
val cons : 'a -> 'a list -> 'a list
val hd : 'a list -> 'a
val tl : 'a list -> 'a list
val nth : 'a list -> int -> 'a
val rev : 'a list -> 'a list
val rev_append : 'a list -> 'a list -> 'a list
val concat : 'a list list -> 'a list
val iter : ('a -> unit) -> 'a list -> unit
val rev_map : ('a -> 'b) -> 'a list -> 'b list
val concat_map : ('a -> 'b list) -> 'a list -> 'b list
val fold_left_map : ('a -> 'b -> 'a * 'c) -> 'a -> 'b list -> 'a * 'c list
val fold_left : ('a -> 'b -> 'a) -> 'a -> 'b list -> 'a
val iter2 : ('a -> 'b -> unit) -> 'a list -> 'b list -> unit
val map2 : ('a -> 'b -> 'c) -> 'a list -> 'b list -> 'c list
val rev_map2 : ('a -> 'b -> 'c) -> 'a list -> 'b list -> 'c list
val fold_left2 : ('a -> 'b -> 'c -> 'a) -> 'a -> 'b list -> 'c list -> 'a
val fold_right2 : ('a -> 'b -> 'c -> 'c) -> 'a list -> 'b list -> 'c -> 'c
val for_all : ('a -> bool) -> 'a list -> bool
val exists : ('a -> bool) -> 'a list -> bool
val for_all2 : ('a -> 'b -> bool) -> 'a list -> 'b list -> bool
val exists2 : ('a -> 'b -> bool) -> 'a list -> 'b list -> bool
val memq : 'a -> 'a list -> bool
val find : ('a -> bool) -> 'a list -> 'a
val find_all : ('a -> bool) -> 'a list -> 'a list
val filteri : (int -> 'a -> bool) -> 'a list -> 'a list
val partition : ('a -> bool) -> 'a list -> 'a list * 'a list
val assq : 'a -> ('a * 'b) list -> 'b
val mem_assq : 'a -> ('a * 'b) list -> bool
val remove_assq : 'a -> ('a * 'b) list -> ('a * 'b) list
val sort : ('a -> 'a -> int) -> 'a list -> 'a list
val stable_sort : ('a -> 'a -> int) -> 'a list -> 'a list
val fast_sort : ('a -> 'a -> int) -> 'a list -> 'a list
val merge : ('a -> 'a -> int) -> 'a list -> 'a list -> 'a list
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

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

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

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

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 l is preserved. Safe version of List.filter.

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

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

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

fold_while f init l folds until a stop condition via ('a, `Stop) is indicated by the accumulator.

  • since 0.8
val fold_map : ('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 reduce : ('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
val reduce_exn : ('a -> 'a -> 'a) -> 'a list -> 'a

reduce_exn is the unsafe version of reduce.

  • raises Invalid_argument

    if the given list is empty.

  • since 3.2
val fold_map2 : ('acc -> 'a -> 'b -> 'acc * 'c) -> '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.

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

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

  • 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

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

  • raises Invalid_argument

    if the lists have distinct lengths.

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

combine_gen l1 l2 transforms two lists into 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
val 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
val split : ('a * 'b) t -> 'a t * 'b t

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

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

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

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

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

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

flat_map f l maps and flattens at the same time (safe). Evaluation order is not guaranteed.

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

flat_map_i f l maps with index and flattens at the same time (safe). Evaluation order is not guaranteed.

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

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

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

product comb l1 l2 computes the cartesian product of the two lists, with the given combinator comb.

val fold_product : ('c -> 'a -> 'b -> 'c) -> '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.

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

cartesian_product [[l1]; [l2]; …; [ln]] produces the cartesian product of this list of lists, by returning all the ways of picking one element per sublist. NOTE the order of the returned list is unspecified. For example:

# cartesian_product [[1;2];[3];[4;5;6]] |> sort =
 [[1;3;4];[1;3;5];[1;3;6];[2;3;4];[2;3;5];[2;3;6]];;
 # cartesian_product [[1;2];[];[4;5;6]] = [];;
 # cartesian_product [[1;2];[3];[4];[5];[6]] |> sort =
@@ -14,5 +14,12 @@
       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_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
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 3.5
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 3.5
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 3.5
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 (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
val let+ : 'a list -> ('a -> 'b) -> 'b list
val and+ : 'a list -> 'b list -> ('a * 'b) list
val let* : 'a list -> ('a -> 'b list) -> 'b list
val and* : 'a list -> 'b list -> ('a * 'b) list

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

return x is x.

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 3.5
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 3.5
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 3.5
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 3.5
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 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 (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
include module type of Infix
val (>|=) : 'a t -> ('a -> 'b) -> 'b t

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

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

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

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

f <$> l is like map.

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

l >>= f is flat_map f l.

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

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

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

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

  • since 0.17

Let operators on OCaml >= 4.08.0, nothing otherwise

  • since 2.8
val let+ : 'a list -> ('a -> 'b) -> 'b list
val and+ : 'a list -> 'b list -> ('a * 'b) list
val let* : 'a list -> ('a -> 'b list) -> 'b list
val and* : 'a list -> 'b list -> ('a * 'b) list
val 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

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/Infix/index.html b/dev/containers/CCListLabels/Infix/index.html index a1f2d7d9..7eae19b8 100644 --- a/dev/containers/CCListLabels/Infix/index.html +++ b/dev/containers/CCListLabels/Infix/index.html @@ -1,9 +1,9 @@ -Infix (containers.CCListLabels.Infix)

Module CCListLabels.Infix

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

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

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

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

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

f <$> l is like map.

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

l >>= f is flat_map f l.

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

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

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

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

  • since 0.17

Let operators on OCaml >= 4.08.0, nothing otherwise

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

Module CCListLabels.Infix

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

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

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

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

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

f <$> l is like map.

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

l >>= f is flat_map f l.

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

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

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

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

  • since 0.17

Let operators on OCaml >= 4.08.0, nothing otherwise

  • since 2.8
val let+ : 'a list -> ('a -> 'b) -> 'b list
val and+ : 'a list -> 'b list -> ('a * 'b) list
val let* : 'a list -> ('a -> 'b list) -> 'b list
val and* : 'a list -> 'b list -> ('a * 'b) list
val 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
\ No newline at end of file +- : int list = [16; 18] \ No newline at end of file diff --git a/dev/containers/CCListLabels/index.html b/dev/containers/CCListLabels/index.html index 531cb256..2b6fd43d 100644 --- a/dev/containers/CCListLabels/index.html +++ b/dev/containers/CCListLabels/index.html @@ -1,5 +1,5 @@ -CCListLabels (containers.CCListLabels)

Module CCListLabels

Complements to ListLabels

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

Fast internal iterator.

  • since 2.8
type 'a gen = unit -> 'a option
type 'a printer = Stdlib.Format.formatter -> 'a -> unit
type 'a random_gen = Stdlib.Random.State.t -> 'a
include module type of Stdlib.ListLabels
val length : 'a list -> int
val hd : 'a list -> 'a
val tl : 'a list -> 'a list
val nth : 'a list -> int -> 'a
val rev : 'a list -> 'a list
val rev_append : 'a list -> 'a list -> 'a list
val concat : 'a list list -> 'a list
val iter : f:('a -> unit) -> 'a list -> unit
val rev_map : f:('a -> 'b) -> 'a list -> 'b list
val concat_map : f:('a -> 'b list) -> 'a list -> 'b list
val fold_left_map : f:('a -> 'b -> 'a * 'c) -> init:'a -> 'b list -> 'a * 'c list
val fold_left : f:('a -> 'b -> 'a) -> init:'a -> 'b list -> 'a
val iter2 : f:('a -> 'b -> unit) -> 'a list -> 'b list -> unit
val map2 : f:('a -> 'b -> 'c) -> 'a list -> 'b list -> 'c list
val rev_map2 : f:('a -> 'b -> 'c) -> 'a list -> 'b list -> 'c list
val fold_left2 : f:('a -> 'b -> 'c -> 'a) -> init:'a -> 'b list -> 'c list -> 'a
val fold_right2 : f:('a -> 'b -> 'c -> 'c) -> 'a list -> 'b list -> init:'c -> 'c
val for_all : f:('a -> bool) -> 'a list -> bool
val exists : f:('a -> bool) -> 'a list -> bool
val for_all2 : f:('a -> 'b -> bool) -> 'a list -> 'b list -> bool
val exists2 : f:('a -> 'b -> bool) -> 'a list -> 'b list -> bool
val memq : 'a -> set:'a list -> bool
val find : f:('a -> bool) -> 'a list -> 'a
val find_all : f:('a -> bool) -> 'a list -> 'a list
val filteri : f:(int -> 'a -> bool) -> 'a list -> 'a list
val partition : f:('a -> bool) -> 'a list -> 'a list * 'a list
val assq : 'a -> ('a * 'b) list -> 'b
val mem_assq : 'a -> map:('a * 'b) list -> bool
val remove_assq : 'a -> ('a * 'b) list -> ('a * 'b) list
val sort : cmp:('a -> 'a -> int) -> 'a list -> 'a list
val stable_sort : cmp:('a -> 'a -> int) -> 'a list -> 'a list
val fast_sort : cmp:('a -> 'a -> int) -> 'a list -> 'a list
val merge : cmp:('a -> 'a -> int) -> 'a list -> 'a list -> 'a list
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 : 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.

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

l >|= f is the 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

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

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

l1 @ l2 is like append l1 l2. Concatenate the two lists l1 and l2.

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

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

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

fold_while ~f ~init l folds until a stop condition via ('a, `Stop) is indicated by the accumulator.

  • since 0.8
val fold_map : f:('acc -> 'a -> 'acc * 'b) -> init:'acc -> 'a list -> 'acc * 'b list

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

  • since 0.14
val fold_map_i : f:('acc -> int -> 'a -> 'acc * 'b) -> init:'acc -> +CCListLabels (containers.CCListLabels)

Module CCListLabels

Complements to ListLabels

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

Fast internal iterator.

  • since 2.8
type 'a gen = unit -> 'a option
type 'a printer = Stdlib.Format.formatter -> 'a -> unit
type 'a random_gen = Stdlib.Random.State.t -> 'a
include module type of Stdlib.ListLabels
val length : 'a list -> int
val hd : 'a list -> 'a
val tl : 'a list -> 'a list
val nth : 'a list -> int -> 'a
val rev : 'a list -> 'a list
val rev_append : 'a list -> 'a list -> 'a list
val concat : 'a list list -> 'a list
val iter : f:('a -> unit) -> 'a list -> unit
val rev_map : f:('a -> 'b) -> 'a list -> 'b list
val concat_map : f:('a -> 'b list) -> 'a list -> 'b list
val fold_left_map : f:('a -> 'b -> 'a * 'c) -> init:'a -> 'b list -> 'a * 'c list
val fold_left : f:('a -> 'b -> 'a) -> init:'a -> 'b list -> 'a
val iter2 : f:('a -> 'b -> unit) -> 'a list -> 'b list -> unit
val map2 : f:('a -> 'b -> 'c) -> 'a list -> 'b list -> 'c list
val rev_map2 : f:('a -> 'b -> 'c) -> 'a list -> 'b list -> 'c list
val fold_left2 : f:('a -> 'b -> 'c -> 'a) -> init:'a -> 'b list -> 'c list -> 'a
val fold_right2 : f:('a -> 'b -> 'c -> 'c) -> 'a list -> 'b list -> init:'c -> 'c
val for_all : f:('a -> bool) -> 'a list -> bool
val exists : f:('a -> bool) -> 'a list -> bool
val for_all2 : f:('a -> 'b -> bool) -> 'a list -> 'b list -> bool
val exists2 : f:('a -> 'b -> bool) -> 'a list -> 'b list -> bool
val memq : 'a -> set:'a list -> bool
val find : f:('a -> bool) -> 'a list -> 'a
val find_all : f:('a -> bool) -> 'a list -> 'a list
val filteri : f:(int -> 'a -> bool) -> 'a list -> 'a list
val partition : f:('a -> bool) -> 'a list -> 'a list * 'a list
val assq : 'a -> ('a * 'b) list -> 'b
val mem_assq : 'a -> map:('a * 'b) list -> bool
val remove_assq : 'a -> ('a * 'b) list -> ('a * 'b) list
val sort : cmp:('a -> 'a -> int) -> 'a list -> 'a list
val stable_sort : cmp:('a -> 'a -> int) -> 'a list -> 'a list
val fast_sort : cmp:('a -> 'a -> int) -> 'a list -> 'a list
val merge : cmp:('a -> 'a -> int) -> 'a list -> 'a list -> 'a list
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 : 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.

val cons : 'a -> 'a t -> 'a t

cons x l is x::l.

  • since 0.12
val 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.

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

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

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

fold_while ~f ~init l folds until a stop condition via ('a, `Stop) is indicated by the accumulator.

  • since 0.8
val fold_map : f:('acc -> 'a -> 'acc * 'b) -> init:'acc -> 'a list -> 'acc * 'b list

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

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

fold_map_i ~f ~init l is 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) -> 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
val scan_left : f:('acc -> 'a -> 'acc) -> init:'acc -> 'a list -> 'acc list

scan_left ~f ~init l returns the list [init; f init x0; f (f init x0) x1; …] where x0, x1, etc. are the elements of l.

  • since 1.2, but only
  • since 2.2 with labels
val 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
val reduce_exn : f:('a -> 'a -> 'a) -> 'a list -> 'a

reduce_exn is the unsafe version of reduce.

  • raises Invalid_argument

    if the given list is empty.

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

fold_map2 ~f ~init l1 l2 is 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 : f:('acc -> 'a -> 'acc * 'b option) -> init:'acc -> @@ -23,5 +23,12 @@ 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_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
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 3.5
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 3.5
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 3.5
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 (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
val let+ : 'a list -> ('a -> 'b) -> 'b list
val and+ : 'a list -> 'b list -> ('a * 'b) list
val let* : 'a list -> ('a -> 'b list) -> 'b list
val and* : 'a list -> 'b list -> ('a * 'b) list

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

return x is x.

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 3.5
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 3.5
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 3.5
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 3.5
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 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 (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
include module type of Infix
val (>|=) : 'a t -> ('a -> 'b) -> 'b t

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

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

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

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

f <$> l is like map.

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

l >>= f is flat_map f l.

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

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

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

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

  • since 0.17

Let operators on OCaml >= 4.08.0, nothing otherwise

  • since 2.8
val let+ : 'a list -> ('a -> 'b) -> 'b list
val and+ : 'a list -> 'b list -> ('a * 'b) list
val let* : 'a list -> ('a -> 'b list) -> 'b list
val and* : 'a list -> 'b list -> ('a * 'b) list
val 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

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/CCOpt/Infix/index.html b/dev/containers/CCOpt/Infix/index.html index f254fdab..0741f037 100644 --- a/dev/containers/CCOpt/Infix/index.html +++ b/dev/containers/CCOpt/Infix/index.html @@ -1,2 +1,2 @@ -Infix (containers.CCOpt.Infix)

Module CCOpt.Infix

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

o >|= f is map f o.

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

o >>= f is the monadic bind.

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

f <*> o returns Some (f x) if o is Some x and None if o is None.

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

f <$> o is like map f o.

val (<+>) : 'a t -> 'a t -> 'a t

o1 <+> o2 is o1 if o1 is Some _, o2 if o1 is None.

Let operators on OCaml >= 4.08.0, nothing otherwise

  • since 2.8
include CCShimsMkLet_.S with type 'a t_let := 'a option
val let+ : 'a option -> ('a -> 'b) -> 'b option
val and+ : 'a option -> 'b option -> ('a * 'b) option
val let* : 'a option -> ('a -> 'b option) -> 'b option
val and* : 'a option -> 'b option -> ('a * 'b) option
\ No newline at end of file +Infix (containers.CCOpt.Infix)

Module CCOpt.Infix

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

o >|= f is map f o.

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

o >>= f is the monadic bind.

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

f <*> o returns Some (f x) if o is Some x and None if o is None.

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

f <$> o is like map f o.

val (<+>) : 'a t -> 'a t -> 'a t

o1 <+> o2 is o1 if o1 is Some _, o2 if o1 is None.

Let operators on OCaml >= 4.08.0, nothing otherwise

  • since 2.8
val let+ : 'a option -> ('a -> 'b) -> 'b option
val and+ : 'a option -> 'b option -> ('a * 'b) option
val let* : 'a option -> ('a -> 'b option) -> 'b option
val and* : 'a option -> 'b option -> ('a * 'b) option
\ No newline at end of file diff --git a/dev/containers/CCOpt/index.html b/dev/containers/CCOpt/index.html index 081e9fb0..5f53a4ce 100644 --- a/dev/containers/CCOpt/index.html +++ b/dev/containers/CCOpt/index.html @@ -1,2 +1,2 @@ -CCOpt (containers.CCOpt)

Module CCOpt

Previous Option module

  • deprecated

    use `CCOption` instead.

include module type of CCOption
type +'a t = 'a option
val map : ('a -> 'b) -> 'a t -> 'b t

map f o applies the function f to the element inside o, if any.

val map_or : default:'b -> ('a -> 'b) -> 'a t -> 'b

map_or ~default f o is f x if o = Some x, default otherwise.

  • since 0.16
val map_lazy : (unit -> 'b) -> ('a -> 'b) -> 'a t -> 'b

map_lazy default_fn f o if f o if o = Some x, default_fn () otherwise.

  • since 1.2
val is_some : _ t -> bool

is_some (Some x) returns true otherwise it returns false.

val is_none : _ t -> bool

is_none None returns true otherwise it returns false.

  • since 0.11
val compare : ('a -> 'a -> int) -> 'a t -> 'a t -> int

compare comp o1 o2 compares two options o1 and o2, using custom comparators comp for the value. None is always assumed to be less than Some _.

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

equal p o1 o2 tests for equality between option types o1 and o2, using a custom equality predicate p.

val return : 'a -> 'a t

return x is a monadic return, that is return x = Some x.

val some : 'a -> 'a t

Alias to return.

  • since 3.5
val none : 'a t

Alias to None.

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

o >|= f is the infix version of map.

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

flat_map f o is equivalent to map followed by flatten. Flip version of >>=.

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

bind o f is f v if o is Some v, None otherwise. Monadic bind.

  • since 3.0
val (>>=) : 'a t -> ('a -> 'b t) -> 'b t

o >>= f is the infix version of bind.

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

map2 f o1 o2 maps 'a option and 'b option to a 'c option using f.

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

iter f o applies f to o. Iterate on 0 or 1 element.

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

fold f init o is f init x if o is Some x, or init if o is None. Fold on 0 or 1 element.

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

filter f o returns Some x if o is Some x and f x is true, or None if f x is false or if o is None. Filter on 0 or 1 element.

  • since 0.5
val if_ : ('a -> bool) -> 'a -> 'a option

if_ f x is Some x if f x, None otherwise.

  • since 0.17
val exists : ('a -> bool) -> 'a t -> bool

exists f o returns true iff there exists an element for which the provided function f evaluates to true.

  • since 0.17
val for_all : ('a -> bool) -> 'a t -> bool

for_all f o returns true iff the provided function f evaluates to true for all elements.

  • since 0.17
val get_or : default:'a -> 'a t -> 'a

get_or ~default o extracts the value from o, or returns default if o is None.

  • since 0.18
val value : 'a t -> default:'a -> 'a

value o ~default is similar to the Stdlib's Option.value and to get_or.

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

get_exn o returns x if o is Some x or fails if o is None.

  • raises Invalid_argument

    if the option is None.

  • deprecated

    use get_exn_or instead

val get_exn_or : string -> 'a t -> 'a

get_exn_or msg o returns x if o is Some x or fails with Invalid_argument msg if o is None.

  • raises Invalid_argument

    if the option is None.

  • since 3.4
val get_lazy : (unit -> 'a) -> 'a t -> 'a

get_lazy default_fn o unwraps o, but if o is None it returns default_fn () instead.

  • since 0.6.1
val sequence_l : 'a t list -> 'a list t

sequence_l [x1; x2; …; xn] returns Some [y1; y2; …; yn] if every xi is Some yi. Otherwise, if the list contains at least one None, the result is None.

val wrap : ?handler:(exn -> bool) -> ('a -> 'b) -> 'a -> 'b option

wrap ?handler f x calls f x and returns Some y if f x = y. If f x raises any exception, the result is None. This can be useful to wrap functions such as Map.S.find.

  • parameter handler

    the exception handler, which returns true if the exception is to be caught.

val wrap2 : ?handler:(exn -> bool) -> ('a -> 'b -> 'c) -> 'a -> 'b -> 'c option

wrap2 ?handler f x y is similar to wrap but for binary functions.

Applicative

val pure : 'a -> 'a t

pure x is an alias to return.

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

f <*> (Some x) returns Some (f x) and f <*> None returns None.

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

f <$> o is like map f o.

Alternatives

val or_ : else_:'a t -> 'a t -> 'a t

or_ ~else_ o is o if o is Some _, else_ if o is None.

  • since 1.2
val or_lazy : else_:(unit -> 'a t) -> 'a t -> 'a t

or_lazy ~else_ o is o if o is Some _, else_ () if o is None.

  • since 1.2
val (<+>) : 'a t -> 'a t -> 'a t

o1 <+> o2 is o1 if o1 is Some _, o2 if o1 is None.

val choice : 'a t list -> 'a t

choice lo returns the first non-None element of the list lo, or None.

val flatten : 'a t t -> 'a t

flatten oo transforms Some x into x.

  • since 2.2
val return_if : bool -> 'a -> 'a t

return_if b x applies Some or None depending on the boolean b. More precisely, return_if false x is None, and return_if true x is Some x.

  • since 2.2

Infix Operators

  • 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 option
val let+ : 'a option -> ('a -> 'b) -> 'b option
val and+ : 'a option -> 'b option -> ('a * 'b) option
val let* : 'a option -> ('a -> 'b option) -> 'b option
val and* : 'a option -> 'b option -> ('a * 'b) option

Conversion and IO

val to_list : 'a t -> 'a list

to_list o returns [x] if o is Some x or the empty list [] if o is None.

val of_list : 'a list -> 'a t

of_list l returns Some x (x being the head of the list l), or None if l is the empty list.

val to_result : 'e -> 'a t -> ('a'e) Stdlib.result

to_result e o returns Ok x if o is Some x, or Error e if o is None.

  • since 1.2
val to_result_lazy : (unit -> 'e) -> 'a t -> ('a'e) Stdlib.result

to_result_lazy f o returns Ok x if o is Some x or Error f if o is None.

  • since 1.2
val of_result : ('a_) Stdlib.result -> 'a t

of_result result returns an option from a result.

  • since 1.2
type 'a iter = ('a -> unit) -> unit
type 'a gen = unit -> 'a option
type 'a printer = Stdlib.Format.formatter -> 'a -> unit
type 'a random_gen = Stdlib.Random.State.t -> 'a
val random : 'a random_gen -> 'a t random_gen
val choice_iter : 'a t iter -> 'a t

choice_iter iter is similar to choice, but works on iter. It returns the first Some x occurring in iter, or None otherwise.

  • since 3.0
val choice_seq : 'a t Stdlib.Seq.t -> 'a t

choice_seq seq works on Seq.t. It returns the first Some x occurring in seq, or None otherwise.

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

to_gen o is o as a gen. Some x is the singleton gen containing x and None is the empty gen.

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

to_seq o is o as a sequence Seq.t. Some x is the singleton sequence containing x and None is the empty sequence. Same as Stdlib.Option.to_seq Renamed from to_std_seq since 3.0.

  • since 3.0
val to_iter : 'a t -> 'a iter

to_iter o returns an internal iterator, like in the library Iter.

  • since 2.8
val pp : 'a printer -> 'a t printer

pp ppf o pretty-prints option o using ppf.

\ No newline at end of file +CCOpt (containers.CCOpt)

Module CCOpt

Previous Option module

  • deprecated

    use `CCOption` instead.

include module type of CCOption
type +'a t = 'a option
val map : ('a -> 'b) -> 'a t -> 'b t

map f o applies the function f to the element inside o, if any.

val map_or : default:'b -> ('a -> 'b) -> 'a t -> 'b

map_or ~default f o is f x if o = Some x, default otherwise.

  • since 0.16
val map_lazy : (unit -> 'b) -> ('a -> 'b) -> 'a t -> 'b

map_lazy default_fn f o if f o if o = Some x, default_fn () otherwise.

  • since 1.2
val is_some : _ t -> bool

is_some (Some x) returns true otherwise it returns false.

val is_none : _ t -> bool

is_none None returns true otherwise it returns false.

  • since 0.11
val compare : ('a -> 'a -> int) -> 'a t -> 'a t -> int

compare comp o1 o2 compares two options o1 and o2, using custom comparators comp for the value. None is always assumed to be less than Some _.

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

equal p o1 o2 tests for equality between option types o1 and o2, using a custom equality predicate p.

val return : 'a -> 'a t

return x is a monadic return, that is return x = Some x.

val some : 'a -> 'a t

Alias to return.

  • since 3.5
val none : 'a t

Alias to None.

  • since 3.5
val flat_map : ('a -> 'b t) -> 'a t -> 'b t

flat_map f o is equivalent to map followed by flatten. Flip version of >>=.

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

bind o f is f v if o is Some v, None otherwise. Monadic bind.

  • since 3.0
val map2 : ('a -> 'b -> 'c) -> 'a t -> 'b t -> 'c t

map2 f o1 o2 maps 'a option and 'b option to a 'c option using f.

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

iter f o applies f to o. Iterate on 0 or 1 element.

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

fold f init o is f init x if o is Some x, or init if o is None. Fold on 0 or 1 element.

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

filter f o returns Some x if o is Some x and f x is true, or None if f x is false or if o is None. Filter on 0 or 1 element.

  • since 0.5
val if_ : ('a -> bool) -> 'a -> 'a option

if_ f x is Some x if f x, None otherwise.

  • since 0.17
val exists : ('a -> bool) -> 'a t -> bool

exists f o returns true iff there exists an element for which the provided function f evaluates to true.

  • since 0.17
val for_all : ('a -> bool) -> 'a t -> bool

for_all f o returns true iff the provided function f evaluates to true for all elements.

  • since 0.17
val get_or : default:'a -> 'a t -> 'a

get_or ~default o extracts the value from o, or returns default if o is None.

  • since 0.18
val value : 'a t -> default:'a -> 'a

value o ~default is similar to the Stdlib's Option.value and to get_or.

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

get_exn o returns x if o is Some x or fails if o is None.

  • raises Invalid_argument

    if the option is None.

  • deprecated

    use get_exn_or instead

val get_exn_or : string -> 'a t -> 'a

get_exn_or msg o returns x if o is Some x or fails with Invalid_argument msg if o is None.

  • raises Invalid_argument

    if the option is None.

  • since 3.4
val get_lazy : (unit -> 'a) -> 'a t -> 'a

get_lazy default_fn o unwraps o, but if o is None it returns default_fn () instead.

  • since 0.6.1
val sequence_l : 'a t list -> 'a list t

sequence_l [x1; x2; …; xn] returns Some [y1; y2; …; yn] if every xi is Some yi. Otherwise, if the list contains at least one None, the result is None.

val wrap : ?handler:(exn -> bool) -> ('a -> 'b) -> 'a -> 'b option

wrap ?handler f x calls f x and returns Some y if f x = y. If f x raises any exception, the result is None. This can be useful to wrap functions such as Map.S.find.

  • parameter handler

    the exception handler, which returns true if the exception is to be caught.

val wrap2 : ?handler:(exn -> bool) -> ('a -> 'b -> 'c) -> 'a -> 'b -> 'c option

wrap2 ?handler f x y is similar to wrap but for binary functions.

Applicative

val pure : 'a -> 'a t

pure x is an alias to return.

Alternatives

val or_ : else_:'a t -> 'a t -> 'a t

or_ ~else_ o is o if o is Some _, else_ if o is None.

  • since 1.2
val or_lazy : else_:(unit -> 'a t) -> 'a t -> 'a t

or_lazy ~else_ o is o if o is Some _, else_ () if o is None.

  • since 1.2
val choice : 'a t list -> 'a t

choice lo returns the first non-None element of the list lo, or None.

val flatten : 'a t t -> 'a t

flatten oo transforms Some x into x.

  • since 2.2
val return_if : bool -> 'a -> 'a t

return_if b x applies Some or None depending on the boolean b. More precisely, return_if false x is None, and return_if true x is Some x.

  • since 2.2

Infix Operators

  • since 0.16
module Infix : sig ... end
include module type of Infix
val (>|=) : 'a t -> ('a -> 'b) -> 'b t

o >|= f is map f o.

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

o >>= f is the monadic bind.

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

f <*> o returns Some (f x) if o is Some x and None if o is None.

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

f <$> o is like map f o.

val (<+>) : 'a t -> 'a t -> 'a t

o1 <+> o2 is o1 if o1 is Some _, o2 if o1 is None.

Let operators on OCaml >= 4.08.0, nothing otherwise

  • since 2.8
val let+ : 'a option -> ('a -> 'b) -> 'b option
val and+ : 'a option -> 'b option -> ('a * 'b) option
val let* : 'a option -> ('a -> 'b option) -> 'b option
val and* : 'a option -> 'b option -> ('a * 'b) option

Conversion and IO

val to_list : 'a t -> 'a list

to_list o returns [x] if o is Some x or the empty list [] if o is None.

val of_list : 'a list -> 'a t

of_list l returns Some x (x being the head of the list l), or None if l is the empty list.

val to_result : 'e -> 'a t -> ('a'e) Stdlib.result

to_result e o returns Ok x if o is Some x, or Error e if o is None.

  • since 1.2
val to_result_lazy : (unit -> 'e) -> 'a t -> ('a'e) Stdlib.result

to_result_lazy f o returns Ok x if o is Some x or Error f if o is None.

  • since 1.2
val of_result : ('a_) Stdlib.result -> 'a t

of_result result returns an option from a result.

  • since 1.2
type 'a iter = ('a -> unit) -> unit
type 'a gen = unit -> 'a option
type 'a printer = Stdlib.Format.formatter -> 'a -> unit
type 'a random_gen = Stdlib.Random.State.t -> 'a
val random : 'a random_gen -> 'a t random_gen
val choice_iter : 'a t iter -> 'a t

choice_iter iter is similar to choice, but works on iter. It returns the first Some x occurring in iter, or None otherwise.

  • since 3.0
val choice_seq : 'a t Stdlib.Seq.t -> 'a t

choice_seq seq works on Seq.t. It returns the first Some x occurring in seq, or None otherwise.

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

to_gen o is o as a gen. Some x is the singleton gen containing x and None is the empty gen.

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

to_seq o is o as a sequence Seq.t. Some x is the singleton sequence containing x and None is the empty sequence. Same as Stdlib.Option.to_seq Renamed from to_std_seq since 3.0.

  • since 3.0
val to_iter : 'a t -> 'a iter

to_iter o returns an internal iterator, like in the library Iter.

  • since 2.8
val pp : 'a printer -> 'a t printer

pp ppf o pretty-prints option o using ppf.

\ No newline at end of file diff --git a/dev/containers/CCOption/Infix/index.html b/dev/containers/CCOption/Infix/index.html index ae4980c9..6ee0dd9b 100644 --- a/dev/containers/CCOption/Infix/index.html +++ b/dev/containers/CCOption/Infix/index.html @@ -1,2 +1,2 @@ -Infix (containers.CCOption.Infix)

Module CCOption.Infix

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

o >|= f is map f o.

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

o >>= f is the monadic bind.

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

f <*> o returns Some (f x) if o is Some x and None if o is None.

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

f <$> o is like map f o.

val (<+>) : 'a t -> 'a t -> 'a t

o1 <+> o2 is o1 if o1 is Some _, o2 if o1 is None.

Let operators on OCaml >= 4.08.0, nothing otherwise

  • since 2.8
include CCShimsMkLet_.S with type 'a t_let := 'a option
val let+ : 'a option -> ('a -> 'b) -> 'b option
val and+ : 'a option -> 'b option -> ('a * 'b) option
val let* : 'a option -> ('a -> 'b option) -> 'b option
val and* : 'a option -> 'b option -> ('a * 'b) option
\ No newline at end of file +Infix (containers.CCOption.Infix)

Module CCOption.Infix

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

o >|= f is map f o.

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

o >>= f is the monadic bind.

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

f <*> o returns Some (f x) if o is Some x and None if o is None.

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

f <$> o is like map f o.

val (<+>) : 'a t -> 'a t -> 'a t

o1 <+> o2 is o1 if o1 is Some _, o2 if o1 is None.

Let operators on OCaml >= 4.08.0, nothing otherwise

  • since 2.8
val let+ : 'a option -> ('a -> 'b) -> 'b option
val and+ : 'a option -> 'b option -> ('a * 'b) option
val let* : 'a option -> ('a -> 'b option) -> 'b option
val and* : 'a option -> 'b option -> ('a * 'b) option
\ No newline at end of file diff --git a/dev/containers/CCOption/index.html b/dev/containers/CCOption/index.html index 216f5292..ef2d37b9 100644 --- a/dev/containers/CCOption/index.html +++ b/dev/containers/CCOption/index.html @@ -1,2 +1,2 @@ -CCOption (containers.CCOption)

Module CCOption

Basic operations on the option type.

This module replaces `CCOpt`.

  • since 3.6
type +'a t = 'a option
val map : ('a -> 'b) -> 'a t -> 'b t

map f o applies the function f to the element inside o, if any.

val map_or : default:'b -> ('a -> 'b) -> 'a t -> 'b

map_or ~default f o is f x if o = Some x, default otherwise.

  • since 0.16
val map_lazy : (unit -> 'b) -> ('a -> 'b) -> 'a t -> 'b

map_lazy default_fn f o if f o if o = Some x, default_fn () otherwise.

  • since 1.2
val is_some : _ t -> bool

is_some (Some x) returns true otherwise it returns false.

val is_none : _ t -> bool

is_none None returns true otherwise it returns false.

  • since 0.11
val compare : ('a -> 'a -> int) -> 'a t -> 'a t -> int

compare comp o1 o2 compares two options o1 and o2, using custom comparators comp for the value. None is always assumed to be less than Some _.

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

equal p o1 o2 tests for equality between option types o1 and o2, using a custom equality predicate p.

val return : 'a -> 'a t

return x is a monadic return, that is return x = Some x.

val some : 'a -> 'a t

Alias to return.

  • since 3.5
val none : 'a t

Alias to None.

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

o >|= f is the infix version of map.

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

flat_map f o is equivalent to map followed by flatten. Flip version of >>=.

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

bind o f is f v if o is Some v, None otherwise. Monadic bind.

  • since 3.0
val (>>=) : 'a t -> ('a -> 'b t) -> 'b t

o >>= f is the infix version of bind.

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

map2 f o1 o2 maps 'a option and 'b option to a 'c option using f.

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

iter f o applies f to o. Iterate on 0 or 1 element.

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

fold f init o is f init x if o is Some x, or init if o is None. Fold on 0 or 1 element.

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

filter f o returns Some x if o is Some x and f x is true, or None if f x is false or if o is None. Filter on 0 or 1 element.

  • since 0.5
val if_ : ('a -> bool) -> 'a -> 'a option

if_ f x is Some x if f x, None otherwise.

  • since 0.17
val exists : ('a -> bool) -> 'a t -> bool

exists f o returns true iff there exists an element for which the provided function f evaluates to true.

  • since 0.17
val for_all : ('a -> bool) -> 'a t -> bool

for_all f o returns true iff the provided function f evaluates to true for all elements.

  • since 0.17
val get_or : default:'a -> 'a t -> 'a

get_or ~default o extracts the value from o, or returns default if o is None.

  • since 0.18
val value : 'a t -> default:'a -> 'a

value o ~default is similar to the Stdlib's Option.value and to get_or.

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

get_exn o returns x if o is Some x or fails if o is None.

  • raises Invalid_argument

    if the option is None.

  • deprecated

    use get_exn_or instead

val get_exn_or : string -> 'a t -> 'a

get_exn_or msg o returns x if o is Some x or fails with Invalid_argument msg if o is None.

  • raises Invalid_argument

    if the option is None.

  • since 3.4
val get_lazy : (unit -> 'a) -> 'a t -> 'a

get_lazy default_fn o unwraps o, but if o is None it returns default_fn () instead.

  • since 0.6.1
val sequence_l : 'a t list -> 'a list t

sequence_l [x1; x2; …; xn] returns Some [y1; y2; …; yn] if every xi is Some yi. Otherwise, if the list contains at least one None, the result is None.

val wrap : ?handler:(exn -> bool) -> ('a -> 'b) -> 'a -> 'b option

wrap ?handler f x calls f x and returns Some y if f x = y. If f x raises any exception, the result is None. This can be useful to wrap functions such as Map.S.find.

  • parameter handler

    the exception handler, which returns true if the exception is to be caught.

val wrap2 : ?handler:(exn -> bool) -> ('a -> 'b -> 'c) -> 'a -> 'b -> 'c option

wrap2 ?handler f x y is similar to wrap but for binary functions.

Applicative

val pure : 'a -> 'a t

pure x is an alias to return.

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

f <*> (Some x) returns Some (f x) and f <*> None returns None.

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

f <$> o is like map f o.

Alternatives

val or_ : else_:'a t -> 'a t -> 'a t

or_ ~else_ o is o if o is Some _, else_ if o is None.

  • since 1.2
val or_lazy : else_:(unit -> 'a t) -> 'a t -> 'a t

or_lazy ~else_ o is o if o is Some _, else_ () if o is None.

  • since 1.2
val (<+>) : 'a t -> 'a t -> 'a t

o1 <+> o2 is o1 if o1 is Some _, o2 if o1 is None.

val choice : 'a t list -> 'a t

choice lo returns the first non-None element of the list lo, or None.

val flatten : 'a t t -> 'a t

flatten oo transforms Some x into x.

  • since 2.2
val return_if : bool -> 'a -> 'a t

return_if b x applies Some or None depending on the boolean b. More precisely, return_if false x is None, and return_if true x is Some x.

  • since 2.2

Infix Operators

  • 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 option
val let+ : 'a option -> ('a -> 'b) -> 'b option
val and+ : 'a option -> 'b option -> ('a * 'b) option
val let* : 'a option -> ('a -> 'b option) -> 'b option
val and* : 'a option -> 'b option -> ('a * 'b) option

Conversion and IO

val to_list : 'a t -> 'a list

to_list o returns [x] if o is Some x or the empty list [] if o is None.

val of_list : 'a list -> 'a t

of_list l returns Some x (x being the head of the list l), or None if l is the empty list.

val to_result : 'e -> 'a t -> ('a'e) Stdlib.result

to_result e o returns Ok x if o is Some x, or Error e if o is None.

  • since 1.2
val to_result_lazy : (unit -> 'e) -> 'a t -> ('a'e) Stdlib.result

to_result_lazy f o returns Ok x if o is Some x or Error f if o is None.

  • since 1.2
val of_result : ('a_) Stdlib.result -> 'a t

of_result result returns an option from a result.

  • since 1.2
type 'a iter = ('a -> unit) -> unit
type 'a gen = unit -> 'a option
type 'a printer = Stdlib.Format.formatter -> 'a -> unit
type 'a random_gen = Stdlib.Random.State.t -> 'a
val random : 'a random_gen -> 'a t random_gen
val choice_iter : 'a t iter -> 'a t

choice_iter iter is similar to choice, but works on iter. It returns the first Some x occurring in iter, or None otherwise.

  • since 3.0
val choice_seq : 'a t Stdlib.Seq.t -> 'a t

choice_seq seq works on Seq.t. It returns the first Some x occurring in seq, or None otherwise.

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

to_gen o is o as a gen. Some x is the singleton gen containing x and None is the empty gen.

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

to_seq o is o as a sequence Seq.t. Some x is the singleton sequence containing x and None is the empty sequence. Same as Stdlib.Option.to_seq Renamed from to_std_seq since 3.0.

  • since 3.0
val to_iter : 'a t -> 'a iter

to_iter o returns an internal iterator, like in the library Iter.

  • since 2.8
val pp : 'a printer -> 'a t printer

pp ppf o pretty-prints option o using ppf.

\ No newline at end of file +CCOption (containers.CCOption)

Module CCOption

Basic operations on the option type.

This module replaces `CCOpt`.

  • since 3.6
type +'a t = 'a option
val map : ('a -> 'b) -> 'a t -> 'b t

map f o applies the function f to the element inside o, if any.

val map_or : default:'b -> ('a -> 'b) -> 'a t -> 'b

map_or ~default f o is f x if o = Some x, default otherwise.

  • since 0.16
val map_lazy : (unit -> 'b) -> ('a -> 'b) -> 'a t -> 'b

map_lazy default_fn f o if f o if o = Some x, default_fn () otherwise.

  • since 1.2
val is_some : _ t -> bool

is_some (Some x) returns true otherwise it returns false.

val is_none : _ t -> bool

is_none None returns true otherwise it returns false.

  • since 0.11
val compare : ('a -> 'a -> int) -> 'a t -> 'a t -> int

compare comp o1 o2 compares two options o1 and o2, using custom comparators comp for the value. None is always assumed to be less than Some _.

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

equal p o1 o2 tests for equality between option types o1 and o2, using a custom equality predicate p.

val return : 'a -> 'a t

return x is a monadic return, that is return x = Some x.

val some : 'a -> 'a t

Alias to return.

  • since 3.5
val none : 'a t

Alias to None.

  • since 3.5
val flat_map : ('a -> 'b t) -> 'a t -> 'b t

flat_map f o is equivalent to map followed by flatten. Flip version of >>=.

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

bind o f is f v if o is Some v, None otherwise. Monadic bind.

  • since 3.0
val map2 : ('a -> 'b -> 'c) -> 'a t -> 'b t -> 'c t

map2 f o1 o2 maps 'a option and 'b option to a 'c option using f.

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

iter f o applies f to o. Iterate on 0 or 1 element.

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

fold f init o is f init x if o is Some x, or init if o is None. Fold on 0 or 1 element.

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

filter f o returns Some x if o is Some x and f x is true, or None if f x is false or if o is None. Filter on 0 or 1 element.

  • since 0.5
val if_ : ('a -> bool) -> 'a -> 'a option

if_ f x is Some x if f x, None otherwise.

  • since 0.17
val exists : ('a -> bool) -> 'a t -> bool

exists f o returns true iff there exists an element for which the provided function f evaluates to true.

  • since 0.17
val for_all : ('a -> bool) -> 'a t -> bool

for_all f o returns true iff the provided function f evaluates to true for all elements.

  • since 0.17
val get_or : default:'a -> 'a t -> 'a

get_or ~default o extracts the value from o, or returns default if o is None.

  • since 0.18
val value : 'a t -> default:'a -> 'a

value o ~default is similar to the Stdlib's Option.value and to get_or.

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

get_exn o returns x if o is Some x or fails if o is None.

  • raises Invalid_argument

    if the option is None.

  • deprecated

    use get_exn_or instead

val get_exn_or : string -> 'a t -> 'a

get_exn_or msg o returns x if o is Some x or fails with Invalid_argument msg if o is None.

  • raises Invalid_argument

    if the option is None.

  • since 3.4
val get_lazy : (unit -> 'a) -> 'a t -> 'a

get_lazy default_fn o unwraps o, but if o is None it returns default_fn () instead.

  • since 0.6.1
val sequence_l : 'a t list -> 'a list t

sequence_l [x1; x2; …; xn] returns Some [y1; y2; …; yn] if every xi is Some yi. Otherwise, if the list contains at least one None, the result is None.

val wrap : ?handler:(exn -> bool) -> ('a -> 'b) -> 'a -> 'b option

wrap ?handler f x calls f x and returns Some y if f x = y. If f x raises any exception, the result is None. This can be useful to wrap functions such as Map.S.find.

  • parameter handler

    the exception handler, which returns true if the exception is to be caught.

val wrap2 : ?handler:(exn -> bool) -> ('a -> 'b -> 'c) -> 'a -> 'b -> 'c option

wrap2 ?handler f x y is similar to wrap but for binary functions.

Applicative

val pure : 'a -> 'a t

pure x is an alias to return.

Alternatives

val or_ : else_:'a t -> 'a t -> 'a t

or_ ~else_ o is o if o is Some _, else_ if o is None.

  • since 1.2
val or_lazy : else_:(unit -> 'a t) -> 'a t -> 'a t

or_lazy ~else_ o is o if o is Some _, else_ () if o is None.

  • since 1.2
val choice : 'a t list -> 'a t

choice lo returns the first non-None element of the list lo, or None.

val flatten : 'a t t -> 'a t

flatten oo transforms Some x into x.

  • since 2.2
val return_if : bool -> 'a -> 'a t

return_if b x applies Some or None depending on the boolean b. More precisely, return_if false x is None, and return_if true x is Some x.

  • since 2.2

Infix Operators

  • since 0.16
module Infix : sig ... end
include module type of Infix
val (>|=) : 'a t -> ('a -> 'b) -> 'b t

o >|= f is map f o.

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

o >>= f is the monadic bind.

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

f <*> o returns Some (f x) if o is Some x and None if o is None.

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

f <$> o is like map f o.

val (<+>) : 'a t -> 'a t -> 'a t

o1 <+> o2 is o1 if o1 is Some _, o2 if o1 is None.

Let operators on OCaml >= 4.08.0, nothing otherwise

  • since 2.8
val let+ : 'a option -> ('a -> 'b) -> 'b option
val and+ : 'a option -> 'b option -> ('a * 'b) option
val let* : 'a option -> ('a -> 'b option) -> 'b option
val and* : 'a option -> 'b option -> ('a * 'b) option

Conversion and IO

val to_list : 'a t -> 'a list

to_list o returns [x] if o is Some x or the empty list [] if o is None.

val of_list : 'a list -> 'a t

of_list l returns Some x (x being the head of the list l), or None if l is the empty list.

val to_result : 'e -> 'a t -> ('a'e) Stdlib.result

to_result e o returns Ok x if o is Some x, or Error e if o is None.

  • since 1.2
val to_result_lazy : (unit -> 'e) -> 'a t -> ('a'e) Stdlib.result

to_result_lazy f o returns Ok x if o is Some x or Error f if o is None.

  • since 1.2
val of_result : ('a_) Stdlib.result -> 'a t

of_result result returns an option from a result.

  • since 1.2
type 'a iter = ('a -> unit) -> unit
type 'a gen = unit -> 'a option
type 'a printer = Stdlib.Format.formatter -> 'a -> unit
type 'a random_gen = Stdlib.Random.State.t -> 'a
val random : 'a random_gen -> 'a t random_gen
val choice_iter : 'a t iter -> 'a t

choice_iter iter is similar to choice, but works on iter. It returns the first Some x occurring in iter, or None otherwise.

  • since 3.0
val choice_seq : 'a t Stdlib.Seq.t -> 'a t

choice_seq seq works on Seq.t. It returns the first Some x occurring in seq, or None otherwise.

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

to_gen o is o as a gen. Some x is the singleton gen containing x and None is the empty gen.

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

to_seq o is o as a sequence Seq.t. Some x is the singleton sequence containing x and None is the empty sequence. Same as Stdlib.Option.to_seq Renamed from to_std_seq since 3.0.

  • since 3.0
val to_iter : 'a t -> 'a iter

to_iter o returns an internal iterator, like in the library Iter.

  • since 2.8
val pp : 'a printer -> 'a t printer

pp ppf o pretty-prints option o using ppf.

\ No newline at end of file diff --git a/dev/containers/CCParse/Infix/index.html b/dev/containers/CCParse/Infix/index.html index 87f0ee32..912dae26 100644 --- a/dev/containers/CCParse/Infix/index.html +++ b/dev/containers/CCParse/Infix/index.html @@ -1,2 +1,2 @@ -Infix (containers.CCParse.Infix)

Module CCParse.Infix

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

Alias to map. p >|= f parses an item x using p, and returns f x.

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

Alias to bind. p >>= f results in a new parser which behaves as p then, in case of success, applies f to the result.

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

Applicative.

val (<*) : 'a t -> _ t -> 'a t

a <* b parses a into x, parses b and ignores its result, and returns x.

val (*>) : _ t -> 'a t -> 'a t

a *> b parses a, then parses b into x, and returns x. The result of a is ignored.

val (<|>) : 'a t -> 'a t -> 'a t

Alias to or_.

a <|> b tries to parse a, and if a fails without consuming any input, backtracks and tries to parse b, otherwise it fails as a. See try_ to ensure a does not consume anything (but it is best to avoid wrapping large parsers with try_).

val (<?>) : 'a t -> string -> 'a t

a <?> msg behaves like a, but if a fails, a <?> msg fails with msg instead. Useful as the last choice in a series of <|>. For example: a <|> b <|> c <?> "expected one of a, b, c".

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

Alias to both. a ||| b parses a, then b, then returns the pair of their results.

  • since 3.6

Let operators on OCaml >= 4.08.0, nothing otherwise

  • since 2.8
include CCShimsMkLet_.S with type 'a t_let := 'a t
val let+ : 'a t -> ('a -> 'b) -> 'b t
val and+ : 'a t -> 'b t -> ('a * 'b) t
val let* : 'a t -> ('a -> 'b t) -> 'b t
val and* : 'a t -> 'b t -> ('a * 'b) t
\ No newline at end of file +Infix (containers.CCParse.Infix)

Module CCParse.Infix

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

Alias to map. p >|= f parses an item x using p, and returns f x.

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

Alias to bind. p >>= f results in a new parser which behaves as p then, in case of success, applies f to the result.

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

Applicative.

val (<*) : 'a t -> _ t -> 'a t

a <* b parses a into x, parses b and ignores its result, and returns x.

val (*>) : _ t -> 'a t -> 'a t

a *> b parses a, then parses b into x, and returns x. The result of a is ignored.

val (<|>) : 'a t -> 'a t -> 'a t

Alias to or_.

a <|> b tries to parse a, and if a fails without consuming any input, backtracks and tries to parse b, otherwise it fails as a. See try_ to ensure a does not consume anything (but it is best to avoid wrapping large parsers with try_).

val (<?>) : 'a t -> string -> 'a t

a <?> msg behaves like a, but if a fails, a <?> msg fails with msg instead. Useful as the last choice in a series of <|>. For example: a <|> b <|> c <?> "expected one of a, b, c".

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

Alias to both. a ||| b parses a, then b, then returns the pair of their results.

  • since 3.6

Let operators on OCaml >= 4.08.0, nothing otherwise

  • since 2.8
val let+ : 'a t -> ('a -> 'b) -> 'b t
val and+ : 'a t -> 'b t -> ('a * 'b) t
val let* : 'a t -> ('a -> 'b t) -> 'b t
val and* : 'a t -> 'b t -> ('a * 'b) t
\ No newline at end of file diff --git a/dev/containers/CCParse/index.html b/dev/containers/CCParse/index.html index 7c9b2306..e9ebbe66 100644 --- a/dev/containers/CCParse/index.html +++ b/dev/containers/CCParse/index.html @@ -26,4 +26,4 @@ let l' = CCParse.parse_string_exn p l_printed;; assert (l=l');;

Stability guarantees

Some functions are marked "experimental" and are still subject to change.

type position

A position in the input. Typically it'll point at the beginning of an error location.

module Position : sig ... end
module Error : sig ... end
type +'a or_error = ('aError.t) Stdlib.result

'a or_error is either Ok x for some result x : 'a, or an error Error.t.

See stringify_result and Error.to_string to print the error message.

exception ParseError of Error.t

Input

Combinators

type 'a t

The abstract type of parsers that return a value of type 'a (or fail).

  • raises ParseError

    in case of failure.

  • since 3.6 the type is private.
val return : 'a -> 'a t

Always succeeds, without consuming its input.

val pure : 'a -> 'a t

Synonym to return.

val map : ('a -> 'b) -> 'a t -> 'b t
val map2 : ('a -> 'b -> 'c) -> 'a t -> 'b t -> 'c t
val map3 : ('a -> 'b -> 'c -> 'd) -> 'a t -> 'b t -> 'c t -> 'd t
val bind : ('a -> 'b t) -> 'a t -> 'b t

bind f p results in a new parser which behaves as p then, in case of success, applies f to the result.

  • since 3.6
val ap : ('a -> 'b) t -> 'a t -> 'b t

Applicative.

  • since 3.6
val eoi : unit t

Expect the end of input, fails otherwise.

val empty : unit t

Succeed with ().

  • since 3.6
val fail : string -> 'a t

fail msg fails with the given message. It can trigger a backtrack.

val failf : ('a, unit, string, 'b t) Stdlib.format4 -> 'a

Format.sprintf version of fail.

val fail_lazy : (unit -> string) -> 'a t

Like fail, but only produce an error message on demand.

  • since 3.6
val parsing : string -> 'a t -> 'a t

parsing s p behaves the same as p, with the information that we are parsing s, if p fails. The message s is added to the error, it does not replace it, not does the location change (the error still points to the same location as in p).

val set_error_message : string -> 'a t -> 'a t

set_error_message msg p behaves like p, but if p fails, set_error_message msg p fails with msg instead and at the current position. The internal error message of p is just discarded.

  • since 3.6
val with_pos : 'a t -> ('a * position) t

with_pos p behaves like p, but returns the (starting) position along with p's result.

EXPERIMENTAL

  • since 3.6
val any_char : char t

any_char parses any character. It still fails if the end of input was reached.

  • since 3.6
val any_char_n : int -> string t

any_char_n len parses exactly len characters from the input. Fails if the input doesn't contain at least len chars.

  • since 3.6
val char : char -> char t

char c parses the character c and nothing else.

type slice

A slice of the input, as returned by some combinators such as split_1 or split_list or take.

The idea is that one can use some parsers to cut the input into slices, e.g. split into lines, or split a line into fields (think CSV or TSV). Then a variety of parsers can be used on each slice to extract data from it using recurse.

Slices contain enough information to make it possible for recurse slice p to report failures (if p fails) using locations from the original input, not relative to the slice. Therefore, even after splitting the input into lines using, say, each_line, a failure to parse the 500th line will be reported at line 500 and not at line 1.

EXPERIMENTAL

  • since 3.6
module Slice : sig ... end

Functions on slices.

val recurse : slice -> 'a t -> 'a t

recurse slice p parses the slice (most likely obtained via another combinator, such as split_1 or split_n), using p.

The slice contains a position which is used to relocate error messages to their position in the whole input, not just relative to the slice.

EXPERIMENTAL

  • since 3.6
val set_current_slice : slice -> unit t

set_current_slice slice replaces the parser's state with slice.

EXPERIMENTAL

  • since 3.6
val chars_fold : f:('acc -> char -> [ `Continue of 'acc | `Consume_and_stop of 'acc | `Stop of 'acc | `Fail of string ]) -> 'acc -> ('acc * slice) t

chars_fold f acc0 folds over characters of the input. Each char c is passed, along with the current accumulator, to f; f can either:

  • stop, by returning `Stop acc. In this case the final accumulator acc is returned, and c is not consumed.
  • consume char and stop, by returning `Consume_and_stop acc.
  • fail, by returning `Fail msg. In this case the parser fails with the given message.
  • continue, by returning `Continue acc. The parser continues to the next char with the new accumulator.

This is a generalization of of chars_if that allows one to transform characters on the fly, skip some, handle escape sequences, etc. It can also be useful as a base component for a lexer.

  • returns

    a pair of the final accumular, and the slice matched by the fold.

  • since 3.6
val chars_fold_transduce : f:('acc -> char -> [ `Continue of 'acc | `Yield of 'acc * char | `Consume_and_stop | `Stop | `Fail of string ]) --> 'acc -> ('acc * string) t

Same as char_fold but with the following differences:

  • returns a string along with the accumulator, rather than the slice of all the characters accepted by `Continue _. The string is built from characters returned by `Yield.
  • new case `Yield (acc, c) adds c to the returned string and continues parsing with acc.
  • since 3.6
val take : int -> slice t

take len parses exactly len characters from the input. Fails if the input doesn't contain at least len chars.

  • since 3.6
val take_if : (char -> bool) -> slice t

take_if f takes characters as long as they satisfy the predicate f.

  • since 3.6
val take1_if : ?descr:string -> (char -> bool) -> slice t

take1_if f takes characters as long as they satisfy the predicate f. Fails if no character satisfies f.

  • parameter descr

    describes what kind of character was expected, in case of error

  • since 3.6
val char_if : ?descr:string -> (char -> bool) -> char t

char_if f parses a character c if f c = true. Fails if the next char does not satisfy f.

  • parameter descr

    describes what kind of character was expected, in case of error

val chars_if : (char -> bool) -> string t

chars_if f parses a string of chars that satisfy f. Cannot fail.

val chars1_if : ?descr:string -> (char -> bool) -> string t

Like chars_if, but accepts only non-empty strings. chars1_if p fails if the string accepted by chars_if p is empty. chars1_if p is equivalent to take1_if p >|= Slice.to_string.

  • parameter descr

    describes what kind of character was expected, in case of error

val endline : char t

Parse '\n'.

val space : char t

Tab or space.

val white : char t

Tab or space or newline.

val skip_chars : (char -> bool) -> unit t

Skip 0 or more chars satisfying the predicate.

val skip_space : unit t

Skip ' ' and '\t'.

val skip_white : unit t

Skip ' ' and '\t' and '\n'.

val is_alpha : char -> bool

Is the char a letter?

val is_num : char -> bool

Is the char a digit?

val is_alpha_num : char -> bool

Is the char a letter or a digit?

val is_space : char -> bool

True on ' ' and '\t'.

val is_white : char -> bool

True on ' ' and '\t' and '\n'.

val suspend : (unit -> 'a t) -> 'a t

suspend f is the same as f (), but evaluates f () only when needed.

val string : string -> string t

string s parses exactly the string s, and nothing else.

val exact : string -> string t

Alias to string.

  • since 3.6
val many : 'a t -> 'a list t

many p parses p repeatedly, until p fails, and collects the results into a list.

val optional : _ t -> unit t

optional p tries to parse p, and return () whether it succeeded or failed. Cannot fail itself. It consumes input if p succeeded (as much as p consumed), but consumes not input if p failed.

  • since 3.6
val try_ : 'a t -> 'a t

try_ p is just like p (it used to play a role in backtracking semantics but no more).

  • deprecated

    since 3.6 it can just be removed. See try_opt if you want to detect failure.

val try_opt : 'a t -> 'a option t

try_opt p tries to parse using p, and return Some x if p succeeded with x (and consumes what p consumed). Otherwise it returns None and consumes nothing. This cannot fail.

  • since 3.6
val many_until : until:_ t -> 'a t -> 'a list t

many_until ~until p parses as many p as it can until the until parser successfully returns. If p fails before that then many_until ~until p fails as well. Typically until can be a closing ')' or another termination condition, and what is consumed by until is also consumed by many_until ~until p.

EXPERIMENTAL

  • since 3.6
val try_or : 'a t -> f:('a -> 'b t) -> else_:'b t -> 'b t

try_or p1 ~f ~else_:p2 attempts to parse x using p1, and then becomes f x. If p1 fails, then it becomes p2. This can be useful if f is expensive but only ever works if p1 matches (e.g. after an opening parenthesis or some sort of prefix).

  • since 3.6
val try_or_l : ?msg:string -> ?else_:'a t -> (unit t * 'a t) list -> 'a t

try_or_l ?else_ l tries each pair (test, p) in order. If the n-th test succeeds, then try_or_l l behaves like n-th p, whether p fails or not. If they all fail, and else_ is defined, then it behaves like else_. If all fail, and else_ is None, then it fails as well.

This is a performance optimization compared to (<|>). We commit to a branch if the test succeeds, without backtracking at all.

See lookahead_ignore for a convenient way of writing the test conditions.

  • parameter msg

    error message if all options fail

    EXPERIMENTAL

  • since 3.6
val or_ : 'a t -> 'a t -> 'a t

or_ p1 p2 tries to parse p1, and if it fails, tries p2 from the same position.

  • since 3.6
val both : 'a t -> 'b t -> ('a * 'b) t

both a b parses a, then b, then returns the pair of their results.

  • since 3.6
val many1 : 'a t -> 'a list t

many1 p is like many p excepts it fails if the list is empty (i.e. it needs p to succeed at least once).

val skip : _ t -> unit t

skip p parses zero or more times p and ignores its result. It is eager, meaning it will continue as long as p succeeds. As soon as p fails, skip p stops consuming any input.

val sep : by:_ t -> 'a t -> 'a list t

sep ~by p parses a list of p separated by by.

val sep_until : until:_ t -> by:_ t -> 'a t -> 'a list t

Same as sep but stop when until parses successfully.

  • since 3.6
val sep1 : by:_ t -> 'a t -> 'a list t

sep1 ~by p parses a non empty list of p, separated by by.

val lookahead : 'a t -> 'a t

lookahead p behaves like p, except it doesn't consume any input.

EXPERIMENTAL

  • since 3.6
val lookahead_ignore : 'a t -> unit t

lookahead_ignore p tries to parse input with p, and succeeds if p succeeds. However it doesn't consume any input and returns (), so in effect its only use-case is to detect whether p succeeds, e.g. in try_or_l.

EXPERIMENTAL

  • since 3.6
val fix : ('a t -> 'a t) -> 'a t

Fixpoint combinator.

val line : slice t

Parse a line, '\n' excluded, and position the cursor after the '\n'.

  • since 3.6
val line_str : string t

line_str is line >|= Slice.to_string. It parses the next line and turns the slice into a string. The state points to the character immediately after the '\n' character.

  • since 3.6
val each_line : 'a t -> 'a list t

each_line p runs p on each line of the input. EXPERIMENTAL

  • since 3.6
val split_1 : on_char:char -> (slice * slice option) t

split_1 ~on_char looks for on_char in the input, and returns a pair sl1, sl2, where:

  • sl1 is the slice of the input the precedes the first occurrence of on_char, or the whole input if on_char cannot be found. It does not contain on_char.
  • sl2 is the slice that comes after on_char, or None if on_char couldn't be found. It doesn't contain the first occurrence of on_char (if any).

The parser is now positioned at the end of the input.

EXPERIMENTAL

  • since 3.6
val split_list : on_char:char -> slice list t

split_list ~on_char splits the input on all occurrences of on_char, returning a list of slices.

EXPERIMENTAL

  • since 3.6
val split_list_at_most : on_char:char -> int -> slice list t

split_list_at_most ~on_char n applies split_1 ~on_char at most n times, to get a list of n+1 elements. The last element might contain on_char. This is useful to limit the amount of work done by split_list.

EXPERIMENTAL

  • since 3.6
val split_2 : on_char:char -> (slice * slice) t

split_2 ~on_char splits the input into exactly 2 fields, and fails if the split yields less or more than 2 items. EXPERIMENTAL

  • since 3.6
val split_3 : on_char:char -> (slice * slice * slice) t

See split_2 EXPERIMENTAL

  • since 3.6
val split_4 : on_char:char -> (slice * slice * slice * slice) t

See split_2 EXPERIMENTAL

  • since 3.6
val each_split : on_char:char -> 'a t -> 'a list t

split_list_map ~on_char p uses split_list ~on_char to split the input, then parses each chunk of the input thus obtained using p.

The difference with sep ~by:(char on_char) p is that sep calls p first, and only tries to find on_char after p returns. While it is more flexible, this technique also means p has to be careful not to consume on_char by error.

A useful specialization of this is each_line, which is basically each_split ~on_char:'\n' p.

EXPERIMENTAL

  • since 3.6
val all : slice t

all returns all the unconsumed input as a slice, and consumes it. Use Slice.to_string to turn it into a string.

Note that lookahead all can be used to peek at the rest of the input without consuming anything.

  • since 3.6
val all_str : string t

all_str accepts all the remaining chars and extracts them into a string. Similar to all but with a string.

EXPERIMENTAL

  • since 3.6
val memo : 'a t -> 'a t

Memoize the parser. memo p will behave like p, but when called in a state (read: position in input) it has already processed, memo p returns a result directly. The implementation uses an underlying hashtable. This can be costly in memory, but improve the run time a lot if there is a lot of backtracking involving p.

Do not call memo inside other functions, especially with (>>=), map, etc. being so prevalent. Instead the correct way to use it is in a toplevel definition:

let my_expensive_parser = memo (foo *> bar >>= fun i -> …)

This function is not thread-safe.

val fix_memo : ('a t -> 'a t) -> 'a t

Like fix, but the fixpoint is memoized.

Infix

module Infix : sig ... end
include module type of Infix
val (>|=) : 'a t -> ('a -> 'b) -> 'b t

Alias to map. p >|= f parses an item x using p, and returns f x.

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

Alias to bind. p >>= f results in a new parser which behaves as p then, in case of success, applies f to the result.

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

Applicative.

val (<*) : 'a t -> _ t -> 'a t

a <* b parses a into x, parses b and ignores its result, and returns x.

val (*>) : _ t -> 'a t -> 'a t

a *> b parses a, then parses b into x, and returns x. The result of a is ignored.

val (<|>) : 'a t -> 'a t -> 'a t

Alias to or_.

a <|> b tries to parse a, and if a fails without consuming any input, backtracks and tries to parse b, otherwise it fails as a. See try_ to ensure a does not consume anything (but it is best to avoid wrapping large parsers with try_).

val (<?>) : 'a t -> string -> 'a t

a <?> msg behaves like a, but if a fails, a <?> msg fails with msg instead. Useful as the last choice in a series of <|>. For example: a <|> b <|> c <?> "expected one of a, b, c".

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

Alias to both. a ||| b parses a, then b, then returns the pair of their results.

  • since 3.6

Let operators on OCaml >= 4.08.0, nothing otherwise

  • since 2.8
include CCShimsMkLet_.S with type 'a t_let := 'a t
val let+ : 'a t -> ('a -> 'b) -> 'b t
val and+ : 'a t -> 'b t -> ('a * 'b) t
val let* : 'a t -> ('a -> 'b t) -> 'b t
val and* : 'a t -> 'b t -> ('a * 'b) t

Parse input

val stringify_result : 'a or_error -> ('a, string) Stdlib.result

Turn a Error.t-oriented result into a more basic string result.

  • since 3.6
val parse_string : 'a t -> string -> ('a, string) Stdlib.result

Parse a string using the parser.

val parse_string_e : 'a t -> string -> 'a or_error

Version of parse_string that returns a more detailed error.

val parse_string_exn : 'a t -> string -> 'a
  • raises ParseError

    if it fails.

val parse_file : 'a t -> string -> ('a, string) Stdlib.result

parse_file p filename parses file named filename with p by opening the file and reading it whole.

val parse_file_e : 'a t -> string -> 'a or_error

Version of parse_file that returns a more detailed error.

val parse_file_exn : 'a t -> string -> 'a

Same as parse_file, but

  • raises ParseError

    if it fails.

module U : sig ... end
module Debug_ : sig ... end

Debugging utils. EXPERIMENTAL

\ No newline at end of file +-> 'acc -> ('acc * string) t

Same as char_fold but with the following differences:

val take : int -> slice t

take len parses exactly len characters from the input. Fails if the input doesn't contain at least len chars.

  • since 3.6
val take_if : (char -> bool) -> slice t

take_if f takes characters as long as they satisfy the predicate f.

  • since 3.6
val take1_if : ?descr:string -> (char -> bool) -> slice t

take1_if f takes characters as long as they satisfy the predicate f. Fails if no character satisfies f.

  • parameter descr

    describes what kind of character was expected, in case of error

  • since 3.6
val char_if : ?descr:string -> (char -> bool) -> char t

char_if f parses a character c if f c = true. Fails if the next char does not satisfy f.

  • parameter descr

    describes what kind of character was expected, in case of error

val chars_if : (char -> bool) -> string t

chars_if f parses a string of chars that satisfy f. Cannot fail.

val chars1_if : ?descr:string -> (char -> bool) -> string t

Like chars_if, but accepts only non-empty strings. chars1_if p fails if the string accepted by chars_if p is empty. chars1_if p is equivalent to take1_if p >|= Slice.to_string.

  • parameter descr

    describes what kind of character was expected, in case of error

val endline : char t

Parse '\n'.

val space : char t

Tab or space.

val white : char t

Tab or space or newline.

val skip_chars : (char -> bool) -> unit t

Skip 0 or more chars satisfying the predicate.

val skip_space : unit t

Skip ' ' and '\t'.

val skip_white : unit t

Skip ' ' and '\t' and '\n'.

val is_alpha : char -> bool

Is the char a letter?

val is_num : char -> bool

Is the char a digit?

val is_alpha_num : char -> bool

Is the char a letter or a digit?

val is_space : char -> bool

True on ' ' and '\t'.

val is_white : char -> bool

True on ' ' and '\t' and '\n'.

val suspend : (unit -> 'a t) -> 'a t

suspend f is the same as f (), but evaluates f () only when needed.

val string : string -> string t

string s parses exactly the string s, and nothing else.

val exact : string -> string t

Alias to string.

  • since 3.6
val many : 'a t -> 'a list t

many p parses p repeatedly, until p fails, and collects the results into a list.

val optional : _ t -> unit t

optional p tries to parse p, and return () whether it succeeded or failed. Cannot fail itself. It consumes input if p succeeded (as much as p consumed), but consumes not input if p failed.

  • since 3.6
val try_ : 'a t -> 'a t

try_ p is just like p (it used to play a role in backtracking semantics but no more).

  • deprecated

    since 3.6 it can just be removed. See try_opt if you want to detect failure.

val try_opt : 'a t -> 'a option t

try_opt p tries to parse using p, and return Some x if p succeeded with x (and consumes what p consumed). Otherwise it returns None and consumes nothing. This cannot fail.

  • since 3.6
val many_until : until:_ t -> 'a t -> 'a list t

many_until ~until p parses as many p as it can until the until parser successfully returns. If p fails before that then many_until ~until p fails as well. Typically until can be a closing ')' or another termination condition, and what is consumed by until is also consumed by many_until ~until p.

EXPERIMENTAL

  • since 3.6
val try_or : 'a t -> f:('a -> 'b t) -> else_:'b t -> 'b t

try_or p1 ~f ~else_:p2 attempts to parse x using p1, and then becomes f x. If p1 fails, then it becomes p2. This can be useful if f is expensive but only ever works if p1 matches (e.g. after an opening parenthesis or some sort of prefix).

  • since 3.6
val try_or_l : ?msg:string -> ?else_:'a t -> (unit t * 'a t) list -> 'a t

try_or_l ?else_ l tries each pair (test, p) in order. If the n-th test succeeds, then try_or_l l behaves like n-th p, whether p fails or not. If they all fail, and else_ is defined, then it behaves like else_. If all fail, and else_ is None, then it fails as well.

This is a performance optimization compared to (<|>). We commit to a branch if the test succeeds, without backtracking at all.

See lookahead_ignore for a convenient way of writing the test conditions.

  • parameter msg

    error message if all options fail

    EXPERIMENTAL

  • since 3.6
val or_ : 'a t -> 'a t -> 'a t

or_ p1 p2 tries to parse p1, and if it fails, tries p2 from the same position.

  • since 3.6
val both : 'a t -> 'b t -> ('a * 'b) t

both a b parses a, then b, then returns the pair of their results.

  • since 3.6
val many1 : 'a t -> 'a list t

many1 p is like many p excepts it fails if the list is empty (i.e. it needs p to succeed at least once).

val skip : _ t -> unit t

skip p parses zero or more times p and ignores its result. It is eager, meaning it will continue as long as p succeeds. As soon as p fails, skip p stops consuming any input.

val sep : by:_ t -> 'a t -> 'a list t

sep ~by p parses a list of p separated by by.

val sep_until : until:_ t -> by:_ t -> 'a t -> 'a list t

Same as sep but stop when until parses successfully.

  • since 3.6
val sep1 : by:_ t -> 'a t -> 'a list t

sep1 ~by p parses a non empty list of p, separated by by.

val lookahead : 'a t -> 'a t

lookahead p behaves like p, except it doesn't consume any input.

EXPERIMENTAL

  • since 3.6
val lookahead_ignore : 'a t -> unit t

lookahead_ignore p tries to parse input with p, and succeeds if p succeeds. However it doesn't consume any input and returns (), so in effect its only use-case is to detect whether p succeeds, e.g. in try_or_l.

EXPERIMENTAL

  • since 3.6
val fix : ('a t -> 'a t) -> 'a t

Fixpoint combinator.

val line : slice t

Parse a line, '\n' excluded, and position the cursor after the '\n'.

  • since 3.6
val line_str : string t

line_str is line >|= Slice.to_string. It parses the next line and turns the slice into a string. The state points to the character immediately after the '\n' character.

  • since 3.6
val each_line : 'a t -> 'a list t

each_line p runs p on each line of the input. EXPERIMENTAL

  • since 3.6
val split_1 : on_char:char -> (slice * slice option) t

split_1 ~on_char looks for on_char in the input, and returns a pair sl1, sl2, where:

  • sl1 is the slice of the input the precedes the first occurrence of on_char, or the whole input if on_char cannot be found. It does not contain on_char.
  • sl2 is the slice that comes after on_char, or None if on_char couldn't be found. It doesn't contain the first occurrence of on_char (if any).

The parser is now positioned at the end of the input.

EXPERIMENTAL

  • since 3.6
val split_list : on_char:char -> slice list t

split_list ~on_char splits the input on all occurrences of on_char, returning a list of slices.

EXPERIMENTAL

  • since 3.6
val split_list_at_most : on_char:char -> int -> slice list t

split_list_at_most ~on_char n applies split_1 ~on_char at most n times, to get a list of n+1 elements. The last element might contain on_char. This is useful to limit the amount of work done by split_list.

EXPERIMENTAL

  • since 3.6
val split_2 : on_char:char -> (slice * slice) t

split_2 ~on_char splits the input into exactly 2 fields, and fails if the split yields less or more than 2 items. EXPERIMENTAL

  • since 3.6
val split_3 : on_char:char -> (slice * slice * slice) t

See split_2 EXPERIMENTAL

  • since 3.6
val split_4 : on_char:char -> (slice * slice * slice * slice) t

See split_2 EXPERIMENTAL

  • since 3.6
val each_split : on_char:char -> 'a t -> 'a list t

split_list_map ~on_char p uses split_list ~on_char to split the input, then parses each chunk of the input thus obtained using p.

The difference with sep ~by:(char on_char) p is that sep calls p first, and only tries to find on_char after p returns. While it is more flexible, this technique also means p has to be careful not to consume on_char by error.

A useful specialization of this is each_line, which is basically each_split ~on_char:'\n' p.

EXPERIMENTAL

  • since 3.6
val all : slice t

all returns all the unconsumed input as a slice, and consumes it. Use Slice.to_string to turn it into a string.

Note that lookahead all can be used to peek at the rest of the input without consuming anything.

  • since 3.6
val all_str : string t

all_str accepts all the remaining chars and extracts them into a string. Similar to all but with a string.

EXPERIMENTAL

  • since 3.6
val memo : 'a t -> 'a t

Memoize the parser. memo p will behave like p, but when called in a state (read: position in input) it has already processed, memo p returns a result directly. The implementation uses an underlying hashtable. This can be costly in memory, but improve the run time a lot if there is a lot of backtracking involving p.

Do not call memo inside other functions, especially with (>>=), map, etc. being so prevalent. Instead the correct way to use it is in a toplevel definition:

let my_expensive_parser = memo (foo *> bar >>= fun i -> …)

This function is not thread-safe.

val fix_memo : ('a t -> 'a t) -> 'a t

Like fix, but the fixpoint is memoized.

Infix

module Infix : sig ... end
include module type of Infix
val (>|=) : 'a t -> ('a -> 'b) -> 'b t

Alias to map. p >|= f parses an item x using p, and returns f x.

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

Alias to bind. p >>= f results in a new parser which behaves as p then, in case of success, applies f to the result.

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

Applicative.

val (<*) : 'a t -> _ t -> 'a t

a <* b parses a into x, parses b and ignores its result, and returns x.

val (*>) : _ t -> 'a t -> 'a t

a *> b parses a, then parses b into x, and returns x. The result of a is ignored.

val (<|>) : 'a t -> 'a t -> 'a t

Alias to or_.

a <|> b tries to parse a, and if a fails without consuming any input, backtracks and tries to parse b, otherwise it fails as a. See try_ to ensure a does not consume anything (but it is best to avoid wrapping large parsers with try_).

val (<?>) : 'a t -> string -> 'a t

a <?> msg behaves like a, but if a fails, a <?> msg fails with msg instead. Useful as the last choice in a series of <|>. For example: a <|> b <|> c <?> "expected one of a, b, c".

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

Alias to both. a ||| b parses a, then b, then returns the pair of their results.

  • since 3.6

Let operators on OCaml >= 4.08.0, nothing otherwise

  • since 2.8
val let+ : 'a t -> ('a -> 'b) -> 'b t
val and+ : 'a t -> 'b t -> ('a * 'b) t
val let* : 'a t -> ('a -> 'b t) -> 'b t
val and* : 'a t -> 'b t -> ('a * 'b) t

Parse input

val stringify_result : 'a or_error -> ('a, string) Stdlib.result

Turn a Error.t-oriented result into a more basic string result.

  • since 3.6
val parse_string : 'a t -> string -> ('a, string) Stdlib.result

Parse a string using the parser.

val parse_string_e : 'a t -> string -> 'a or_error

Version of parse_string that returns a more detailed error.

val parse_string_exn : 'a t -> string -> 'a
  • raises ParseError

    if it fails.

val parse_file : 'a t -> string -> ('a, string) Stdlib.result

parse_file p filename parses file named filename with p by opening the file and reading it whole.

val parse_file_e : 'a t -> string -> 'a or_error

Version of parse_file that returns a more detailed error.

val parse_file_exn : 'a t -> string -> 'a

Same as parse_file, but

  • raises ParseError

    if it fails.

module U : sig ... end
module Debug_ : sig ... end

Debugging utils. EXPERIMENTAL

\ No newline at end of file diff --git a/dev/containers/CCRandom/index.html b/dev/containers/CCRandom/index.html index 9a6fd392..510f1dc5 100644 --- a/dev/containers/CCRandom/index.html +++ b/dev/containers/CCRandom/index.html @@ -5,4 +5,4 @@ delay (fun () -> let name = gensym() in small_int >>= fun i -> return (name,i) )
val choose : 'a t list -> 'a option t

Choose a generator within the list.

val choose_exn : 'a t list -> 'a t

Like choose but without option.

  • raises Invalid_argument

    if the list is empty.

val choose_array : 'a t array -> 'a option t

Choose a generator within the array.

val choose_return : 'a list -> 'a t

Choose among the list.

  • raises Invalid_argument

    if the list is empty.

val replicate : int -> 'a t -> 'a list t

replicate n g makes a list of n elements which are all generated randomly using g.

val sample_without_duplicates : cmp:('a -> 'a -> int) -> int -> 'a t -> 'a list t

sample_without_replacement n g makes a list of n elements which are all generated randomly using g with the added constraint that none of the generated random values are equal.

  • raises Invalid_argument

    if n <= 0.

  • since 2.4
val list_seq : 'a t list -> 'a list t

Build random lists from lists of random generators.

  • since 0.4
exception Pick_from_empty
  • since 0.16
val pick_list : 'a list -> 'a t

Pick an element at random from the list.

  • raises Pick_from_empty

    if the list is empty.

  • since 0.16
val pick_array : 'a array -> 'a t

Pick an element at random from the array.

  • raises Pick_from_empty

    if the array is empty.

  • since 0.16
val small_int : int t

A small int (100).

val int : int -> int t

Random int within the given range.

val int_range : int -> int -> int t

Inclusive range.

val small_float : float t

A reasonably small float (100.0).

  • since 0.6.1
val float : float -> float t

Random float within the given range.

  • since 0.6.1
val float_range : float -> float -> float t

Inclusive range. float_range a b assumes a < b.

  • since 0.6.1
val split : int -> (int * int) option t

Split a positive value n into n1,n2 where n = n1 + n2.

  • returns

    None if the value is too small.

val split_list : int -> len:int -> int list option t

Split a value n into a list of values whose sum is n and whose length is length. The list is never empty and does not contain 0.

  • raises Invalid_argument

    if len <= 1.

  • returns

    None if the value is too small.

val retry : ?max:int -> 'a option t -> 'a option t

retry g calls g until it returns some value, or until the maximum number of retries was reached. If g fails, then it counts for one iteration, and the generator retries.

  • parameter max:

    maximum number of retries. Default 10.

val try_successively : 'a option t list -> 'a option t

try_successively l tries each generator of l, one after the other. If some generator succeeds its result is returned, else the next generator is tried.

val (<?>) : 'a option t -> 'a option t -> 'a option t

a <?> b is a choice operator. It first tries a, and returns its result if successful. If a fails, then b is returned.

val fix : ?sub1:('a t -> 'a t) list -> ?sub2:('a t -> 'a t -> 'a t) list -> -?subn:(int t * ('a list t -> 'a t)) list -> base:'a t -> int t -> 'a t

Recursion combinators, for building recursive values. The integer generator is used to provide fuel. The sub_ generators should use their arguments only once!

  • parameter sub1

    cases that recurse on one value.

  • parameter sub2

    cases that use the recursive gen twice.

  • parameter subn

    cases that use a list of recursive cases.

Applicative
val pure : 'a -> 'a t
val (<*>) : ('a -> 'b) t -> 'a t -> 'b t

Let operators on OCaml >= 4.08.0, nothing otherwise

  • since 2.8
include CCShimsMkLet_.S with type 'a t_let := 'a t
val let+ : 'a t -> ('a -> 'b) -> 'b t
val and+ : 'a t -> 'b t -> ('a * 'b) t
val let* : 'a t -> ('a -> 'b t) -> 'b t
val and* : 'a t -> 'b t -> ('a * 'b) t
Run a generator
val run : ?st:state -> 'a t -> 'a

Using a random state (possibly the one in argument) run a generator.

\ No newline at end of file +?subn:(int t * ('a list t -> 'a t)) list -> base:'a t -> int t -> 'a t

Recursion combinators, for building recursive values. The integer generator is used to provide fuel. The sub_ generators should use their arguments only once!

Applicative
val pure : 'a -> 'a t
val (<*>) : ('a -> 'b) t -> 'a t -> 'b t

Let operators on OCaml >= 4.08.0, nothing otherwise

  • since 2.8
val let+ : 'a t -> ('a -> 'b) -> 'b t
val and+ : 'a t -> 'b t -> ('a * 'b) t
val let* : 'a t -> ('a -> 'b t) -> 'b t
val and* : 'a t -> 'b t -> ('a * 'b) t
Run a generator
val run : ?st:state -> 'a t -> 'a

Using a random state (possibly the one in argument) run a generator.

\ No newline at end of file diff --git a/dev/containers/CCResult/Infix/index.html b/dev/containers/CCResult/Infix/index.html index 05934401..936778a3 100644 --- a/dev/containers/CCResult/Infix/index.html +++ b/dev/containers/CCResult/Infix/index.html @@ -1,2 +1,2 @@ -Infix (containers.CCResult.Infix)

Module CCResult.Infix

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

Infix version of map.

  • since 3.0
val (>|=) : ('a'err) t -> ('a -> 'b) -> ('b'err) t

Infix version of map with reversed arguments.

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

Monadic composition. e >>= f proceeds as f x if e is Ok x or returns e if e is an Error.

val (<*>) : ('a -> 'b'err) t -> ('a'err) t -> ('b'err) t

a <*> b evaluates a and b, and, in case of success, returns Ok (a b). Otherwise, it fails, and the error of a is chosen over the error of b if both fail.

Let operators on OCaml >= 4.08.0, nothing otherwise

  • since 2.8
include CCShimsMkLet_.S2 with type ('a, 'e) t_let2 := ('a'e) result
val let+ : ('a'e) result -> ('a -> 'b) -> ('b'e) result
val and+ : ('a'e) result -> ('b'e) result -> ('a * 'b'e) result
val let* : ('a'e) result -> ('a -> ('b'e) result) -> ('b'e) result
val and* : ('a'e) result -> ('b'e) result -> ('a * 'b'e) result
\ No newline at end of file +Infix (containers.CCResult.Infix)

Module CCResult.Infix

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

Infix version of map.

  • since 3.0
val (>|=) : ('a'err) t -> ('a -> 'b) -> ('b'err) t

Infix version of map with reversed arguments.

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

Monadic composition. e >>= f proceeds as f x if e is Ok x or returns e if e is an Error.

val (<*>) : ('a -> 'b'err) t -> ('a'err) t -> ('b'err) t

a <*> b evaluates a and b, and, in case of success, returns Ok (a b). Otherwise, it fails, and the error of a is chosen over the error of b if both fail.

Let operators on OCaml >= 4.08.0, nothing otherwise

  • since 2.8
val let+ : ('a'e) result -> ('a -> 'b) -> ('b'e) result
val and+ : ('a'e) result -> ('b'e) result -> ('a * 'b'e) result
val let* : ('a'e) result -> ('a -> ('b'e) result) -> ('b'e) result
val and* : ('a'e) result -> ('b'e) result -> ('a * 'b'e) result
\ No newline at end of file diff --git a/dev/containers/CCResult/index.html b/dev/containers/CCResult/index.html index 5895a1d3..b988706b 100644 --- a/dev/containers/CCResult/index.html +++ b/dev/containers/CCResult/index.html @@ -1,2 +1,2 @@ -CCResult (containers.CCResult)

Module CCResult

Error Monad

Uses the new "result" type from OCaml 4.03.

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

Fast internal iterator.

  • since 2.8
type 'a equal = 'a -> 'a -> bool
type 'a ord = 'a -> 'a -> int
type 'a printer = Stdlib.Format.formatter -> 'a -> unit

Basics

type nonrec (+'good, +'bad) result = ('good'bad) Stdlib.result =
| Ok of 'good
| Error of 'bad
type (+'good, +'bad) t = ('good'bad) result =
| Ok of 'good
| Error of 'bad
val return : 'a -> ('a'err) t

Successfully return a value.

val fail : 'err -> ('a'err) t

Fail with an error.

val of_exn : exn -> ('a, string) t

of_exn e uses Printexc to print the exception as a string.

val of_exn_trace : exn -> ('a, string) t

of_exn_trace e is similar to of_exn e, but it adds the stacktrace to the error message.

Remember to call Printexc.record_backtrace true and compile with the debug flag for this to work.

val fail_printf : ('aStdlib.Buffer.t, unit, ('b, string) t) Stdlib.format4 -> 'a

fail_printf format uses format to obtain an error message and then returns Error msg.

val fail_fprintf : ('aStdlib.Format.formatter, unit, ('b, string) t) Stdlib.format4 -> 'a

fail_fprintf format uses format to obtain an error message and then returns Error msg.

val add_ctx : string -> ('a, string) t -> ('a, string) t

add_ctx msg leaves Ok x untouched, but transforms Error s into Error s' where s' contains the additional context given by msg.

  • since 1.2
val add_ctxf : ('aStdlib.Format.formatter, unit, ('b, string) t -> ('b, string) t) Stdlib.format4 -> 'a

add_ctxf format_message is similar to add_ctx but with Format for printing the message (eagerly). Example:

add_ctxf "message(number %d, foo: %B)" 42 true (Error "error)"
  • since 1.2
val map : ('a -> 'b) -> ('a'err) t -> ('b'err) t

Map on success.

val map_err : ('err1 -> 'err2) -> ('a'err1) t -> ('a'err2) t

Map on the error variant.

val map2 : ('a -> 'b) -> ('err1 -> 'err2) -> ('a'err1) t -> ('b'err2) t

Like map, but also with a function that can transform the error message in case of failure.

val iter : ('a -> unit) -> ('a_) t -> unit

Apply the function only in case of Ok.

val iter_err : ('err -> unit) -> (_'err) t -> unit

Apply the function in case of Error.

  • since 2.4
exception Get_error
val get_exn : ('a_) t -> 'a

Extract the value x from Ok x, fails otherwise. You should be careful with this function, and favor other combinators whenever possible.

  • raises Get_error

    if the value is an error.

val get_or : ('a_) t -> default:'a -> 'a

get_or e ~default returns x if e = Ok x, default otherwise.

val get_or_failwith : ('a, string) t -> 'a

get_or_failwith e returns x if e = Ok x, fails otherwise.

  • raises Failure

    with msg if e = Error msg.

  • since 2.4
val get_lazy : ('b -> 'a) -> ('a'b) t -> 'a

get_lazy default_fn x unwraps x, but if x = Error e it returns default_fr e instead.

  • since 3.0
val map_or : ('a -> 'b) -> ('a'c) t -> default:'b -> 'b

map_or f e ~default returns f x if e = Ok x, default otherwise.

val catch : ('a'err) t -> ok:('a -> 'b) -> err:('err -> 'b) -> 'b

catch e ~ok ~err calls either ok or err depending on the value of e.

val flat_map : ('a -> ('b'err) t) -> ('a'err) t -> ('b'err) t
val equal : err:'err equal -> 'a equal -> ('a'err) t equal
val compare : err:'err ord -> 'a ord -> ('a'err) t ord
val fold : ok:('a -> 'b) -> error:('err -> 'b) -> ('a'err) t -> 'b

fold ~ok ~error e opens e and, if e = Ok x, returns ok x, otherwise e = Error s and it returns error s.

val fold_ok : ('a -> 'b -> 'a) -> 'a -> ('b_) t -> 'a

fold_ok f acc r will compute f acc x if r=Ok x, and return acc otherwise, as if the result were a mere option.

  • since 1.2
val is_ok : ('a'err) t -> bool

Return true if Ok.

  • since 1.0
val is_error : ('a'err) t -> bool

Return true if Error.

  • since 1.0

Wrappers

val guard : (unit -> 'a) -> ('a, exn) t

guard f runs f () and returns its result wrapped in Ok. If f () raises some exception e, then it fails with Error e.

val guard_str : (unit -> 'a) -> ('a, string) t

Like guard but uses of_exn to print the exception.

val guard_str_trace : (unit -> 'a) -> ('a, string) t

Like guard_str but uses of_exn_trace instead of of_exn so that the stack trace is printed.

val wrap1 : ('a -> 'b) -> 'a -> ('b, exn) t

Like guard but gives the function one argument.

val wrap2 : ('a -> 'b -> 'c) -> 'a -> 'b -> ('c, exn) t

Like guard but gives the function two arguments.

val wrap3 : ('a -> 'b -> 'c -> 'd) -> 'a -> 'b -> 'c -> ('d, exn) t

Like guard but gives the function three arguments.

Applicative

val pure : 'a -> ('a'err) t

Synonym of return.

val join : (('a'err) t'err) t -> ('a'err) t

join t, in case of success, returns Ok o from Ok (Ok o). Otherwise, it fails with Error e where e is the unwrapped error of t.

val both : ('a'err) t -> ('b'err) t -> ('a * 'b'err) t

both a b, in case of success, returns Ok (o, o') with the ok values of a and b. Otherwise, it fails, and the error of a is chosen over the error of b if both fail.

Infix

module Infix : sig ... end
include module type of Infix
val (<$>) : ('a -> 'b) -> ('a'err) t -> ('b'err) t

Infix version of map.

  • since 3.0
val (>|=) : ('a'err) t -> ('a -> 'b) -> ('b'err) t

Infix version of map with reversed arguments.

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

Monadic composition. e >>= f proceeds as f x if e is Ok x or returns e if e is an Error.

val (<*>) : ('a -> 'b'err) t -> ('a'err) t -> ('b'err) t

a <*> b evaluates a and b, and, in case of success, returns Ok (a b). Otherwise, it fails, and the error of a is chosen over the error of b if both fail.

Let operators on OCaml >= 4.08.0, nothing otherwise

  • since 2.8
include CCShimsMkLet_.S2 with type ('a, 'e) t_let2 := ('a'e) result
val let+ : ('a'e) result -> ('a -> 'b) -> ('b'e) result
val and+ : ('a'e) result -> ('b'e) result -> ('a * 'b'e) result
val let* : ('a'e) result -> ('a -> ('b'e) result) -> ('b'e) result
val and* : ('a'e) result -> ('b'e) result -> ('a * 'b'e) result

Let operators on OCaml >= 4.08.0, nothing otherwise

  • since 2.8
include CCShimsMkLet_.S2 with type ('a, 'e) t_let2 := ('a'e) result
val let+ : ('a'e) result -> ('a -> 'b) -> ('b'e) result
val and+ : ('a'e) result -> ('b'e) result -> ('a * 'b'e) result
val let* : ('a'e) result -> ('a -> ('b'e) result) -> ('b'e) result
val and* : ('a'e) result -> ('b'e) result -> ('a * 'b'e) result

Collections

val flatten_l : ('a'err) t list -> ('a list'err) t

Same as map_l id: returns Ok [x1;…;xn] if l=[Ok x1; …; Ok xn], or the first error otherwise.

  • since 2.7
val map_l : ('a -> ('b'err) t) -> 'a list -> ('b list'err) t

map_l f [a1; …; an] applies the function f to a1, …, an ,and, in case of success for every element, returns the list of Ok-value. Otherwise, it fails and returns the first error encountered. Tail-recursive.

val fold_l : ('b -> 'a -> ('b'err) t) -> 'b -> 'a list -> ('b'err) t
val fold_iter : ('b -> 'a -> ('b'err) t) -> 'b -> 'a iter -> ('b'err) t
  • since 3.0

Misc

val choose : ('a'err) t list -> ('a'err list) t

choose l selects a member of l that is a Ok _ value, or returns Error l otherwise, where l is the list of errors.

val retry : int -> (unit -> ('a'err) t) -> ('a'err list) t

retry n f calls f at most n times, returning the first result of f () that doesn't fail. If f fails n times, retry n f fails with the list of successive errors.

module type MONAD = sig ... end
module Traverse (M : MONAD) : sig ... end

Conversions

val to_opt : ('a_) t -> 'a option

Convert a result to an option.

val of_opt : 'a option -> ('a, string) t

Convert an option to a result.

val to_iter : ('a_) t -> 'a iter
  • since 2.8
val to_seq : ('a_) t -> 'a Stdlib.Seq.t

Renamed from to_std_seq since 3.0.

  • since 3.0
type ('a, 'b) error = [
| `Ok of 'a
| `Error of 'b
]
val of_err : ('a'b) error -> ('a'b) t
  • since 0.17
val to_err : ('a'b) t -> ('a'b) error
  • since 0.17

IO

val pp : 'a printer -> ('a, string) t printer
val pp' : 'a printer -> 'e printer -> ('a'e) t printer

Printer that is generic on the error type.

\ No newline at end of file +CCResult (containers.CCResult)

Module CCResult

Error Monad

Uses the new "result" type from OCaml 4.03.

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

Fast internal iterator.

  • since 2.8
type 'a equal = 'a -> 'a -> bool
type 'a ord = 'a -> 'a -> int
type 'a printer = Stdlib.Format.formatter -> 'a -> unit

Basics

type nonrec (+'good, +'bad) result = ('good'bad) Stdlib.result =
| Ok of 'good
| Error of 'bad
type (+'good, +'bad) t = ('good'bad) result =
| Ok of 'good
| Error of 'bad
val return : 'a -> ('a'err) t

Successfully return a value.

val fail : 'err -> ('a'err) t

Fail with an error.

val of_exn : exn -> ('a, string) t

of_exn e uses Printexc to print the exception as a string.

val of_exn_trace : exn -> ('a, string) t

of_exn_trace e is similar to of_exn e, but it adds the stacktrace to the error message.

Remember to call Printexc.record_backtrace true and compile with the debug flag for this to work.

val fail_printf : ('aStdlib.Buffer.t, unit, ('b, string) t) Stdlib.format4 -> 'a

fail_printf format uses format to obtain an error message and then returns Error msg.

val fail_fprintf : ('aStdlib.Format.formatter, unit, ('b, string) t) Stdlib.format4 -> 'a

fail_fprintf format uses format to obtain an error message and then returns Error msg.

val add_ctx : string -> ('a, string) t -> ('a, string) t

add_ctx msg leaves Ok x untouched, but transforms Error s into Error s' where s' contains the additional context given by msg.

  • since 1.2
val add_ctxf : ('aStdlib.Format.formatter, unit, ('b, string) t -> ('b, string) t) Stdlib.format4 -> 'a

add_ctxf format_message is similar to add_ctx but with Format for printing the message (eagerly). Example:

add_ctxf "message(number %d, foo: %B)" 42 true (Error "error)"
  • since 1.2
val map : ('a -> 'b) -> ('a'err) t -> ('b'err) t

Map on success.

val map_err : ('err1 -> 'err2) -> ('a'err1) t -> ('a'err2) t

Map on the error variant.

val map2 : ('a -> 'b) -> ('err1 -> 'err2) -> ('a'err1) t -> ('b'err2) t

Like map, but also with a function that can transform the error message in case of failure.

val iter : ('a -> unit) -> ('a_) t -> unit

Apply the function only in case of Ok.

val iter_err : ('err -> unit) -> (_'err) t -> unit

Apply the function in case of Error.

  • since 2.4
exception Get_error
val get_exn : ('a_) t -> 'a

Extract the value x from Ok x, fails otherwise. You should be careful with this function, and favor other combinators whenever possible.

  • raises Get_error

    if the value is an error.

val get_or : ('a_) t -> default:'a -> 'a

get_or e ~default returns x if e = Ok x, default otherwise.

val get_or_failwith : ('a, string) t -> 'a

get_or_failwith e returns x if e = Ok x, fails otherwise.

  • raises Failure

    with msg if e = Error msg.

  • since 2.4
val get_lazy : ('b -> 'a) -> ('a'b) t -> 'a

get_lazy default_fn x unwraps x, but if x = Error e it returns default_fr e instead.

  • since 3.0
val map_or : ('a -> 'b) -> ('a'c) t -> default:'b -> 'b

map_or f e ~default returns f x if e = Ok x, default otherwise.

val catch : ('a'err) t -> ok:('a -> 'b) -> err:('err -> 'b) -> 'b

catch e ~ok ~err calls either ok or err depending on the value of e.

val flat_map : ('a -> ('b'err) t) -> ('a'err) t -> ('b'err) t
val equal : err:'err equal -> 'a equal -> ('a'err) t equal
val compare : err:'err ord -> 'a ord -> ('a'err) t ord
val fold : ok:('a -> 'b) -> error:('err -> 'b) -> ('a'err) t -> 'b

fold ~ok ~error e opens e and, if e = Ok x, returns ok x, otherwise e = Error s and it returns error s.

val fold_ok : ('a -> 'b -> 'a) -> 'a -> ('b_) t -> 'a

fold_ok f acc r will compute f acc x if r=Ok x, and return acc otherwise, as if the result were a mere option.

  • since 1.2
val is_ok : ('a'err) t -> bool

Return true if Ok.

  • since 1.0
val is_error : ('a'err) t -> bool

Return true if Error.

  • since 1.0

Wrappers

val guard : (unit -> 'a) -> ('a, exn) t

guard f runs f () and returns its result wrapped in Ok. If f () raises some exception e, then it fails with Error e.

val guard_str : (unit -> 'a) -> ('a, string) t

Like guard but uses of_exn to print the exception.

val guard_str_trace : (unit -> 'a) -> ('a, string) t

Like guard_str but uses of_exn_trace instead of of_exn so that the stack trace is printed.

val wrap1 : ('a -> 'b) -> 'a -> ('b, exn) t

Like guard but gives the function one argument.

val wrap2 : ('a -> 'b -> 'c) -> 'a -> 'b -> ('c, exn) t

Like guard but gives the function two arguments.

val wrap3 : ('a -> 'b -> 'c -> 'd) -> 'a -> 'b -> 'c -> ('d, exn) t

Like guard but gives the function three arguments.

Applicative

val pure : 'a -> ('a'err) t

Synonym of return.

val join : (('a'err) t'err) t -> ('a'err) t

join t, in case of success, returns Ok o from Ok (Ok o). Otherwise, it fails with Error e where e is the unwrapped error of t.

val both : ('a'err) t -> ('b'err) t -> ('a * 'b'err) t

both a b, in case of success, returns Ok (o, o') with the ok values of a and b. Otherwise, it fails, and the error of a is chosen over the error of b if both fail.

Infix

module Infix : sig ... end
include module type of Infix
val (<$>) : ('a -> 'b) -> ('a'err) t -> ('b'err) t

Infix version of map.

  • since 3.0
val (>|=) : ('a'err) t -> ('a -> 'b) -> ('b'err) t

Infix version of map with reversed arguments.

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

Monadic composition. e >>= f proceeds as f x if e is Ok x or returns e if e is an Error.

val (<*>) : ('a -> 'b'err) t -> ('a'err) t -> ('b'err) t

a <*> b evaluates a and b, and, in case of success, returns Ok (a b). Otherwise, it fails, and the error of a is chosen over the error of b if both fail.

Let operators on OCaml >= 4.08.0, nothing otherwise

  • since 2.8
val let+ : ('a'e) result -> ('a -> 'b) -> ('b'e) result
val and+ : ('a'e) result -> ('b'e) result -> ('a * 'b'e) result
val let* : ('a'e) result -> ('a -> ('b'e) result) -> ('b'e) result
val and* : ('a'e) result -> ('b'e) result -> ('a * 'b'e) result

Collections

val flatten_l : ('a'err) t list -> ('a list'err) t

Same as map_l id: returns Ok [x1;…;xn] if l=[Ok x1; …; Ok xn], or the first error otherwise.

  • since 2.7
val map_l : ('a -> ('b'err) t) -> 'a list -> ('b list'err) t

map_l f [a1; …; an] applies the function f to a1, …, an ,and, in case of success for every element, returns the list of Ok-value. Otherwise, it fails and returns the first error encountered. Tail-recursive.

val fold_l : ('b -> 'a -> ('b'err) t) -> 'b -> 'a list -> ('b'err) t
val fold_iter : ('b -> 'a -> ('b'err) t) -> 'b -> 'a iter -> ('b'err) t
  • since 3.0

Misc

val choose : ('a'err) t list -> ('a'err list) t

choose l selects a member of l that is a Ok _ value, or returns Error l otherwise, where l is the list of errors.

val retry : int -> (unit -> ('a'err) t) -> ('a'err list) t

retry n f calls f at most n times, returning the first result of f () that doesn't fail. If f fails n times, retry n f fails with the list of successive errors.

module type MONAD = sig ... end
module Traverse (M : MONAD) : sig ... end

Conversions

val to_opt : ('a_) t -> 'a option

Convert a result to an option.

val of_opt : 'a option -> ('a, string) t

Convert an option to a result.

val to_iter : ('a_) t -> 'a iter
  • since 2.8
val to_seq : ('a_) t -> 'a Stdlib.Seq.t

Renamed from to_std_seq since 3.0.

  • since 3.0
type ('a, 'b) error = [
| `Ok of 'a
| `Error of 'b
]
val of_err : ('a'b) error -> ('a'b) t
  • since 0.17
val to_err : ('a'b) t -> ('a'b) error
  • since 0.17

IO

val pp : 'a printer -> ('a, string) t printer
val pp' : 'a printer -> 'e printer -> ('a'e) t printer

Printer that is generic on the error type.

\ No newline at end of file diff --git a/dev/containers/CCVector/index.html b/dev/containers/CCVector/index.html index 30c4cf7e..1540b2cf 100644 --- a/dev/containers/CCVector/index.html +++ b/dev/containers/CCVector/index.html @@ -1,3 +1,3 @@ CCVector (containers.CCVector)

Module CCVector

Growable, mutable vector

type ro = [
| `RO
]
type rw = [
| `RW
]

Mutability is rw (read-write) or ro (read-only).

type ('a, 'mut) t

The type of a vector of elements of type 'a, with a mutability flat 'mut.

type 'a vector = ('arw) t

Type synonym: a 'a vector is mutable.

type 'a ro_vector = ('aro) t

Alias for immutable vectors.

  • since 0.15
type 'a iter = ('a -> unit) -> unit

Fast internal iterator.

  • since 2.8
type 'a gen = unit -> 'a option
type 'a equal = 'a -> 'a -> bool
type 'a ord = 'a -> 'a -> int
type 'a printer = Stdlib.Format.formatter -> 'a -> unit
val freeze : ('a_) t -> ('aro) t

Make an immutable vector (no copy! Don't use the old version).

val freeze_copy : ('a_) t -> ('aro) t

Copy the vector into an immutable version.

val create : unit -> ('arw) t

Create a new, empty vector.

val create_with : ?capacity:int -> 'a -> ('arw) t

Create a new vector, the value is used to enforce the type the new vector.

  • parameter capacity

    the size of the underlying array.

val return : 'a -> ('a'mut) t

Singleton vector.

  • since 0.14
val make : int -> 'a -> ('a'mut) t

make n x makes a vector of size n, filled with x.

val init : int -> (int -> 'a) -> ('a'mut) t

Init the vector with the given function and size.

val clear : ('arw) t -> unit

Clear the content of the vector. This ensures that length v = 0 but the underlying array is kept, and possibly references to former elements, which are therefore not garbage collectible.

val clear_and_reset : ('arw) t -> unit

Clear the content of the vector, and deallocate the underlying array, removing references to all the elements. The elements can be collected.

  • since 2.8
val ensure_with : init:'a -> ('arw) t -> int -> unit

Hint to the vector that it should have at least the given capacity. This does not affect length v.

  • parameter init

    if capacity v = 0, used to enforce the type of the vector (see create_with).

  • raises Invalid_arg

    if the size is not suitable (negative, or too big for OCaml arrays)

  • since 0.14
val ensure : ('arw) t -> int -> unit

Hint to the vector that it should have at least the given capacity. Just a hint, will not be enforced if the vector is empty and init is not provided.

  • raises Invalid_arg

    if the size is not suitable (negative, or too big for OCaml arrays)

val is_empty : ('a_) t -> bool

Is the vector empty?

val push : ('arw) t -> 'a -> unit

Add an element at the end of the vector.

val resize_with : ('arw) t -> (int -> 'a) -> int -> unit

resize_with vec f size resizes vector vec up to size, fills vector with calls to f on indexes [vec.size-1.. size - 1]. The contents and size of vec are untouched if size is inferior or equal to length vec.

  • raises Invalid_argument

    if the size is too big

  • since NEXT_RELEASE
val resize_with_init : ('arw) t -> init:'a -> int -> unit

resize_with_init vec init size resizes vector vec up to size, fills vector with calls to init on indexes [length vec -1.. size - 1]. The contents and size of vec are untouched if size is inferior or equal to length vec.

  • raises Invalid_argument

    if the size is too big

  • since NEXT_RELEASE
val append : ('arw) t -> ('a_) t -> unit

append a b adds all elements of b to a.

val append_array : ('arw) t -> 'a array -> unit

Like append, with an array.

val append_iter : ('arw) t -> 'a iter -> unit

Append content of iterator.

  • since 2.8
val append_seq : ('arw) t -> 'a Stdlib.Seq.t -> unit

Append content of iterator. Renamed from append_std_seq since 3.0.

  • since 3.0
val append_list : ('arw) t -> 'a list -> unit

Append content of list.

  • since 0.14
val append_gen : ('arw) t -> 'a gen -> unit

Append content of generator.

  • since 0.20
val equal : 'a equal -> ('a_) t equal
val compare : 'a ord -> ('a_) t ord

Total ordering on vectors. Lexicographic comparison.

exception Empty

Raised on empty stack.

val pop : ('arw) t -> 'a option

Remove last element, or None.

val pop_exn : ('arw) t -> 'a

Remove last element, or raise an exception if empty.

  • raises Empty

    on an empty vector.

val top : ('a_) t -> 'a option

Top element, if present.

  • since 0.6
val top_exn : ('a_) t -> 'a

Top element, if present.

  • raises Empty

    on an empty vector.

  • since 0.6
val copy : ('a_) t -> ('a'mut) t

Shallow copy (may give an immutable or mutable vector).

val truncate : ('arw) t -> int -> unit

Truncate to the given size (remove elements above this size). Does nothing if the parameter is bigger than the current size. truncate was called shrink.

  • since 3.0
val shrink_to_fit : ('a_) t -> unit

Shrink internal array to fit the size of the vector

  • since 2.8
val member : eq:('a -> 'a -> bool) -> 'a -> ('a_) t -> bool

Is the element a member of the vector?

val sort : ('a -> 'a -> int) -> ('a_) t -> ('a'mut) t

Sort the vector, returning a copy of it that is sorted w.r.t the given ordering. The vector itself is unchanged. The underlying array of the new vector can be smaller than the original one.

val sort' : ('a -> 'a -> int) -> ('arw) t -> unit

Sort the vector in place (modifying it). This function change the size of the underlying array.

val uniq_sort : ('a -> 'a -> int) -> ('arw) t -> unit

Sort the array and remove duplicates, in place (e.g. modifying the vector itself).

val iter : ('a -> unit) -> ('a_) t -> unit

Iterate on the vector's content.

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

Iterate on the vector, with indexes.

val map : ('a -> 'b) -> ('a_) t -> ('b'mut) t

Map elements of the vector, yielding a new vector.

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

map f v is just like map, but it also passes in the index of each element as the first argument to the function f.

  • since 2.8
val map_in_place : ('a -> 'a) -> ('a_) t -> unit

Map elements of the vector in place

  • since 2.3
val filter : ('a -> bool) -> ('a_) t -> ('a'mut) t

Filter elements from the vector. filter p v leaves v unchanged but returns a new vector that only contains elements of v satisfying p.

val filter_in_place : ('a -> bool) -> ('arw) t -> unit

Filter elements from the vector in place.

  • since 3.0
val fold : ('b -> 'a -> 'b) -> 'b -> ('a_) t -> 'b

Fold on elements of the vector

val exists : ('a -> bool) -> ('a_) t -> bool

Existential test (is there an element that satisfies the predicate?).

val for_all : ('a -> bool) -> ('a_) t -> bool

Universal test (do all the elements satisfy the predicate?).

val find : ('a -> bool) -> ('a_) t -> 'a option

Find an element that satisfies the predicate.

val find_exn : ('a -> bool) -> ('a_) t -> 'a

Find an element that satisfies the predicate, or

  • raises Not_found

    if no element does.

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

find_map f v returns the first Some y = f x for x in v, or None if f x = None for each x in v.

  • since 0.14
val filter_map : ('a -> 'b option) -> ('a_) t -> ('b'mut) t

Map elements with a function, possibly filtering some of them out.

val filter_map_in_place : ('a -> 'a option) -> ('a_) t -> unit

Filter-map elements of the vector in place

  • since 2.3
val flat_map : ('a -> ('b_) t) -> ('a_) t -> ('b'mut) t

Map each element to a sub-vector.

val flat_map_seq : ('a -> 'b Stdlib.Seq.t) -> ('a_) t -> ('b'mut) t

Like flat_map, but using Seq for intermediate collections. Renamed from flat_map_std_seq since 3.0.

  • since 3.0
val flat_map_list : ('a -> 'b list) -> ('a_) t -> ('b'mut) t

Like flat_map, but using list for intermediate collections.

  • since 0.14
val monoid_product : ('a -> 'b -> 'c) -> ('a_) t -> ('b_) t -> ('c_) t

All combinaisons of tuples from the two vectors are passed to the function.

  • since 2.8
val (>>=) : ('a_) t -> ('a -> ('b_) t) -> ('b'mut) t

Infix version of flat_map.

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

Infix version of map.

val get : ('a_) t -> int -> 'a

Access element by its index, or

  • raises Invalid_argument

    if bad index.

val set : ('arw) t -> int -> 'a -> unit

Modify element at given index, or

  • raises Invalid_argument

    if the index is invalid (i.e. not in [0.. length v-1]).

val remove_and_shift : ('arw) t -> int -> unit

remove_and_shift v i remove the i-th element from v. Move elements that are after the i-th in v, in linear time. Preserve the order of the elements in v. See remove_unordered for constant time removal function that doesn't preserve the order of elements.

  • since 3.0
val remove_unordered : ('arw) t -> int -> unit

remove_unordered v i remove the i-th element from v. Does NOT preserve the order of the elements in v (might swap with the last element). See remove_and_shift if you want to keep the ordering.

val rev : ('a_) t -> ('a'mut) t

Reverse the vector.

val rev_in_place : ('arw) t -> unit

Reverse the vector in place.

  • since 0.14
val rev_iter : ('a -> unit) -> ('a_) t -> unit

rev_iter f a is the same as iter f (rev a), only more efficient.

  • since 0.14
val size : ('a_) t -> int

Number of elements in the vector.

val length : (__) t -> int

Synonym for size.

val capacity : (__) t -> int

Number of elements the vector can contain without being resized.

val unsafe_get_array : ('arw) t -> 'a array

Access the underlying shared array (do not modify!). unsafe_get_array v is longer than size v, but elements at higher index than size v are undefined (do not access!).

val (--) : int -> int -> (int, 'mut) t

Range of integers, either ascending or descending (both included, therefore the result is never empty). Example: 1 -- 10 returns the vector [1;2;3;4;5;6;7;8;9;10].

val (--^) : int -> int -> (int, 'mut) t

Range of integers, either ascending or descending, but excluding right. Example: 1 --^ 10 returns the vector [1;2;3;4;5;6;7;8;9].

  • since 0.17
val of_array : 'a array -> ('a'mut) t

of_array a returns a vector corresponding to the array a. Operates in O(n) time.

val of_list : 'a list -> ('a'mut) t
val to_array : ('a_) t -> 'a array

to_array v returns an array corresponding to the vector v.

val to_list : ('a_) t -> 'a list

Return a list with the elements contained in the vector.

val of_iter : ?init:('arw) t -> 'a iter -> ('arw) t

Convert an Iterator to a vector.

  • since 2.8.1
val of_seq : ?init:('arw) t -> 'a Stdlib.Seq.t -> ('arw) t

Convert an Iterator to a vector. Renamed from of_std_seq since 3.0.

  • since 3.0
val to_iter : ('a_) t -> 'a iter

Return a iter with the elements contained in the vector.

  • since 2.8
val to_iter_rev : ('a_) t -> 'a iter

to_iter_rev v returns the sequence of elements of v in reverse order, that is, the last elements of v are iterated on first.

  • since 2.8
val to_seq : ('a_) t -> 'a Stdlib.Seq.t

Return an iterator with the elements contained in the vector. Renamed from to_std_seq since 3.0.

  • since 3.0
val to_seq_rev : ('a_) t -> 'a Stdlib.Seq.t

to_seq v returns the sequence of elements of v in reverse order, that is, the last elements of v are iterated on first. Renamed from to_std_seq since 3.0.

  • since 3.0
val slice : ('arw) t -> 'a array * int * int

Vector as an array slice. By doing it we expose the internal array, so be careful!.

val slice_iter : ('a_) t -> int -> int -> 'a iter

slice_iter v start len is the sequence of elements from v.(start) to v.(start+len-1).

  • since 3.0
val of_gen : ?init:('arw) t -> 'a gen -> ('arw) t
val to_gen : ('a_) t -> 'a gen
val to_string : ?start:string -> ?stop:string -> ?sep:string -> -('a -> string) -> ('a_) t -> string

Print the vector in a string

  • since 2.7
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 pp_item ppf v formats the vector v on ppf. Each element is formatted with pp_item, pp_start is called at the beginning, pp_stop is called at the end, pp_sep is called between each elements. By defaults pp_start and pp_stop does nothing and pp_sep defaults to (fun out -> Format.fprintf out ",@ ").

Let operators on OCaml >= 4.08.0, nothing otherwise

  • since 2.8
include CCShimsMkLet_.S2 with type ('a, 'e) t_let2 := ('a'e) t
val let+ : ('a'e) t -> ('a -> 'b) -> ('b'e) t
val and+ : ('a'e) t -> ('b'e) t -> ('a * 'b'e) t
val let* : ('a'e) t -> ('a -> ('b'e) t) -> ('b'e) t
val and* : ('a'e) t -> ('b'e) t -> ('a * 'b'e) t
\ No newline at end of file +('a -> string) -> ('a_) t -> string

Print the vector in a string

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 pp_item ppf v formats the vector v on ppf. Each element is formatted with pp_item, pp_start is called at the beginning, pp_stop is called at the end, pp_sep is called between each elements. By defaults pp_start and pp_stop does nothing and pp_sep defaults to (fun out -> Format.fprintf out ",@ ").

Let operators on OCaml >= 4.08.0, nothing otherwise

  • since 2.8
val let+ : ('a'e) t -> ('a -> 'b) -> ('b'e) t
val and+ : ('a'e) t -> ('b'e) t -> ('a * 'b'e) t
val let* : ('a'e) t -> ('a -> ('b'e) t) -> ('b'e) t
val and* : ('a'e) t -> ('b'e) t -> ('a * 'b'e) t
\ No newline at end of file