NonEmpty
Import NonEmpty when a sequence must contain at least one value.
Type and constructor
NonEmpty
The public constructor stores the guaranteed head followed by an ordinary list tail. The same name refers to the type and its constructor.
NonEmpty
The constructor is available to patterns.
Construction and conversion
singleton
fromList
Returns Nothing for []; otherwise stores the list head and tail without
traversing them. This is O(1).
toList
prepend
Adds a new first value in O(1).
appendList
Appends an ordinary list after the non-empty sequence. This is O(n) in the
original sequence tail.
Access
head
Total and O(1).
tail
The result may be empty. This is O(1).
last
Returns the final value. The function is total and O(n).
length
Returns the number of values, which is always at least one. This is O(n).
Transforming and folding
map
Transforms every value in order and preserves non-emptiness. This is O(n)
plus callback work.
foldLeft
Combines values from head to last, beginning with the supplied accumulator.
This is O(n) plus callback work.
foldRight
Combines values from last to head, beginning with the supplied terminal value.
This is O(n) plus callback work.
Generic methods
Importing NonEmpty supplies element-based Equatable, Mappable, Reducible, and Combinable instances. Mapping and concatenation preserve the nonempty invariant.