#nonempty
A list with at least one element.
NonEmpty a is a first element and a tail, so it cannot be empty. That
makes head, maximum, and minimum total: they return the element
itself rather than an Option. Build one with singleton, fromList,
or the NonEmpty constructor.
Import the module by name, import nonempty, and call nonempty.head
and the rest qualified, since the names overlap with list's.
#NonEmpty
data NonEmpty a
= NonEmpty a (List a)A first element and the rest of the list.
Instances: Mappable, Foldable, Traversable, Semigroup, Eq, Debug, Display
#Construction
#singleton
singleton : a -> NonEmpty a
singleton xA list holding one element.
> toList (singleton 9)
[9]#fromList
fromList : List a -> Option (NonEmpty a)A non-empty list holding the elements of a list, or None when the
list is empty.
The inverse of toList.
> fromList [1, 2, 3]
Some (NonEmpty [1, 2, 3])
> fromList ([] : List Int)
None#Accessing elements
#head
head : NonEmpty a -> aThe first element.
> head (NonEmpty 7 [8, 9])
7#maximum
maximum : Ord a => NonEmpty a -> aThe largest element.
> maximum (NonEmpty 3 [1, 4, 1, 5])
5#minimum
minimum : Ord a => NonEmpty a -> aThe smallest element.
> minimum (NonEmpty 3 [1, 4, 1, 5])
1#Instances
#Mappable NonEmpty
impl Mappable NonEmptymap applies a function to every element.
> toList (map (n => n * 2) (NonEmpty 1 [2, 3]))
[2, 4, 6]#Foldable NonEmpty
impl Foldable NonEmptyThe Foldable methods visit the elements in order, first element
first. toList gives back the plain list.
> toList (NonEmpty 1 [2, 3])
[1, 2, 3]#Semigroup (NonEmpty a)
impl Semigroup (NonEmpty a)++ concatenates two non-empty lists.
> toList (append (NonEmpty 1 [2]) (NonEmpty 3 [4]))
[1, 2, 3, 4]#Display (NonEmpty a)
impl Display (NonEmpty a) requires Display adisplay renders a non-empty list as NonEmpty [x, ...].
> display (NonEmpty 1 [2, 3])
"NonEmpty [1, 2, 3]"