jsuder-xx/elm-ra/1.0.0

NOTE: the latest version is 1.1.1

Ra

Ra supports Pointfree style in Elm by providing various combinators to work with Predicates, Relations, Math, Functions, and Flow Control.

Pointfree can be abused, but when it is appropriate pointfree style is

  • More Readable & Literate. The declaration of the lambda with an argument and repetitious application of the argument can obscure the meaning.
  • Safer. It is impossible to inadvertently pass the wrong value because functions are only composed and not applied or called.el

Predicate Combinators

allPass : List (Predicate a) -> a -> Bool

Takes a list of predicates and returns a predicate that returns true for a given list of arguments if every one of the provided predicates is satisfied by those arguments.

isAmazing : Person -> Bool
isAmazing = allPass [isKind, isCreative, isHardWorking, isIntelligent]
anyPass : List (Predicate a) -> a -> Bool

Takes a list of predicates and returns a predicate that returns true for a given list of arguments if at least one of the provided predicates is satisfied by those arguments.

hasAtLeastOneGoodQuality : Person -> Bool
hasAtLeastOneGoodQuality = anyPass [Person.isKind, Person.isCreative, Person.isHardWorking, Person.isIntelligent]
both : Predicate a -> Predicate a -> a -> Bool

Return a new predicate that is true if both of the two predicates return true. Note that this is short-circuited, meaning that the second predicate will not be invoked if the first returns false.

either : Predicate a -> Predicate a -> a -> Bool

Return a new predicate that is true if either of the two predicates return true. Note that this is short-circuited, meaning that the second predicate will not be invoked if the first returns true.

complement : Predicate a -> Predicate a

Invert (negate, complement) a predicate i.e if the predicate would have return True for some a it will now return False and vice versa.

false : Predicate a

A predicate that always returns false.

true : Predicate a

A predicate that always returns true.

Chain and Composition Friendly Relations

lessThan : comparable -> comparable -> Bool

Designed for use in pipelines or currying. Returns a predicate that tests if the value is less than the first value given.

20 |> lessThan 10 -- False


20 |> lessThan 30 -- True
lessThanEqualTo : comparable -> comparable -> Bool

Designed for currying. Returns a predicate that tests if the value is less than or equal to the first value given.

20 |> lessThanEqualTo 10 -- False


20 |> lessThanEqualTo 20 -- True


[5, 10, 11, 20]
|> List.map (lessThanEqualto 10) -- [True, True, False, False]
greaterThan : comparable -> comparable -> Bool

Designed for currying. Returns a predicate that tests if the value is greater than the first value given.

20 |> greaterThan 10 -- True


20 |> greaterThan 20 -- False


[5, 10, 11, 20]
|> List.map (greaterThan 10) -- [False, False, True, True]
greaterThanEqualTo : comparable -> comparable -> Bool

Designed for currying. Returns a predicate that tests if the value is greater than or equal to the first value given.

20 |> greaterThanEqualTo 10 -- True


20 |> greaterThanEqualTo 20 -- False


[5, 10, 11, 20]
|> List.map (greaterThanEqualTo 10) -- [False, True, True, True]
equals : a -> Predicate a

A natural language alias for == that also helps to avoid parenthesis when writing point-free flows

employees
|> List.filter <| .employmentType >> equals Contractor

Chain and Composition Friendly Math

adding : number -> number -> number

A literate alias for addition that is nice for building functional pipelines by avoiding parenthesis.

List.map <| adding 5

rather than

List.map <| (+) 5
subtracting : number -> number -> number

Subtraction alias designed for pipelines i.e. the order is swapped such that the subtrahend is the first argument.

10
|> subtracting 4 -- 6


[20, 30, 40]
|> List.map (subtracting 6) -- [14, 24, 34]
dividedByInt : Int -> Int -> Int

Division designed for pipelines i.e. the divisor is the first argument.

20
|> divideByInt 4 -- 5


[20, 32, 40]
|> List (divideByInt 4) -- [5, 8, 10]
dividedByFloat : Float -> Float -> Float

Division designed for pipelines i.e. the divisor is the first argument.

20
|> dividedByFloat 2.5 -- 8.0


[20, 25, 26.25]
|> List (divideByFloat 2.5) -- [8.0, 10.0, 10.5]
multiplying : number -> number -> number

A natural language alias for *.

negated : number -> number

Negate the given number.

Flow Control

ifElse : Predicate a -> (a -> b) -> (a -> b) -> a -> b

Creates a function that will process either the whenTrue or the whenFalse function depending upon the result of the condition predicate.

ifElse
    (.weightInLbs >> R.Relation.greaterThan 500)
    (.name >> String.append "Sorry. You cannot ride the rollercoaster ")
    (.name >> String.append "Enjoy your ride ")
cond : List ( Predicate a, a -> b ) -> a -> Maybe b

Returns a function which encapsulates if/else, if/else, ... logic given a list of (predicate, transformer) tuples.

  • The condition pairs are processed in order
    • and returns the transformation of the first predicate that passes
  • If no conditions pass then returns Nothing.
condDefault : List ( Predicate a, a -> b ) -> Thunk b -> a -> b

Same as cond but also provides a default option in the case that none of the conditions pass.

when : Predicate a -> (a -> a) -> a -> a

Tests the final argument by passing it to the given predicate function.

  • If the predicate is satisfied, the function will return the result of calling the whenTrueFn function with the same argument.
  • If the predicate is not satisfied, the argument is returned as is.
unless : Predicate a -> (a -> a) -> a -> a

Tests the final argument by passing it to the given predicate function.

  • If the predicate is not satisfied, the function will return the result of calling the whenFalseFn function with the same argument.
  • If the predicate is satisfied, the argument is returned as is.
until : Predicate a -> (a -> a) -> a -> a

Takes a predicate, a transformation function, and an initial value, and returns a value of the same type as the initial value. It does so by applying the transformation until the predicate is satisfied, at which point it returns the satisfactory value.

Function Combinators

(first -> second -> output)
-> ( input -> first, input -> second )
-> input
-> output

This function can best be explained with examples as it is unusually useful when building data pipelines but has a tricky type signature.

Please do NOT be afraid of the type signature!

If you would like a function that transforms a Person to a String of their first and last name.

converge
    String.append
    ( .firstName, .lastName >> String.append " " )

Or perhaps you are building a data pipeline

  • where at some stage you have a List Widget
  • and you have a function extraWidgets: List Widget -> List Widget
  • and you would like to like to end up with a new list of widgets that contains both the original widgets and the extra widgets.

Then you might write

converge
    List.append
    (identity, extraWidgets)
(first -> second -> third -> output)
-> ( input -> first, input -> second, input -> third )
-> input
-> output

Converge for the case of a 3 argument function. See converge for examples.

convergeList : (List a -> output) -> List (input -> a) -> input -> output

As with converge, convergeList is best explained with an example

convergeList
    (String.join " ")
    [ .firstName, .middleInitial, .lastName ]

Or to be more complete

nonEmptyStrings : List String -> List String
nonEmptyStrings = List.filter (complement String.isEmpty)


fullName : Person -> String
fullName =
    convergeList
        (nonEmptyStrings >> String.join " ")
        [ .firstName, .middleInitial, .lastName ]
curry : (( a, b ) -> c) -> a -> b -> c

Converts a function from 2-tuple to normal curried function. This is mostly useful for reverting an uncurry.

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

Converts a function from 3-tuple to normal curried function. This is mostly useful for reverting an uncurry.

flip : (a -> b -> c) -> b -> a -> c

Flip the first and second arguments of a function.

  • This is an incredibly useful function when needing to change the application of an existing function.
  • WARNING However, it can also destroy the literate reading of code if overused and it is easy to overuse.

For example, the first argument to Dict.get is the key and the second is the dictionary. What if we want to map over multiple keys? We would want to apply the dictionary first and get a function that accepts the key.

valuesForKeys : Dict comparable value -> List comparable -> List value
valuesForKeys =
    flip Dict.get >> List.filterMap

More simply, consider the case of having a (Dictionary k v) of values but wanting to pass along a function k -> Maybe v to some provider so that the provider doesn't couple to the Dictionary? This would be a good idea to Reduce Coupling and honor the Principle of Least Privilege.

someInterestingProvider : (Int -> Widget) -> String -> Maybe Widget
someInterestingProvider = Debug.todo "doesn't matter for the demonstration"


someConsumer : List Widget -> String -> Maybe Widget
someConsumer widgets =
    someInterestingProvider (flip Dict.get (indexBy .id widgets))
fnContraMap : (a -> b) -> (b -> c) -> a -> c

This is like map for functions but going the opposite direction (contra functor).

  • map: (a -> b) -> F a -> F b
  • contraMap: (a -> b) -> F b -> F a

OKAY. So that it probably a little or a lot confusing. How would you use this? Any time yout have a parent/child relation and a function that will transform the child and you want to get back a function that transforms the parent.

addressesCount : Person -> Int
addressesCount = fnContraMap .addresses List.length
fnContraMap2 : (a -> b) -> (b -> b -> c) -> a -> a -> c

Best described with an example. Let's say you have a parent/child relation, a function of 2 arguments that applies to the child type and you would like to get back a function that works on the parent.

sameFavoriteColor : Person -> Person -> Bool
sameFavoriteColor = fnContraMap2 (.favorites >> .color) (==)
fnContraMap3 : (a -> b) -> (b -> b -> b -> c) -> a -> a -> a -> c

Same as fnContraMap2 but for 3 argument functions.

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

Converts a normal Elm function to a function that accepts a 2-tuple. This can be useful if the output of some function happens to be a tuple or a functor (List, Maybe, Result, etc.) of a tuple.

sums : List (number, number) -> List number
sums = List.map (uncurry adding)


sum : Maybe (number, number) -> Maybe number
sum = Maybe.map (uncurry adding)
uncurry3 : (a -> b -> c -> d) -> ( a, b, c ) -> d

Converts a standard Elm function to a function that accepts a 3-tuple.

Name and Relation to RamdaJS

Ra is a subset of RamdaJS converted to Elm and excluding functions relating to topics indicated below. I originally toyed with naming this package simply R but that would create confusion with the R language.

Miscellaneous

isMemberOf : Dict comparable v -> comparable -> Bool

Dict.member with the arguments flipped in order to test the member of many keys against a single dictionary.

let
    validPersonIds =
        personIds
        |> List.filter (isMemberOf idToPerson)
in
-- ...
deduplicateConsecutiveItemsBy : (a -> b) -> List a -> List a

Deduplicate consecutive items in a list using a function to extra the element of interest.

deduplicateConsecutiveItemsBy identity [ 1, 1, 1, 2, 3, 3 ] -- [1, 2, 3]


deduplicateConsecutiveItemsBy
    Tuple.second
    [ ("Betty", 1), ("Tom", 1), ("Jane", 2)] -- [("Betty", 1), ("Jane", 2)]

Excellent elm-community Extra packages such as

Many of the RamdaJS functions or their equivalent functionality can be found in these packages.

Object Meta-Programming

RamdaJS features a number of functions to manipulate JavaScript objects. The closest equivalent in Elm would be Dict which already features all of the necessary functions either through Core or Dict.Extra. Further, Dict is not the same as Record and has a number of tradeoffs when compared to Records

  • lose of explicit type contract
    • requiring neurotic optionality checks (i.e. Dict.get returns a Maybe)
  • a Dict can only hold values of a single type while a Record may feature a different type for each field.

Relating to Optics (Lens)

There are several high quality Elm packages that provide optics.

Higher-Kinded Types and Categories

Elm does not feature higher kinded types or typeclasses and so we cannot express abstract

  • map (Functor)
  • chain (Monad)
  • append (Monoid)
  • sequence/traverse (Applicative, Traversable).

NOTE We get better error messages, fewer opportunities for ambiguous code, and, very likely, a faster compiler in the exchange.