danielnarey/elm-toolkit/6.1.1

NOTE: this package is not compatible with Elm 0.19.1

Toolkit.List

Helpers for working with lists

General

isOneOf : List a -> a -> Bool

Given a list and a test value, returns True if the list contains a value equal to the test value.

"apple"
  |> Toolkit.List.isOneOf
    [ "apple"
    , "banana"
    , "cherry"
    ]


--> True

Equivalent to List.member with the arguments flipped

getNth : Int -> List a -> Maybe a

Get the value at the nth place of a list without converting the list to an array; returns Nothing if the list contains fewer than n + 1 items, or if n is negative

[1, 3, 9, 27]
  |> Toolkit.List.getNth 0


--> Just 1


[1, 3, 9, 27]
  |> Toolkit.List.getNth 3


--> Just 27


[1, 3, 9, 27]
  |> Toolkit.List.getNth 4


--> Nothing


[1, 3, 9, 27]
  |> Toolkit.List.getNth -1


--> Nothing
unique : List comparable -> List comparable

Given a list of values, returns the unique values as a list sorted from highest to lowest

[ "banana"
, "cherry"
, "apple"
, "apple"
]
  |> Toolkit.List.unique


--> ["apple", "banana", "cherry"]

List-Tuple conversions

take2Tuple : List a -> Maybe ( a, a )

Returns the first two items in a list as a 2-tuple, or Nothing if the list contains fewer than two items

[1,2]
  |> Toolkit.List.take2Tuple


--> Just (1,2)


[1,2,3]
  |> Toolkit.List.take2Tuple


--> Just (1,2)


[1]
  |> Toolkit.List.take2Tuple


--> Nothing
take3Tuple : List a -> Maybe ( a, a, a )

Returns the first three items in a list as a 3-tuple, or Nothing if the list contains fewer than three items

take4Tuple : List a -> Maybe ( a, a, a, a )

Returns the first four items in a list as a 4-tuple, or Nothing if the list contains fewer than four items

from2Tuple : ( a, a ) -> List a

Given a 2-tuple where both values are of the same type, return a list containing those values

(1,2)
  |> Toolkit.List.from2Tuple


--> Just [1,2]
from3Tuple : ( a, a, a ) -> List a

Given a 3-tuple where all three values are of the same type, return a list containing those values

from4Tuple : ( a, a, a, a ) -> List a

Given a 4-tuple where all four values are of the same type, return a list containing those values

Zipping and unzipping

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

Convert a 2-tuple of lists to a list of 2-tuples

( [ 0
  , 17
  , 1337
  ]


, [ True
  , False
  , True
  ]


)
  |> Toolkit.List.zip


--> [(0, True), (17, False), (1337, True)]
zip3 : ( List a, List b, List c ) -> List ( a, b, c )

Convert a 3-tuple of lists to a list of 3-tuples

zip4 : ( List a, List b, List c, List d ) -> List ( a, b, c, d )

Convert a 4-tuple of lists to a list of 4-tuples

unzip3 : List ( a, b, c ) -> ( List a, List b, List c )

Convert a 3-tuple of lists to a list of 3-tuples (see List.unzip)

unzip4 : List ( a, b, c, d ) -> ( List a, List b, List c, List d )

Convert a 4-tuple of lists to a list of 4-tuples