Skip to main content

Function Helpers

This section contains information about composable higher-order functions whose principal arguments are other functions.

compose#

compose(...functions)

compose :: f (g a) -> compose f g a

Combines multiple functions into a single meta-function.

The ability to combine functions is a cornerstone principle of Functional Programming. Adapted from Mathematics - the rubric for the paradigm - composition makes it possible to apply the output of one function as the input of another - in a chain. A function f and another function g - per the said basis -are composable into a chain f o g.

Since:

  • bingo-functional - v1.0.0
  • bingo-functional-js - v0.1.0

Arguments:

  • functions (function) - The functions to chain
use function Chemem\Bingo\Functional\compose;
$square     = fn (int $x): int => $x ** 2;$cube       = fn (int $x): int => $x ** 3;
$composite  = compose($square, $cube);$composite(2);

composeRight#

composeRight(...functions)

composeRight :: g (f a) -> compose g f a

Combines multiple functions into a single meta-function, but from right-to-left.

Since:

  • bingo-functional-js - v0.1.0
  • bingo-functional - v1.0.0

Arguments:

  • functions - Arbitrary functions to chain
use function Chemem\Bingo\Functional\composeRight;
$square     = fn (int $x): int => $x ** 2;$cube       = fn (int $x): int => $x ** 3;
$composite  = composeRight($square, $cube);$composite(2);

partial#

partial(function, ...args)

partial :: (a, b) -> (a) b

Decomposes a function into sub-functions of variable arity.

This technique allows for the incremental supply of arguments to a function.

Since:

  • bingo-functional - v1.0.0, absent from versions 1.2.0 to 1.7.2
  • bingo-functional-js - v0.1.0

Arguments:

  • function - The function to which arguments are partially applied
  • args - The arguments to bind to the function
use function Chemem\Bingo\Functional\partial;
$add = partial(fn (int $x, int $y, int $z): int => $x + $y + $z, 2, 3);
$add(4);

partialRight#

partialRight(function, ...args)

partialRight :: (a, b) -> (b) a

Decomposes a function in such a way as to allow for right-to-left argument binding.

Since:

  • bingo-functional - v1.7.2
  • bingo-functional-js - v0.1.0

Arguments:

  • function - The function to which arguments are partially applied
  • args - The arguments to bind to the function
use function Chemem\Bingo\Functional\partialRight;
$add = partialRight(  fn (string $x, string $y, string $z): string => $x . '-' . $y . '-' . $z,  'baz',  'bar',);
$add('foo');

curry#

curry(function)

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

Decomposes an n-ary function into unary functions.

Since:

  • bingo-functional - v1.0.0
  • bingo-functional-js - v0.1.0

Arguments:

  • function - The function to curry
use function Chemem\Bingo\Functional\curry;
$add = curry(fn (int $x, int $y, int $z): int => ($x + $y + $z));$fst = $add(2);$snd = $add(3);
$add(4);

curryRight#

curryRight(function)

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

Decomposes an n-ary function into unary functions.

The function invocation order is from right-to-left.

Since:

  • bingo-functional - v1.9.0
  • bingo-functional-js - v0.1.0

Arguments:

  • function - The function to curry
use function Chemem\Bingo\Functional\curryRight;
$add = curryRight(fn (int $x, int $y, int $z): int => ($x + $y + $z));$fst = $add(2);$snd = $add(3);
$add(4);

curryN#

curryN(argCount, function)

curryN :: ((a, b), c) -> Int -> a -> b -> c

Decomposes an n-ary function into unary functions.

Note: By specifying an argument count, it is possible to set the number of unary functions to create.

Since:

  • bingo-functional - v1.0.0
  • bingo-functional-js - v0.1.0

Arguments:

  • argCount - The number of unary functions to create
  • function - The function to which arguments are partially applied
use function Chemem\Bingo\Functional\curryN;
$add = curryN(2, fn (int $x, int $y, int $z = 2): int => ($x + $y + $z));$fst = $add(2);$snd = $add(3);
$add(4);

constantFunction#

constantFunction(...args)

constantFunction :: a -> () a

The constant function always returns the first argument it receives.

Since:

  • bingo-functional - v1.0.0
  • bingo-functional-js - v0.1.0

Arguments:

  • args - Arbitrary values to pass to the function
use function Chemem\Bingo\Functional\constantFunction;
$res = constantFunction('foo', 'bar');$res();

memoize#

memoize(function)

memoize :: (a -> b) -> a -> b

Caches the result of a function call - and optimizes subsequent calls with the same inputs.

Since:

  • bingo-functional - v1.0.0
  • bingo-functional-js - v0.1.0

Arguments:

  • function - The function to memoize
use function Chemem\Bingo\Functional\memoize;
$factorial = memoize(function (int $val) use (&$factorial) {  return $val < 2 ? 1 : $factorial($val - 1) * $val;});
$factorial(130);

trampoline#

trampoline(function)

trampoline :: (a -> (a -> b)) -> a -> b

Optimizes tail-recursive functions.

Since:

  • bingo-functional - v1.10.0
  • bingo-functional-js - v0.1.0

Arguments:

  • function - The tail-call recursive function to optimize.
use function Chemem\Bingo\Functional\trampoline;
$fibonacci = trampoline(function (int $val) use (&$fibonacci) {  return $val < 2 ? $val : $fibonacci($val - 1) + $fibonacci($val - 2);});
$fibonacci(11);

toException#

toException(function, handler)

toException :: (a -> b) -> (Object -> b) -> b

Abstracts anomalous situation handling for functions likely to thow an exception.

Since:

  • bingo-functional - v1.9.0
  • bingo-functional-js - v0.1.0

Arguments:

  • function - The function likely to throw an Error | Exception
  • handler - The anomalous condition handler function (accepts exception object as a parameter)

Note: In the absence of a handler, the result - in the event that an abnormal situation is detected - is the exception error message.

use function Chemem\Bingo\Functional\toException;
$divide = function (int $x, int $y) {  if ($y === 0) {    throw new Exception('Division by zero error');  }
  return $x / $y;};$result = toException($divide, fn ($_) => INF)(2, 0);

flip#

flip(function)flip(function, ...args)

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

The flip function reverses the order of a function's arguments.

Since:

  • bingo-functional - v1.11.0
  • bingo-functional-js - v0.1.0

Arguments:

  • function - The function whose argument order is to be changed
  • args - The function's arguments
use function Chemem\Bingo\Functional\flip;
$quadratic = flip(function (float $a, float $b, float $c) {  $numFrag      = (($b ** 2) - (4 * $a * $c)) ** (1 / 2);  $denominator  = 2 * $a;
  return [    (-$b + $numFrag) / $denominator,    (-$b - $numFrag) / $denominator,  ];});
$result = $quadratic(4, 4, 1);

throttle#

throttle(function, delay)

throttle :: (a -> b) -> Int -> a -> b -> b

Defers function execution by a specified duration.

Since:

  • bingo-functional - v1.4.0

Argument(s):

  • function - The functon to throttle
  • delay - An arbitrary delay (in seconds)
use function Chemem\Bingo\Functional\throttle;
$exec = throttle(fn ($x, $y) => $x ** $y, 10);
// prints result after 2 seconds$exec(2, 2);

Function Aliases#

Some functions specified in this section have alternate names. Shown below is a table of the said functions and their respective aliases.

FunctionJavaScriptPHPAlias
partialNoYespartialLeft
constantFunctionNoYesK