fabiommendes / elm-non-empty-list / NonEmpty

A list with at least one element

Types


type NonEmpty a
    = NonEmpty a (List a)

A list of a`s with at least 1 element.

Construction

create : a -> List a -> NonEmpty a

Create list from first element and tail

cons : a -> NonEmpty a -> NonEmpty a

Add an element to the front of a list.

singleton : a -> NonEmpty a

Create a list with only one element

repeat : Basics.Int -> a -> NonEmpty a

Create a list with n (at least 2) copies of a value

range : Basics.Int -> Basics.Int -> NonEmpty Basics.Int

Create list from a range of values.

The first value is guaranteed to appear in the list.

generate : (a -> a -> b) -> NonEmpty a -> List b

Generate a new List from iterating over pairs of elements in the NonEmpty list.

generate Tuple.pair (range 1 5)
    == [ ( 1, 2 ), ( 2, 3 ), ( 3, 4 ), ( 4, 5 ) ]

fromList : List a -> Maybe (NonEmpty a)

Try to create sequence from a list with at least two elements

withDefault : a -> List a -> NonEmpty a

Create NonEmpty from list, returning a singleton with the given default if list is empty.

withExample : NonEmpty a -> List a -> NonEmpty a

Create NonEmpty from list, returning example if list is empty.

Transform

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

Apply a function to every element of a list.

indexedMap : (Basics.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).

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

Reduce a list from the left.

foldr : (a -> b -> b) -> b -> NonEmpty a -> b

Reduce a list from the right.

reduce : (a -> a -> a) -> NonEmpty a -> a

Reduce a list from left using operator.

This version of foldl does not require an accumulator since the list is guaranteed to not being empty

Filtering

filter : (a -> Basics.Bool) -> NonEmpty a -> List a

Keep elements that satisfy the test.

Return a list, since result might be empty.

filterMap : (a -> Maybe b) -> NonEmpty a -> List b

Filter out certain values.

Return a list, since result might be empty.

Combine

append : NonEmpty a -> NonEmpty a -> NonEmpty a

Put two lists together.

append (range 1 3) (range 4 5) == range 1 5

concat : NonEmpty (NonEmpty a) -> NonEmpty a

Concatenate a bunch of lists into a single list

concatMap : (a -> NonEmpty b) -> NonEmpty a -> NonEmpty b

Map a given function onto a list and flatten the resulting lists.

concatMap f xs == concat (map f xs)

intersperse : a -> NonEmpty a -> NonEmpty a

Places the given value between all members of the given list.

map2 : (a -> b -> r) -> NonEmpty a -> NonEmpty b -> NonEmpty r

Apply a two argument function pairwise to elements of both lists

map3 : (a -> b -> c -> r) -> NonEmpty a -> NonEmpty b -> NonEmpty c -> NonEmpty r

Apply a 3 argument function tuples of elements from all lists

map4 : (a -> b -> c -> d -> r) -> NonEmpty a -> NonEmpty b -> NonEmpty c -> NonEmpty d -> NonEmpty r

Apply a 4 argument function tuples of elements from all lists

map5 : (a -> b -> c -> d -> e -> r) -> NonEmpty a -> NonEmpty b -> NonEmpty c -> NonEmpty d -> NonEmpty e -> NonEmpty r

Apply a 5 argument function tuples of elements from all lists

Sort

sort : NonEmpty comparable -> NonEmpty comparable

Sort values from lowest to highest

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

Sort values by a derived property.

sortWith : (a -> a -> Basics.Order) -> NonEmpty a -> NonEmpty a

Sort values with a custom comparison function.

This is also the most general sort function, allowing you to define any other: sort == sortWith compare

Deconstruct

isEmpty : NonEmpty a -> Basics.Bool

Always return False, since NonEmpty is never empty :)

head : NonEmpty a -> a

Return the first element

head (range 1 5) == 1

tail : NonEmpty a -> List a

Extract the rest of the list.

head (range 1 5) == [ 2, 3, 4, 5 ]

take : Basics.Int -> NonEmpty a -> NonEmpty a

Take (at most) the first n elements.

The number of elements n must be n >= 1, otherwise it returns the singleton list.

take 2 (range 1 5)
    |> toList
    == [ 1, 2 ]

WARNING: differently from lists, append (take n lst) (drop n lst) do not always recover the original list.

drop : Basics.Int -> NonEmpty a -> NonEmpty a

Drop (at most) the first n elements.

The resulting list will have at least one element

drop 2 (range 1 5)
    |> toList
    == [ 3, 4, 5 ]

partition : (a -> Basics.Bool) -> NonEmpty a -> ( List a, List a )

Partition a list based on some test.

The first list contains all values that satisfy the test, and the second list contains all the value that do not.

uncons : NonEmpty a -> ( a, List a )

Deconstruct list into the head and tail parts

uncons (range 1 5)
    == ( 1, [ 2, 3, 4, 5 ] )

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

Decompose a list of tuples into a tuple of lists.

unzip (create ( "a", 1 ) [ ( "b", 2 ), ( "c", 3 ) ])
    == ( NonEmpty "a" [ "b", "c" ], NonEmpty 1 [ 2, 3 ] )

Utilities

length : NonEmpty a -> Basics.Int

Determine the length of a list.

length (range 1 5) == 5

all : (a -> Basics.Bool) -> NonEmpty a -> Basics.Bool

Determine if all elements satisfy some test.

all (\x -> modBy 2 == 0) (range 1 5) == False

any : (a -> Basics.Bool) -> NonEmpty a -> Basics.Bool

Determine if any elements satisfy some test.

any (\x -> modBy 2 == 0) (range 1 5) == True

minimum : NonEmpty comparable -> comparable

Find the minimum element in a non-empty list.

minimum (range 1 5) == 5

maximum : NonEmpty comparable -> comparable

Find the maximum element in a non-empty list.

maximum (range 1 5) == 5

sum : NonEmpty number -> number

Get the sum of the list elements.

sum (range 1 5) == 1 + 2 + 3 + 4 + 5

product : NonEmpty number -> number

Get the product of the list elements.

member : a -> NonEmpty a -> Basics.Bool

Figure out whether a list contains a value.

member 42 (range 1 3) == False

reverse : NonEmpty a -> NonEmpty a

Reverse a list.

reverse (range 1 3)
    |> toList
    == [ 5, 4, 3, 2, 1 ]

toList : NonEmpty a -> List a

Convert data to list

Indexed operations

getAt : Basics.Int -> NonEmpty a -> Maybe a

Returns Just the element at the given index in the list, or Nothing if the index is out of range.

getAt 1 (range 1 5) == Just 2

insertAt : Basics.Int -> a -> NonEmpty a -> NonEmpty a

Insert element at given position.

insertAt 2 42 negate (range 1 5)
    |> toList
    == [ 1, 2, 42, 3, 4, 5 ]

removeAt : Basics.Int -> NonEmpty a -> NonEmpty a

Remove the element at an index from a list.

Return the original list if the index is out of range or if the resulting list would be empty.

removeAt 2 negate (range 1 5)
    |> toList
    == [ 1, 2, 4, 5 ]

updateAt : Basics.Int -> (a -> a) -> NonEmpty a -> NonEmpty a

Update element at the given index

updateAt 2 negate (range 1 5)
    |> toList
    == [ 1, 2, -3, 4, 5 ]