mgold/elm-nonempty-list/1.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.

Definition

type Nonempty a
= Nonempty a (List a)

The Nonempty type. Unless you have both a head and tail, you'll normally use the provided interface.

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.

Retrieve values

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.

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.

replaceHead : a -> Nonempty a -> Nonempty a

Exchange the head element while leaving the tail alone.

dropTail : Nonempty a -> Nonempty a

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

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

Map a function over the nonempty list.