danielnarey/elm-toolkit/4.4.0

NOTE: this package is not compatible with Elm 0.19.1

Toolkit.Helpers

Some generic helper functions for type conversion, error handling, and working with lists, tuples, and functions

This is my personal library of helper functions for writing clean, unidirectional, semantically pleasing Elm code. I've included all of these functions in one module so that I can easily import them into other projects.

Rounding Numbers

roundTo : Int -> Float -> Float

Round a Float to a given number of decimal places

pi |> roundTo 2      --> 3.14
pi |> roundTo 0      --> 3
1234 |> roundTo -2   --> 1200

String-to-Bool Conversion

toBool : String -> Result String Bool

Convert a boolean string to a Bool, ignoring case

toBool "true"     --> Ok True
toBool "True"     --> Ok True
toBool "false"    --> Ok False
toBool "FALSE"    --> Ok False
toBool "blah"     --> Err "String argument must be 'true' or 'false' (case ignored)"

Test Functions

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.

Equivalent to List.member with the arguments flipped

isInRange : ( comparable, comparable ) -> comparable -> Bool

Given a pair of values defining an interval and a test value, returns True if the test value falls within the interval, including its endpoints

isBetween : ( comparable, comparable ) -> comparable -> Bool

Given a pair of values defining an interval and a test value, returns True if the test value falls strictly between the endpoints of the interval, such that a test value equal to one of the endpoints will return False

Error Handling with Multiple Maybe Values

maybe2Tuple : ( Maybe a, Maybe b ) -> Maybe ( a, b )

Given a 2-tuple of Maybe values, if both values are defined, return Just the 2-tuple of values; otherwise, return Nothing

maybe2Tuple (Just 1, Just 2)    --> Just (1,2)
maybe2Tuple (Just 1, Nothing)    --> Nothing
maybe3Tuple : ( Maybe a, Maybe b, Maybe c ) -> Maybe ( a, b, c )

Given a 3-tuple of Maybe values, if all three values are defined, return Just the 3-tuple of values; otherwise, return Nothing

maybe4Tuple : ( Maybe a, Maybe b, Maybe c, Maybe d ) -> Maybe ( a, b, c, d )

Given a 4-tuple of Maybe values, if all four values are defined, return Just the 4-tuple of values; otherwise, return Nothing

maybeList : List (Maybe a) -> Maybe (List a)

Given a list of Maybe values, if all values are defined, return Just the list of values; otherwise, return Nothing. When passed an empty list, returns Just an empty list.

maybeList [Just 1, Just 2]    --> Just [1,2]
maybeList [Just 1, Nothing]   --> Nothing
maybeList []                  --> Just []

Error Handling with Multiple Result Values

result2Tuple : x -> ( Result x a, Result x b ) -> Result x ( a, b )

Given a 2-tuple of Result values, if both values are Ok, return an Ok result containing the 2-tuple of values; otherwise, return an Err value.

result2Tuple "ERROR" (Ok 1, Ok 2)       --> Ok (1,2)
result2Tuple "ERROR" (Ok 1, Err "..")   --> Err "ERROR"
result3Tuple : x -> ( Result x a, Result x b, Result x c ) -> Result x ( a, b, c )

Given a 3-tuple of Result values, if all three values are Ok, return an Ok result containing the 3-tuple of values; otherwise, return an Err value.

x
-> ( Result x a, Result x b, Result x c, Result x d )
-> Result x ( a, b, c, d )

Given a 4-tuple of Result values, if all three values are Ok, return an Ok result containing the 4-tuple of values; otherwise, return an Err value.

resultList : x -> List (Result x a) -> Result x (List a)

Given a list of Result values, if all values are Ok, return an Ok result containing the list of values; otherwise, return an error message. When passed an empty list, returns Ok [].

resultList "ERROR" [Ok 1, Ok 2]       --> Ok [1,2]
resultList "ERROR" [Ok 1, Err ".."]   --> Err "ERROR"
resultList []                         --> Ok []

Value-to-List and Value-From-List Conversions

wrapList : a -> List a

Return a one-item list containing the argument

wrapList ("key", "value")


--> [ ("key", "value") ]
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

getNth 0 [1, 3, 9, 27]    --> Just 1
getNth 3 [1, 3, 9, 27]    --> Just 27
getNth 4 [1, 3, 9, 27]    --> Nothing
getNth -1 [1, 3, 9, 27]   --> Nothing

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

take2Tuple [1,2]    --> Just (1,2)
take2Tuple [1,2,3]  --> Just (1,2)
take2Tuple [1]      --> 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

list2Tuple : ( a, a ) -> List a

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

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

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

list4Tuple : ( 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

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

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

unzip4 : ( 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

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

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

zip ([0,17,1337], [True,False,True])


--> [(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

Getting Values from Tuples

first3 : ( a, b, c ) -> a

Return the first value of a 3-tuple

second3 : ( a, b, c ) -> b

Return the second value of a 3-tuple

third3 : ( a, b, c ) -> c

Return the third value of a 3-tuple

first4 : ( a, b, c, d ) -> a

Return the first value of a 4-tuple

second4 : ( a, b, c, d ) -> b

Return the second value of a 4-tuple

third4 : ( a, b, c, d ) -> c

Return the third value of a 4-tuple

fourth4 : ( a, b, c, d ) -> d

Return the fourth value of a 4-tuple

Mapping Functions to Tuples

map2Tuple : (a -> b) -> ( a, a ) -> ( b, b )

Apply a function to both values in a 2-tuple and return the results as a 2-tuple

map3Tuple : (a -> b) -> ( a, a, a ) -> ( b, b, b )

Apply a function to all 3 values in a 3-tuple and return the results as a 3-tuple

map4Tuple : (a -> b) -> ( a, a, a, a ) -> ( b, b, b, b )

Apply a function to all 4 values in a 4-tuple and return the results as a 4-tuple

Currying and Uncurrying

curry3 : (( a, b, c ) -> d) -> a -> b -> c -> d

curry with 3 parameters

curry4 : (( a, b, c, d ) -> e) -> a -> b -> c -> d -> e

curry with 4 parameters

uncurry3 : (a -> b -> c -> d) -> ( a, b, c ) -> d

uncurry with 3 parameters

uncurry4 : (a -> b -> c -> d -> e) -> ( a, b, c, d ) -> e

uncurry with 4 parameters

Applying Multiple Functions to Data

apply2 : ( a -> b, a -> c ) -> a -> ( b, c )

Given a tuple containing two functions and a value accepted by both functions, return a tuple containing the two results

apply3 : ( a -> b, a -> c, a -> d ) -> a -> ( b, c, d )

Given a tuple containing three functions and a value accepted by all three functions, return a tuple containing the three results

apply4 : ( a -> b, a -> c, a -> d, a -> e ) -> a -> ( b, c, d, e )

Given a tuple containing four functions and a value accepted by all four functions, return a tuple containing the four results

applyList : List (a -> b) -> a -> List b

Given a list containing any number of functions and a value accepted by every function in the list, return a list containing all of the results. Note that to use applyList, all of the results must be of the same type, which is not the case for the apply functions that return tuples.