API¶
Static constructors¶
compose¶
Performs right-to-left function composition. The last argument may have any arity; the remaining arguments must be unary.
Warning
The result of compose is not automatically curried.
<?php
/**
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
// phpcs:disable Generic.Files.LineLength.TooLong
declare(strict_types=1);
namespace Example;
use loophp\fpt\FPT;
$closure = static fn (string $first, string $second): string => sprintf('My cats names are %s and %s.', $first, $second);
$composedClosure = FPT::compose()('strtoupper', $closure);
$composedClosure('Izumi', 'Nakano'); // "MY CATS NAMES ARE IZUMI AND NAKANO."
curryLeft¶
Returns a curried equivalent of the provided function.
<?php
/**
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Example;
use loophp\fpt\FPT;
$curryLeft = FPT::curryLeft()('explode');
[$firstName, $lastName] = $curryLeft(' ')('James Bond');
$curriedLeft = FPT::curryLeft()('explode', 3);
[$evil, $good] = $curriedLeft(',')('Jaws,James,Bond')(2);
curryRight¶
Returns a curried equivalent of the provided function.
<?php
/**
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Example;
use loophp\fpt\FPT;
$curryRight = FPT::curryRight()('explode');
[$firstName, $lastName] = $curryRight('James Bond')(' ');
$curryRight = FPT::curryRight()('explode', 3);
[$evil, $good] = $curryRight(2)('Jaws,James,Bond')(',');
filter¶
flip¶
Returns a new function much like the supplied one, except that the first two arguments’ order is reversed.
<?php
/**
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Example;
use loophp\fpt\FPT;
$f = static fn (string ...$x): string => implode('', $x);
$flip = FPT::flip()($f)('a', 'b', 'c'); // bac
fold¶
Returns a single item by iterating through the list, successively calling the reducer function and passing it an accumulator value and the current value from the array, and then passing the result to the next call.
The iterator function receives two values: $acc
and $value
.
<?php
/**
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Example;
include __DIR__ . '/../../../vendor/autoload.php';
use loophp\fpt\FPT;
$reducer = static fn (callable $a) => static fn (callable $b) => static fn (...$xs) => $a($b(...$xs));
$accumulator = static fn ($v): string => sprintf('[%s]', $v);
$callable = ['strtoupper', static fn ($s): string => sprintf('%s%s', $s, $s)];
FPT::fold()($reducer)($accumulator)(...$callable)('hello'); // [HELLOHELLO]
identity¶
A function that does nothing but return the parameter supplied to it.
<?php
/**
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Example;
use loophp\fpt\FPT;
FPT::identity()('foo'); // foo
map¶
Takes a function and an iterable, applies the function to each of the iterable values, and yield the result.
nary¶
Wraps a function of any arity in a function that accepts exactly n parameters. Any extraneous parameters will not be passed to the supplied function.
<?php
/**
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Example;
use loophp\fpt\FPT;
$closure = static fn (...$args): string => implode(';', $args);
$newClosure = FPT::nary()(1)($closure);
$newClosure('a', 'b'); // a
$newClosure = FPT::nary()(2)($closure);
$newClosure('a', 'b'); // a;b
not¶
Wraps a function in a function that returns the ! of the original function return value.
<?php
/**
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Example;
use loophp\fpt\FPT;
$closure = static fn (string $left, string $right): bool => $left === $right;
$closure('a', 'b'); // false
$closure('a', 'a'); // true
$notClosure = FPT::not()($closure);
$notClosure('a', 'b'); // true
$notClosure = FPT::not()($closure);
$notClosure('a', 'a'); // false
operator¶
Apply an operator on two arguments.
This method is curried and take first the operator, then its left and right members.
Available operators are constants in Operator class.
<?php
/**
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Example;
use loophp\fpt\FPT;
use loophp\fpt\Operator;
FPT::operator()(Operator::OP_PLUS)(40)(2); // 42