package non_empty_list
Module Non_empty_listSource
A non-empty list is defined as a immutable (singly) linked list containing at least 1 element. Identical in terms of complexity to List. The API is inspired by Janestreet's Base.List (but is not identical).
The non-empty list type. The use of the ( :: ) infix constructor allows the usage of the built-in list syntactic sugar provided by OCaml.
For example, a singleton is given by [1] . A list containing 2 elements is given by [1; 2] .
val equal :
('a -> 'a -> Ppx_deriving_runtime.bool) ->
'a t ->
'a t ->
Ppx_deriving_runtime.boolval compare :
('a -> 'a -> Ppx_deriving_runtime.int) ->
'a t ->
'a t ->
Ppx_deriving_runtime.intval pp :
(Ppx_deriving_runtime.Format.formatter -> 'a -> Ppx_deriving_runtime.unit) ->
Ppx_deriving_runtime.Format.formatter ->
'a t ->
Ppx_deriving_runtime.unitval show :
(Ppx_deriving_runtime.Format.formatter -> 'a -> Ppx_deriving_runtime.unit) ->
'a t ->
Ppx_deriving_runtime.stringinclude Base.Monad.S with type 'a t := 'a t
t >>= f returns a computation that sequences the computations represented by two monad elements. The resulting computation first does t to yield a value v, and then runs the computation returned by f v.
ignore_m t is map t ~f:(fun _ -> ()). ignore_m used to be called ignore, but we decided that was a bad name, because it shadowed the widely used Stdlib.ignore. Some monads still do let ignore = ignore_m for historical reasons.
Like all, but ensures that every monadic value in the list produces a unit value, all of which are discarded rather than being collected into a list.
These are convenient to have in scope when programming with a monad:
init n ~f returns the non-empty list [(f 0); (f 1); ...; (f (n-1))].
of_list creates a non-empty list from a standard list, returning None if the empty list is provided.
of_array creates a non-empty list from an array. of_array arr is equivalent to of_list (List.to_array arr).
Simialar to Non_empty_list.of_array, but raises an Invalid_argument if an empty array is provided.
to_array creates an array from a non-empty list. to_array t is equivalent to Array.of_list (to_list t).
length t returns the number of elements in t. Note that length t >= 1 for all t.
is_singleton t returns true if t is a singleton (a non-empty list containing a single element).
append t1 t2 returns a list t consisting of all elements of t1 and t2 (order preserved).
rev_append t1 t2 reverses t1 and then appends it to t2. Equivalent to append (rev t1) t2, however, is more efficient.
hd returns the first element of the given non-empty list t. Note that no exception can occur.
tl returns the tail of the non-empty list. Note that the list type is used for the returned value, since removing an element from a non-empty list may yield an empty list (e.g. a singleton).
Returns the n-th element of the list. The head of the list has position 0. None is returned if the list t is too short or n is negative.
Equivalent to nth. However, an Invalid_argument exception is raised if the list t is too short or n is negative.
Checks whether the provided element x is a member of the non-empty list t, using equal.
find t ~f returns the first element of the non-empty list t that satisfied f.
Similar to Non_empty_list.find, but raises a Not_found if there is no such element that satisfies f.
Similar to find, however, the index is passed as an argument to f as well.
find_map t ~f returns Some x, the first element x for which f x retunrs Some x. Returns None if there is no such element.
Similar to Non_empty_list.find_map, however, the index is passed as an argument to f as well.
Or_unequal_lengths is used for functions that take multiple non-empty lists (denoted t1, etc). Defines the dependent type: {'a : length t1 = length t2 = ... length tn}. Extends the Base.List.Or_unequal_lengths implementation with the Monad methods, improves readabily and reuseability of other library functions.
rev_map t ~f is equivalent to rev (map t ~f). However, it is more efficient.
rev_map2 t1 t2 ~f is equivalent to map2 t1 t2 ~f >>| rev. Returns Unequal_lengths if length t1 <> length t2.
Similar to rev_map2, however, raises Invalid_argument if length t1 <> length t2.
for_all [x1; ...; xn] ~f returns true iff f xi is true for all 1 <= i <= n. This is a short-circuiting operation, evaluting f xi in a left-to-right order.
Similar to Non_empty_list.for_all, however, the index is passed as an argument to f as well.
Similar to Non_empty_list.for_all, but defines a 2 non-empty list quanitifer
for_all [x1; ...; xn] ~f returns true iff there exists 1 <= i <= n such that f xi is true. This is a short-circuiting operation, evaluting f xi in a left-to-right order.
Similar to Non_empty_list.exists, however, the index is passed as an argument to f as well.
Similar to Non_empty_list.exists, but defines a 2 non-empty list quanitifer.
filter t ~f returns a *list* of elements of t that satisfy the predicate f. The order is preserved and evaluation is left-to-right.
rev_filter t ~f is equivalent to List.rev (filter t ~f), however, is more efficient.
map ~f [x1; ...; xn] (n >= 1) applies f to x1, ..., xn (left-to-right order), yielding the resultant non-empty list [f x1; ..., f xn].
Similar to Non_empty_list.map, however, the index is passed as an argument to f as well.
Similar to Non_empty_list.map, but defined for 2 non-empty lists.
concat_map t ~f is equivalent to concat (map t ~f).
iter [x1; ...; xn] ~f is equivalent to f x1; ...; f xn
iter2 [x1; ...; xn] [y1; ...; ym] ~f is equvalent to f x1 y2; ...; f xn yn. Returns Unequal_lengths if m <> n.
fold_left [x1; ...; xn] ~init ~f returns f (... f (f init x1) x2 ...) xn. fold_left t ~init ~f is equivalent to List.fold_left (to_list t) ~init ~f
Similar to Non_empty_list.fold_left, however, the index is passed as an argument to f as well.
fold_right [x1; ...; xn] ~init ~f returns f x1 (... f x(n-1) (f init xn) ...). fold_right t ~init ~f is equivalent to List.fold_right (to_list t) ~init ~f
Similar to Non_empty_list.fold_right, however, the index is passed as an argument to f as well.
reduce [x1; ...; xn] ~f returns f (... f (f x1 x2) x3 ...) xn. Note that no exception cqan occur.
count t ~f returns the number of elements of t that satisfy f.
Similar to Non_empty_list.count, however, the index is passed as an argument to f as well.
zip [x1; ...; xn] [y1; ...; ym] returns [(x1, y1); ...; (xn, yn)]. Returns Unequal_lengths if m <> n
unzip [(x1, y1); ...; (xn, yn)] returns ([x1; ...; xn], [y1; ...; yn]).