Extensions to the core List
library.
chunk : Basics.Int -> List a -> List (List a)
Split a list into chunks of length n
.
Be aware that the last sub-list may be smaller than n
-items long.
For example chunk 3 [1..10] => [[1,2,3], [4,5,6], [7,8,9], [10]]
mergeBy : (a -> comparable) -> List a -> List a -> List a
Merge two lists. The first argument is a function which returns the unique ID of each element. Where an element appears more than once, the last won wins.
singleton : a -> List a
Wrap a single item into a List
.
maybeSingleton : Maybe a -> List a
Wrap a maybe item into a List
. If the item is Nothing
, the List
is empty.
firstMatch : (a -> Basics.Bool) -> List a -> Maybe a
Find the first element in the List
that matches the given predicate.
rest : List a -> List a
Like List.tail, but if the list is empty it returns an empty list rather than Nothing
.
unique : List comparable -> List comparable
Return a new list with duplicates removed. Order is preserved.
exactlyOne : List a -> Result String a
Extract the first item from the List
, demanding that there be exactly one element.
For example, Json.Decode.customDecoder string exactlyOne
creates a
decoder that expects a list of strings, where there is only one
element in the List
.
If you think that's weird, you haven't seen enough real-world JSON. ;-)
maximumBy : (a -> comparable) -> List a -> Maybe a
Like List.maximum
, but it works on non-comparable types by taking a custom function.
minimumBy : (a -> comparable) -> List a -> Maybe a
Like List.minimum
, but it works on non-comparable types by taking a custom function.
unfold : (b -> Maybe ( b, a )) -> b -> List a
Generate a List
from a function and a seed value.
I feel sorry for unfold
- it doesn't get nearly as much love as
map
and fold
, despite being in the same family.