mgold/elm-nonempty-list/1.3.0

NOTE: this package is not compatible with Elm 0.19.1

List.Nonempty

A list that cannot be empty. The head and tail can be accessed without Maybes. Most other list functions are available.

Definition

type Nonempty a
= Nonempty a (List a)

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.

Create

fromElement : a -> Nonempty a

Create a singleton list with the given element.

fromList : List a -> Maybe (Nonempty a)

Create a nonempty list from an ordinary list, failing on the empty list.

Access

tail : Nonempty a -> List a

Return the tail of the list.

toList : Nonempty a -> List a

Convert to an ordinary list.

Inspect

Nonempty lists support equality with the usual (==) operator.

isSingleton : Nonempty a -> Bool

Determine if the nonempty list has exactly one element.

length : Nonempty a -> Int

Find the length of the nonempty list.

member : a -> Nonempty a -> Bool

Convert

cons : a -> Nonempty a -> Nonempty a

Add another element as the head of the list. Also available infix as (:::); be sure to add exposing ((:::)) to your import.

pop : Nonempty a -> Nonempty a

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 : Nonempty a -> Nonempty a

Reverse a nonempty list.

concat : Nonempty (Nonempty a) -> Nonempty a

Flatten a nonempty list of nonempty lists into a single nonempty list.

Swap

replaceHead : a -> Nonempty a -> Nonempty a

Exchange the head element while leaving the tail alone.

replaceTail : List a -> Nonempty a -> Nonempty a

Exchange the tail for another while leaving the head alone.

dropTail : Nonempty a -> Nonempty a

Replace the tail with the empty list while leaving the head alone.

Fold

To fold or scan from the right, reverse the list first.

foldl : (a -> b -> b) -> b -> Nonempty a -> b

Reduce a nonempty list from the left with a base case.

foldl (++) "" (Nonempty "a" ["b", "c"]) == "cba"
foldl1 : (a -> a -> a) -> Nonempty a -> a

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"
scanl : (a -> b -> b) -> b -> Nonempty a -> Nonempty b

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.

scanl1 : (a -> a -> a) -> Nonempty a -> Nonempty a

Like foldl1, but keep each intermediate value. The head and length are not changed.

Map

map : (a -> b) -> Nonempty a -> Nonempty b

Map a function over a nonempty list.

map2 : (a -> b -> c) -> Nonempty a -> Nonempty b -> Nonempty c

Map a function over two nonempty lists.

Deduplicate

The elements must support equality or you will get a runtime error.

dedup : Nonempty a -> Nonempty a

Remove adjacent duplicate elements from the nonempty list. For example, 1,2,2,1 becomes 1,2,1.

uniq : Nonempty a -> Nonempty a

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.