danielnarey/elm-toolkit/1.0.2

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.

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)"

Error Handling with Chained Results

thenTry : (a -> Result x b) -> Result x a -> Result x b

Same as Result.andThen, but flips the order of the arguments, allowing for cleaner syntax when used with the |> operator.

rawString
  |> toInt
  |> thenTry toValidMonth

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

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

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

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

Return the first value of a 3-tuple

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

Return the second value of a 3-tuple

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

Return the third value of a 3-tuple

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

Return the first value of a 4-tuple

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

Return the second value of a 4-tuple

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

Return the third value of a 4-tuple

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

Return the fourth value of 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 ) -> (a -> b -> c -> d) -> d

uncurry with 3 parameters

uncurry4 : ( a, b, c, d ) -> (a -> b -> c -> d -> e) -> 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.