Legend:
Library
Module
Module type
Parameter
Class
Class type
Library
Module
Module type
Parameter
Class
Class type
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.bool
val compare :
('a -> 'a -> Ppx_deriving_runtime.int) ->
'a t ->
'a t ->
Ppx_deriving_runtime.int
val pp :
(Ppx_deriving_runtime.Format.formatter -> 'a -> Ppx_deriving_runtime.unit) ->
Ppx_deriving_runtime.Format.formatter ->
'a t ->
Ppx_deriving_runtime.unit
val show :
(Ppx_deriving_runtime.Format.formatter -> 'a -> Ppx_deriving_runtime.unit) ->
'a t ->
Ppx_deriving_runtime.string
type 'a non_empty_list = 'a t
include 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
.
module Monad_infix : sig ... end
val return : 'a -> 'a t
return v
returns the (trivial) computation that returns 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.
module Let_syntax : sig ... end
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))]
.
val of_list : 'a Base.list -> 'a t Base.option
of_list
creates a non-empty list from a standard list, returning None
if the empty list is provided.
val of_array : 'a Base.array -> 'a t Base.option
of_array
creates a non-empty list from an array. of_array arr
is equivalent to of_list (List.to_array arr)
.
val of_array_exn : 'a Base.array -> 'a t
Simialar to Non_empty_list.of_array
, but raises an Invalid_argument
if an empty array is provided.
val to_array : 'a t -> 'a Base.array
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.
val hd : 'a t -> 'a
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).
val last : 'a t -> 'a
last
returns the final element of the non-empty list.
val nth : 'a t -> Base.int -> 'a Base.option
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
.
val find : 'a t -> f:('a -> Base.bool) -> 'a Base.option
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
.
val findi :
'a t ->
f:(Base.int -> 'a -> Base.bool) ->
(Base.int * 'a) Base.option
Similar to find
, however, the index is passed as an argument to f
as well.
val find_map : 'a t -> f:('a -> 'b Base.option) -> 'b Base.option
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.
val find_map_exn : 'a t -> f:('a -> 'b Base.option) -> 'b
val find_mapi :
'a t ->
f:(Base.int -> 'a -> 'b Base.option) ->
(Base.int * 'b) Base.option
Similar to Non_empty_list.find_map
, however, the index is passed as an argument to f
as well.
val find_mapi_exn :
'a t ->
f:(Base.int -> 'a -> 'b Base.option) ->
Base.int * 'b
module Or_unequal_lengths : sig ... end
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.
val rev_map2 : 'a t -> 'b t -> f:('a -> 'b -> 'c) -> 'c t Or_unequal_lengths.t
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.
val for_all2 :
'a t ->
'b t ->
f:('a -> 'b -> Base.bool) ->
Base.bool Or_unequal_lengths.t
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.
val exists2 :
'a t ->
'b t ->
f:('a -> 'b -> Base.bool) ->
Base.bool Or_unequal_lengths.t
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.
val filter_map : 'a t -> f:('a -> 'b Base.option) -> 'b Base.list
val filter_mapi : 'a t -> f:(Base.int -> 'a -> 'b Base.option) -> 'b Base.list
rev_filter t ~f
is equivalent to List.rev (filter t ~f)
, however, is more efficient.
val rev_filter_map : 'a t -> f:('a -> 'b Base.option) -> 'b Base.list
val rev_filter_mapi :
'a t ->
f:(Base.int -> 'a -> 'b Base.option) ->
'b Base.list
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.
val map2 : 'a t -> 'b t -> f:('a -> 'b -> 'c) -> 'c t Or_unequal_lengths.t
Similar to Non_empty_list.map
, but defined for 2 non-empty lists.
iter [x1; ...; xn] ~f
is equivalent to f x1; ...; f xn
val iter2 :
'a t ->
'b t ->
f:('a -> 'b -> Base.unit) ->
Base.unit Or_unequal_lengths.t
iter2 [x1; ...; xn] [y1; ...; ym] ~f
is equvalent to f x1 y2; ...; f xn yn
. Returns Unequal_lengths
if m <> n
.
val fold_left : 'a t -> init:'b -> f:('b -> 'a -> 'b) -> 'b
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.
val fold_right : 'a t -> init:'b -> f:('a -> 'b -> 'b) -> 'b
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.
val reduce : 'a t -> f:('a -> 'a -> 'a) -> 'a
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.
val zip : 'a t -> 'b t -> ('a * 'b) t Or_unequal_lengths.t
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])
.
val partition_map :
'a t ->
f:('a -> ('b, 'c) Base.Either.t) ->
'b Base.list * 'c Base.list