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.
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)"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
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
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
Result FunctionsApply a function that returns a Result value, with the initial value as
the default (equivalent to f x |> Result.withDefault x). Note that the type
returned in an Ok result must match the type of the initial value.
Maybe ValuesGiven 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) --> NothingGiven a 3-tuple of Maybe values, if all three values are defined, return
Just the 3-tuple of values; otherwise, return Nothing
Given a 4-tuple of Maybe values, if all four values are defined, return
Just the 4-tuple of values; otherwise, return Nothing
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 []Result ValuesGiven 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"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.
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.
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 []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] --> NothingGiven a list of values, returns the unique values as a list sorted from highest to lowest
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] --> NothingReturns the first three items in a list as a 3-tuple, or Nothing if the
list contains fewer than three items
Returns the first four items in a list as a 4-tuple, or Nothing if the
list contains fewer than four items
Given a 2-tuple where both values are of the same type, return a list containing those values
Given a 3-tuple where all three values are of the same type, return a list containing those values
Given a 4-tuple where all four values are of the same type, return a list containing those values
Convert a 3-tuple of lists to a list of 3-tuples (see List.unzip)
Convert a 4-tuple of lists to a list of 4-tuples
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)]Convert a 3-tuple of lists to a list of 3-tuples
Convert a 4-tuple of lists to a list of 4-tuples
Return the first value of a 3-tuple
Return the second value of a 3-tuple
Return the third value of a 3-tuple
Return the first value of a 4-tuple
Return the second value of a 4-tuple
Return the third value of a 4-tuple
Return the fourth value of a 4-tuple
Apply a function to both values in a 2-tuple and return the results as a 2-tuple
Apply a function to all 3 values in a 3-tuple and return the results as a 3-tuple
Apply a function to all 4 values in a 4-tuple and return the results as a 4-tuple
Given a tuple containing two functions and a value accepted by both functions, return a tuple containing the two results
Given a tuple containing three functions and a value accepted by all three functions, return a tuple containing the three results
Given a tuple containing four functions and a value accepted by all four functions, return a tuple containing the four results
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.
Round a
Floatto a given number of decimal places