eldesh/alt-core-elm/1.2.0

Data.Function

Small but handy combinators for building and transforming functions.

These helpers make it easy to compose complex behaviour on the spot, so you can avoid writing ad‑hoc lambdas or intermediate definitions.

id : a -> a

Return the given value unchanged.

const : a -> b -> a

Return the first argument, ignoring the second.

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

Return a new function whose first two arguments are swapped.

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

Apply a projection to two values and then combine the results with the given binary function.

Equivalent to \x y -> f (p x) (p y).

applyWhen : Bool -> (a -> a) -> a -> a

Apply the function to the value only when the first argument is True otherwise return the value unchanged.

fix : ((a -> b) -> a -> b) -> a -> b

The least fixed point of the function f, i.e. the least defined x such that f x = x.

Examples

fix
    (\rec n ->
        if n <= 1 then
            1


        else
            n * rec (n - 1)
    )
    5
    == 120


map
    (fix
        (\rec n ->
            if n < 2 then
                n


            else
                rec (n - 1) + rec (n - 2)
        )
    )
    (range 1 10)
    == [ 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 ]