package batteries

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

Exceptionless counterparts for error-raising operations

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

find p l returns Some x where x is the first element of l such as p x returns true or None if such an element has not been found.

val rfind : ('a -> bool) -> 'a list -> 'a option

rfind p l returns Some x where x is the last element of l such that p x returns true or None if such element as not been found.

val findi : (int -> 'a -> bool) -> 'a list -> (int * 'a) option

findi p l returns Some (i, ai) where ai and i are respectively the first element of l and its index, such that p i ai is true, or None if no such element has been found.

val split_at : int -> 'a list -> [ `Ok of 'a list * 'a list | `Invalid_argument of string ]

Whenever n is inside of l size bounds, split_at n l returns Ok(l1,l2), where l1 contains the first n elements of l and l2 contains the others. Otherwise, returns `Invalid_argument n.

val at : 'a list -> int -> [ `Ok of 'a | `Invalid_argument of string ]

If n is inside the bounds of l, at l n returns Ok x, where x is the n-th element of the list l. Otherwise, returns Error (`Invalid_argument(n)).

val assoc : 'a -> ('a * 'b) list -> 'b option

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

val assoc_inv : 'b -> ('a * 'b) list -> 'a option

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

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

As assoc but with physical equality.

val find_map : ('a -> 'b option) -> 'a list -> 'b option

find_map f xs returns Some y such that x is the first element of the list where f x returns Some y. It returns None if no such element exists.

val hd : 'a list -> 'a option

hd l returns Some x such that x is the first element of the given list l. Returns None if list l is empty.

val tl : 'a list -> 'a list option

tl l returns Some x such that x is the given list l without its first element. Returns None if list l is empty.

val last : 'a list -> 'a option

last l returns either Some x where x is the last element of the list, or None if the list is empty. This function takes linear time.

val reduce : ('a -> 'a -> 'a) -> 'a list -> 'a option

reduce f h::t is Some (fold_left f h t) and reduce f [] is None.

val min_max : ?cmp:('a -> 'a -> int) -> 'a list -> ('a * 'a) option

min_max l returns either Some(s, l) where s and l are respectively the smallest and biggest element of l as judged by Pervasives.compare (by default) or None if l is empty. You can provide another comparison function via the optional cmp parameter.

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

max l returns either Some x where x is the largest value of the list as judged by Pervasives.compare (by default) or None is the list is empty. You can provide another comparison function via the optional cmp parameter.

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

min l returns either Some x where x is the smallest value of the list as judged by Pervasives.compare or None is the list is empty. You can provide another comparison function via the optional cmp parameter.

OCaml

Innovation. Community. Security.