Std.NonEmpty (Cure Standard Library v0.37.2)

View Source

A list that is guaranteed to hold at least one element.

A NonEmpty(T) is a first element (its head) followed by an ordinary List(T) tail. Because the head is always present, head/tail are total — there is no "empty non-empty list" to guard against, so callers never have to handle a missing-first-element case.

Go the other way with from_list, which returns an Option because an ordinary list might be empty.

Examples

use Std.NonEmpty

head(singleton(7))                       # => 7
to_list(push(0, singleton(1)))           # => [0, 1]
from_list([1, 2, 3])                     # => Some(NonEmpty(1, [2, 3]))
from_list([])                            # => None()

A first element (head) and everything after it (tail, possibly empty).

Summary

Functions

Build a NonEmpty(T) from an ordinary list — Some when it has a head, None when the list is empty.

The guaranteed-present first element.

Apply f to every element, preserving non-emptiness.

Prepend x, which becomes the new head.

A non-empty list of exactly one element.

Everything after the head (may be empty).

Forget the non-emptiness guarantee: view as an ordinary List(T).

Types

enum NonEmpty

 enum NonEmpty
   NonEmpty

Functions

from_list(xs: List(t)) -> Option(NonEmpty(t))

 fn from_list(xs: List(t)) -> Option(NonEmpty(t))

Build a NonEmpty(T) from an ordinary list — Some when it has a head, None when the list is empty.

head(ne: NonEmpty(t)) -> t

 fn head(ne: NonEmpty(t)) -> t

The guaranteed-present first element.

map(ne: NonEmpty(t), f: Function(t, u)) -> NonEmpty(u)

 fn map(ne: NonEmpty(t), f: Function(t, u)) -> NonEmpty(u)

Apply f to every element, preserving non-emptiness.

push(x: t, ne: NonEmpty(t)) -> NonEmpty(t)

 fn push(x: t, ne: NonEmpty(t)) -> NonEmpty(t)

Prepend x, which becomes the new head.

singleton(x: t) -> NonEmpty(t)

 fn singleton(x: t) -> NonEmpty(t)

A non-empty list of exactly one element.

tail(ne: NonEmpty(t)) -> List(t)

 fn tail(ne: NonEmpty(t)) -> List(t)

Everything after the head (may be empty).

to_list(ne: NonEmpty(t)) -> List(t)

 fn to_list(ne: NonEmpty(t)) -> List(t)

Forget the non-emptiness guarantee: view as an ordinary List(T).