Function Helpers
This section contains information about composable higher-order functions whose principal arguments are other functions.
#
composecompose(...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.0bingo-functional-js
- v0.1.0
Arguments:
- functions (function) - The functions to chain
- JavaScript
- PHP
import { compose } from 'bingo-functional-js'
const square = (x) => x ** 2const cube = (x) => x ** 3
const composite = compose(square, cube)composite(2)
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);
#
composeRightcomposeRight(...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
- JavaScript
- PHP
import { composeRight } from 'bingo-functional-js'
const square = (x) => x ** 2const cube = (x) => x ** 3
const composite = composeRight(square, cube)composite(2)
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);
#
partialpartial(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.2bingo-functional-js
- v0.1.0
Arguments:
- function - The function to which arguments are partially applied
- args - The arguments to bind to the function
- JavaScript
- PHP
import { partial } from 'bingo-functional-js'
const add = partial((x, y, z) => x + y + z, 2, 3)
add(4)
use function Chemem\Bingo\Functional\partial;
$add = partial(fn (int $x, int $y, int $z): int => $x + $y + $z, 2, 3);
$add(4);
#
partialRightpartialRight(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.2bingo-functional-js
- v0.1.0
Arguments:
- function - The function to which arguments are partially applied
- args - The arguments to bind to the function
- JavaScript
- PHP
import { partialRight } from 'bingo-functional-js'
const concat = partialRight((x, y, z) => `${x}-${y}-${z}`, 'baz', 'bar')
add('foo')
use function Chemem\Bingo\Functional\partialRight;
$add = partialRight( fn (string $x, string $y, string $z): string => $x . '-' . $y . '-' . $z, 'baz', 'bar',);
$add('foo');
#
currycurry(function)
curry :: ((a, b), c) -> a -> b -> c
Decomposes an n-ary function into unary functions.
Since:
bingo-functional
- v1.0.0bingo-functional-js
- v0.1.0
Arguments:
- function - The function to curry
- JavaScript
- PHP
import { curry } from 'bingo-functional-js'
const add = curry((x, y, z) => x + y + z)const fst = add(2)const snd = fst(3)
snd(4)
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);
#
curryRightcurryRight(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.0bingo-functional-js
- v0.1.0
Arguments:
- function - The function to curry
- JavaScript
- PHP
import { curryRight } from 'bingo-functional-js'
const add = curryRight((x, y, z) => x + y + z)const fst = add(2)const snd = fst(3)
snd(4)
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);
#
curryNcurryN(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.0bingo-functional-js
- v0.1.0
Arguments:
- argCount - The number of unary functions to create
- function - The function to which arguments are partially applied
- JavaScript
- PHP
import { curryN } from 'bingo-functional-js'
const add = curryN(2, (x, y, z = 2) => x + y + z)const fst = add(2)const snd = fst(3)
snd(4)
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);
#
constantFunctionconstantFunction(...args)
constantFunction :: a -> () a
The constant function always returns the first argument it receives.
Since:
bingo-functional
- v1.0.0bingo-functional-js
- v0.1.0
Arguments:
- args - Arbitrary values to pass to the function
- JavaScript
- PHP
import { constantFunction } from 'bingo-functional-js'
const res = constantFunction('foo', 'bar')res()
use function Chemem\Bingo\Functional\constantFunction;
$res = constantFunction('foo', 'bar');$res();
#
memoizememoize(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.0bingo-functional-js
- v0.1.0
Arguments:
- function - The function to memoize
- JavaScript
- PHP
import { memoize } from 'bingo-functional-js'
const factorial = (x) => (x < 2 ? 1 : factorial(x - 1) * x)const optimized = memoize(factorial)
optimized(130)
use function Chemem\Bingo\Functional\memoize;
$factorial = memoize(function (int $val) use (&$factorial) { return $val < 2 ? 1 : $factorial($val - 1) * $val;});
$factorial(130);
#
trampolinetrampoline(function)
trampoline :: (a -> (a -> b)) -> a -> b
Optimizes tail-recursive functions.
Since:
bingo-functional
- v1.10.0bingo-functional-js
- v0.1.0
Arguments:
- function - The tail-call recursive function to optimize.
- JavaScript
- PHP
import { trampoline } from 'bingo-functional-js'
const fibonacci = (x) => (x < 2 ? x : fibonacci(x - 1) + fibonacci(x - 2))const optimized = trampoline(fibonacci)
optimized(11)
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);
#
toExceptiontoException(function, handler)
toException :: (a -> b) -> (Object -> b) -> b
Abstracts anomalous situation handling for functions likely to thow an exception.
Since:
bingo-functional
- v1.9.0bingo-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.
- JavaScript
- PHP
import { toException } from 'bingo-functional-js'
const divide = (x, y) => { if (y === 0) { throw new Exception('Division by zero error') }
return x / y}const result = toException(divide, (_) => Infinity)(2, 0)
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);
#
flipflip(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.0bingo-functional-js
- v0.1.0
Arguments:
- function - The function whose argument order is to be changed
- args - The function's arguments
- JavaScript
- PHP
import { flip } from 'bingo-functional-js'
const quadratic = (a, b, c) => { const numFrag = (b ** 2 - 4 * a * c) ** (1 / 2) const denominator = 2 * a
return [(-b + numFrag) / denominator, (-b - numFrag) / denominator]}const result = flip(quadratic, 4, 4, 1)
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);
#
throttlethrottle(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 AliasesSome functions specified in this section have alternate names. Shown below is a table of the said functions and their respective aliases.
Function | JavaScript | PHP | Alias |
---|---|---|---|
partial | No | Yes | partialLeft |
constantFunction | No | Yes | K |