package nel

  1. Overview
  2. Docs

Module NelSource

Describes a non-empty list (usually expressed as nel for Non Empty List) (i.e. one containing at least one element). Most of the documentation was mostly done by OCaml Standard Library.

Types

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

The type isn’t very original and overloads :: (cons) to represent a pair consisting of an element and a (regular) list. As the sum does not contain Nil, it is impossible to construct a non-empty list.

Building non-empty list

A set of functions for constructing non-empty lists.

Sourceval make : 'a -> 'a list -> 'a t

make x xs constructs a non-empty list with a head and a tail.

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

init len f is [f 0; f 1; ...; f (len-1)], evaluated left to right. Return None if the given len is < 1.

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

Same of init but raises Invalid_argument "Nel.init_exn" if the given len is < 1.

Sourceval singleton : 'a -> 'a t

singleton x constructs a non-empty list with just one element, x.

Sourceval return : 'a -> 'a t

return x is singleton x. See singleton.

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

from_list tl attempts to convert a list into a non-empty list. Return None if the given list tl is empty.

Sourceval from_list_exn : 'a list -> 'a t

from_list_exn tl attempts to convert a list into a non-empty list (like from_list but raises Invalid_argument "Nel.from_list_exn" if the given list tl is empty).

Facts

Sourceval length : 'a t -> int

length nel return the length (number of elements) of the given nel. Since it is a non-empty list, length nel always returns a number >= 1.

Sourceval is_singleton : 'a t -> bool

is_singleton nel return true if the non-empty list holds only one element. false otherwise.

Sourceval hd : 'a t -> 'a

hd nel return the first element of the given list nel. Since the list cannot be empty, the function is total.

Sourceval tl : 'a t -> 'a list

tl nel return the given list nel without the first element. Since the list can be a singleton, the tail is returned as a list (and not a non-empty list).

Sourceval last : 'a t -> 'a

last nel retun the latest element of the given nel. Since the list cannot be empty, the function is total.

Sourceval nth : 'a t -> int -> 'a option

Return the n-th element of the given list. The first element (head of the list) is at position 0. If the result does not exits, it return None.

Sourceval nth_exn : 'a t -> int -> 'a

Same of nth but raises Invalid_argument "Nel.nth_exn" if the given n is < 1 and Failure if the index does not exists.

Manipulation

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

cons x nel is x :: nel.

Sourceval rev : 'a t -> 'a t

Non Empty List reversal.

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

append nel1 nel2 appends nel2 to nel1.

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

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

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

Concatenate a list of lists. The elements of the argument are all concatenated together (in the same order) to give the result. Not tail-recursive (length of the argument + length of the longest sub-list).

Sourceval flatten : 'a t t -> 'a t

An alias for concat.

Iteration

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

iter f nel apply f on every element of nel.

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

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 produce a new non-empty list applying f on every element of nel.

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

See 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

Same as rev (map f l) but more efficient.

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

See 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 concat (map f nel).

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

bind nel f is concat_map f nel. See concat_map.

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

See 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 f init [b1; ...; bn] is f (... (f (f init b1) b2) ...) bn.

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

fold_right f [a1; ...; an] init is f a1 (f a2 (... (f an init) ...)). Not tail-recursive.

Sourceval reduce : ('a -> 'a -> 'a) -> 'a t -> 'a

reduce f nel is the semigroup reduction over a non-empty list.

Scanning

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

for_all pred nel return true if all element of the given nel satisfy pred. false otherwise.

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

for_some pred nel return true if at least one element of the given nel satisfy pred. false otherwise.

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

exists pred nel is for_some pred nel, see for_some.

Sourceval mem : 'a -> 'a t -> bool

mem x nel is for_some ((=) x) nel.

Sourceval memq : 'a -> 'a t -> bool

memq x nel is for_some ((==) x) nel.

Searching

Sourceval find : ('a -> bool) -> 'a t -> 'a option

find pred nel returns the first element of the given nel that satisfy the given predicate pred.

Sourceval findi : (int -> 'a -> bool) -> 'a t -> 'a option

See find 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 find_index : ('a -> bool) -> 'a t -> int option

find_index pred nel returns Some i, where i is the index of the first element of the non-empty list nel that satisfies pred x, if there is such an element. It returns None if there is no such element.

Sourceval find_map : ('a -> 'b option) -> 'a t -> 'b option

find_map pred nel applies pred to the elements of nel in order, and returns the first result of the form Some v, or None if none exist.

Sourceval find_mapi : (int -> 'a -> 'b option) -> 'a t -> 'b option

See find_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.

Comparison

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

Equality between non-empty lists.

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

Comparison between non-empty lists. Use the same lexicographic heuristics than Stdlib.List.compare.

Sourceval compare_lengths : 'a t -> 'a t -> int

Compare the lengths of two lists. compare_lengths l1 l2 is equivalent to compare (length l1) (length l2), except that the computation stops after reaching the end of the shortest list.

Sourceval compare_length_with : 'a t -> int -> int

Compare the length of a list to an integer. compare_length_with l len is equivalent to compare (length l) len, except that the computation stops after at most len iterations on the list.

Conversion

Sourceval to_list : 'a t -> 'a list

to_list nel convert the given nel to a regular OCaml list.