View Source Funx.Utils (funx v0.1.0)
A collection of higher-order functions for functional programming in Elixir.
This module provides utilities for working with functions in a functional programming style. It includes:
Summary
Functions
Transforms a function of arity n
into a curried version,
allowing it to be applied one argument at a time.
Alias for curry/1
, explicitly denoting left-to-right argument application.
Transforms a function of arity n
into a right-curried version,
applying arguments from right to left.
Reverses the argument order of a two-argument function.
Functions
Transforms a function of arity n
into a curried version,
allowing it to be applied one argument at a time.
Example
iex> add = fn a, b -> a + b end
iex> curried_add = FunPark.Utils.curry(add)
iex> add_three = curried_add.(3)
iex> add_three.(2)
5
Alias for curry/1
, explicitly denoting left-to-right argument application.
Example
iex> subtract = fn a, b -> a - b end
iex> curried_subtract = FunPark.Utils.curry_l(subtract)
iex> subtract_five = curried_subtract.(5)
iex> subtract_five.(3)
2
Transforms a function of arity n
into a right-curried version,
applying arguments from right to left.
Example
iex> divide = fn a, b -> a / b end
iex> curried_divide = FunPark.Utils.curry_r(divide)
iex> divide_by_two = curried_divide.(2)
iex> divide_by_two.(10)
5.0
Reverses the argument order of a two-argument function.
The flip/1
function takes a function of arity 2 and returns a new function
where the first and second arguments are swapped.
Examples
iex> divide = fn a, b -> a / b end
iex> flipped_divide = Utils.flip(divide)
iex> flipped_divide.(2, 10)
5.0