package yocaml

  1. Overview
  2. Docs

Module Yocaml.NelSource

A Nel, for Non Empty List, is a list that ensures it has at least one element, which is very useful for describing, for example, error lists, where if there is an error, we ensure that there is at least one error.

Types

Sourcetype 'a t =
  1. | :: of 'a * 'a list

A non-empty list is nothing more than a pair of a value and a list.

Creating lists

Sourceval singleton : 'a -> 'a t

singleton x constructs a list with one element.

Sourceval cons : 'a -> 'a t -> 'a t

cons x xs constructs a non-empty list whose head is x and whose tail is xs.

Sourceval init : int -> (int -> 'a) -> 'a t

init len f is f 0; f 1; ...; f (len-1), evaluated left to right.

Sourceval from_list : 'a list -> 'a t option

from_list l convert a regular list to a non-empty one, if the given list is empty the function returns None.

Sourceval from_seq : 'a Seq.t -> 'a t option

from_seq l convert a regular seq to a non-empty one, if the given seq is empty the function returns None.

Conversion

Sourceval to_list : 'a t -> 'a list

to_list nel Converts a non-empty list into a regular list.

Sourceval to_seq : 'a t -> 'a Seq.t

to_seq nel Converts a non-empty list into a sequence.

Fact about size

Sourceval length : 'a t -> int

length nel returns the list of the non-empty list.

Sourceval is_singleton : 'a t -> bool

is_singleton x returns true if the non-empty list has just one element, false otherwise.

Misc functions

Sourceval hd : 'a t -> 'a

hd nel returns the head of the non-empty list. Since the list can't be empty, the function never fail.

Sourceval tl : 'a t -> 'a list

tl nel returns the tail of the non-empty list. Since the list can't be empty, the function never fail.

Sourceval rev : 'a t -> 'a t

rev nel reverse the non-empty-list.

Sourceval rev_append : 'a t -> 'a t -> 'a t

rev_append nel1 nelt2 reverses nel1 and concatenates it with nel2. This is equivalent to (Nel.rev nel1) @ nel2.

Sourceval append : 'a t -> 'a t -> 'a t

append nel1 nel2 is the concatenation of nel1 and nel2.

Sourceval concat : 'a t t -> 'a t

concat nel Concatenate a non-empty list of non-empty lists. The elements of the argument are all concatenated together (in the same order) to give the result.

Iterators

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

iter f nel applies f on each element of nel.

Sourceval iteri : (int -> 'a -> unit) -> 'a t -> unit

iteri f nel same as iter but the function is applied to the index of the element as first argument (counting from 0), and the element itself as second argument.

Sourceval map : ('a -> 'b) -> 'a t -> 'b t

map f nel build a new non-empty list applying f on each element of the non-empty list.

Sourceval mapi : (int -> 'a -> 'b) -> 'a t -> 'b t

mapi f nel same as map but the function is applied to the index of the element as first argument (counting from 0), and the element itself as second argument.

Sourceval rev_map : ('a -> 'b) -> 'a t -> 'b t

rev_map f nel is a more efficient way of making Nel.(rev (map f nel)).

Sourceval rev_mapi : (int -> 'a -> 'b) -> 'a t -> 'b t

rev_mapi f nel same as rev_map but the function is applied to the index of the element as first argument (counting from 0), and the element itself as second argument.

Sourceval concat_map : ('a -> 'b t) -> 'a t -> 'b t

concat_map f nel is Nel.concat (Nel.map f nel).

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

flat_map f nel same as concat_map (present for convention reason).

Sourceval concat_mapi : (int -> 'a -> 'b t) -> 'a t -> 'b t

concat_mapi f nel same as concat_map but the function is applied to the index of the element as first argument (counting from 0), and the element itself as second argument.

Sourceval fold_left : ('acc -> 'a -> 'acc) -> 'acc -> 'a t -> 'acc

fold_left reducer default nel is fold_left f init [b1; ...; bn] is f (... (f (f init b1) b2) ...) bn.

Sourceval fold_lefti : (int -> 'acc -> 'a -> 'acc) -> 'acc -> 'a t -> 'acc

fold_lefti f default nel same as fold_left but the function is applied to the index of the element as first argument (counting from 0), and the element itself as second argument.

Sourceval fold_right : ('a -> 'acc -> 'acc) -> 'a t -> 'acc -> 'acc

fold_right f nel default is fold_right f [a1; ...; an] init is f a1 (f a2 (... (f an init) ...)).

Sourceval fold_righti : (int -> 'a -> 'acc -> 'acc) -> 'a t -> 'acc -> 'acc

fold_righti f nel default same as fold_right but the function is applied to the index of the element as first argument (counting from 0), and the element itself as second argument.

Prettty-printers and equality

Sourceval pp : ?pp_sep:(Format.formatter -> unit -> unit) -> (Format.formatter -> 'a -> unit) -> Format.formatter -> 'a t -> unit

Pretty-printer based on Format.pp_print_list.

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

Equality between non-empty lists.

OCaml

Innovation. Community. Security.