A list that cannot be empty. The head and tail can be accessed without Maybes. Most other list functions are available.
Create a singleton list with the given element.
Create a nonempty list from an ordinary list, failing on the empty list.
Return the head of the list.
Return the tail of the list.
Convert to an ordinary list.
Nonempty lists support equality with the usual (==) operator.
Determine if the nonempty list has exactly one element.
Find the length of the nonempty list.
Determine if an element is present in the nonempty list.
Add another element as the head of the list. Also available infix as
(:::); be sure to add exposing ((:::)) to your import.
Append two nonempty lists together. (++) is not supported.
Pop and discard the head, or do nothing for a singleton list. Useful if you want to exhaust a list but hang on to the last item indefinitely.
Reverse a nonempty list.
Flatten a nonempty list of nonempty lists into a single nonempty list.
Exchange the head element while leaving the tail alone.
Exchange the tail for another while leaving the head alone.
Replace the tail with the empty list while leaving the head alone.
Map a function over a nonempty list.
Map a function over two nonempty lists.
Filter a nonempty list. If all values are filtered out, return the singleton list containing the default value
provided. If any value is retained, the default value is not used. If you want to deal with a Maybe instead, use
toList >> List.filter yourPredicate >> fromList.
filter isEven 0 (Nonempty 7 [2, 5]) == fromElement 2
filter isEven 0 (Nonempty 7 []) == fromElement 0To fold or scan from the right, reverse the list first.
Reduce a nonempty list from the left with a base case.
foldl (++) "" (Nonempty "a" ["b", "c"]) == "cba"Reduce a nonempty list from the left without a base case. As per Elm convention, the first argument is the current element and the second argument is the accumulated value. The function is first invoked on the second element, using the first element as the accumulated value, except for singleton lists in which has the head is returned.
foldl1 (++) (Nonempty "a" ["b", "c"]) == "cba"
foldl1 (++) (fromElement "a") == "a"Like foldl, but keep each intermediate value. For example, scan addition to create the cumulative sum up to each
element. The head of the new nonempty list is always the base case provided, and the length increases by 1.
Like foldl1, but keep each intermediate value. The head and length are not changed.
The nonempty list's elements must support equality (e.g. not functions or signals). Otherwise you will get a runtime error.
Remove adjacent duplicate elements from the nonempty list. For example, 1,2,2,1 becomes 1,2,1.
Remove all duplicate elements from the nonempty list, with the remaining elements ordered by first occurrence. For example, 1,2,2,1 becomes 1,2.
The Nonempty type. If you have both a head and tail, you can construct a nonempty list directly. Usually you'll use one of the many helpers below instead.