mgold/elm-nonempty-list/3.1.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. Otherwise use the 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.

get : Int -> Nonempty a -> a

Get the item at the specified index. The head has index 0. Indices are modulused by the length so out-of-range errors can't happen. This means that negative indices are supported, e.g. -1 to get the last element.

sample : Nonempty a -> Generator a

Create a random generator that returns a value of the nonempty list chosen uniformly at random.

Inspect

Nonempty lists support equality with the usual (==) operator (provided their contents also support equality).

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

Determine if an element is present in the nonempty list.

all : (a -> Bool) -> Nonempty a -> Bool

Determine if all elements satisfy the predicate.

any : (a -> Bool) -> Nonempty a -> Bool

Determine if any elements satisfy the predicate.

Convert

cons : a -> Nonempty a -> Nonempty a

Add another element as the head of the list, pushing the previous head to the tail.

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

Infix cons. Note that you cannot use this for pattern matching. Be sure to add exposing ((:::)) to your import.

4 ::: Nonempty 3 [2,1] == Nonempty 4 [3,2,1]

Note: the infix associativity and precedence are not set for this operator, because doing so can cause conflicts that are hard to work around. In most cases this shouldn't matter.

append : Nonempty a -> Nonempty a -> Nonempty a

Append two nonempty lists together. (++) is not supported.

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.

pop (Nonempty 3 [2,1]) == Nonempty 2 [1]
pop (Nonempty 1 []) == Nonempty 1 []
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.

Map

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

Map a function over a nonempty list.

indexedMap : (Int -> a -> b) -> Nonempty a -> Nonempty b

Same as map but the function is also applied to the index of each element (starting at zero).

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

Map a function over two nonempty lists.

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

Map over an arbitrary number of nonempty lists.

map2 (,) xs ys == map (,) xs  |> andMap ys
head (map (,,) xs |> andMap ys |> andMap zs) == (head xs, head ys, head zs)
concatMap : (a -> Nonempty b) -> Nonempty a -> Nonempty b

Map a given function onto a nonempty list and flatten the resulting nonempty lists. If you're chaining, you can define andThen = flip concatMap.

Filter

filter : (a -> Bool) -> a -> Nonempty a -> Nonempty a

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 0

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"


findMe = 42
minimizeMe n = abs (n-findMe)
nearest = foldl1 (\a b -> if minimizeMe a < minimizeMe b then a else b) (Nonempty 10 [20,30,40,50,60])
nearest == 40
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.

scanl (++) "" (Nonempty "a" ["b", "c"]) == Nonempty "" ["a","ba","cba"]
scanl1 : (a -> a -> a) -> Nonempty a -> Nonempty a

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

This example starts with the number of ways to roll exactly index i on two six-sided dice (the probability density function), and turns it into the number of ways to roll at least i (the cumulative density function).

dicePDF = Nonempty 0 [0,1,2,3,4,5,6,5,4,3,2,1]
diceCDF = scanl1 (+) dicePDF
diceCDF == Nonempty 0 [0,1,3,6,10,15,21,26,30,33,35,36]

Zipping

zip : Nonempty a -> Nonempty b -> Nonempty ( a, b )

Take two nonempty lists and compose them into a nonempty list of corresponding pairs.

The length of the new list equals the length of the smallest list given.

unzip : Nonempty ( a, b ) -> ( Nonempty a, Nonempty b )

Decompose a nonempty list of tuples into a tuple of nonempty lists.

Sort

sort : Nonempty comparable -> Nonempty comparable

Sort a nonempty list of comparable things, lowest to highest.

sortBy : (a -> comparable) -> Nonempty a -> Nonempty a

Sort a nonempty list of things by a derived property.

sortWith : (a -> a -> Order) -> Nonempty a -> Nonempty a

Sort a nonempty list of things by a custom comparison function.

Deduplicate

The nonempty list's elements must support equality (e.g. not functions). Otherwise you will get a runtime error.

dedup : Nonempty a -> Nonempty a

Remove adjacent duplicate elements from the nonempty list.

dedup (Nonempty 1 [2, 2, 1]) == Nonempty 1 [2, 1]
uniq : Nonempty a -> Nonempty a

Remove all duplicate elements from the nonempty list, with the remaining elements ordered by first occurrence.

uniq (Nonempty 1 [2, 2, 1]) == Nonempty 1 [2]