Functional toolbox for PHP
composer require zonuexe/functools
Functools\Operator- Operator Expression wrapper
- Binary operator
1 + 2$a === "abc"$date instanceof '\Datetime' - Conditional operator
$cond ? $then : $else - Lazy Conditional operator
$cond($v) ? $then($v) : $else($v)(thunk)
- Binary operator
- Referential transparent version sorting array functions.
- You can
array_map(f::op('ksort'), $arrays)!
- You can
- Language-construct wrapper
echo,print,eval,require,require_once,include,include_once- limited support:
array,isset,empty
- Operator Expression wrapper
Functools::partial(callable $callback, array $arguments, int $pos)- Partial application for callable (
arary_mapfriendly)
- Partial application for callable (
Functools::arity(callable $callback)- Analyze arity(number of arguments) of
$callback.
- Analyze arity(number of arguments) of
Functools::curry(callable $callback)- Currying
$callbackobject.
- Currying
Functools::op(string $symbol, [array $arguments, int $pos])- Get callable object (and partial application.)
Functools::compose(callable $callback,...)Functools::pipe(mixed $value, callable $callback...)- Pipeline like functions composing
Functools::tuple(mixed $item,...)- Make n-Tuple.
Functools::fix(callable $callback)Functools::memoize(callable $callback, [array $cache])- Function optimizer using Memoization.
- This feature will help in optimization of transparent reference explicit definition equation.
This library does not have iterator. You will be able to combine it by selecting a favorite of iterator library.
- PHP
arrayand functions - Underbar.php - A collection processing library for PHP, like Underscore.js.
- Ginq
- nikic\iter
Japanese version: http://qiita.com/tadsan/items/abc52f0739ab5b4e781b
use Teto\Functools as f;$comma = f::partial("implode", [", "]);
$comma(range(1, 10));
// "1, 2, 3, 4, 5, 6, 7, 8, 9, 10"
$join_10 = f::partial("implode", [1 => range(1, 10)], 0);
$join_10("@");
// "1@2@3@4@5@6@7@8@9@10"
$join_10("\\");
=> "1\\2\\3\\4\\5\\6\\7\\8\\9\\10"
$sleep3 = f::partial("sleep", [3]);
$sleep3();
$sleep3("foo"); // Error!
$sleep3 = f::partial("sleep", [3], -1);
$sleep3("foo"); // OK!$add = f::op("+");
$add(2, 3); // (2 + 3) === 5
$add_1 = f::op("+", [1]);
$add_1(4); // (1 + 4) === 5
$half = f::op("/", [1 => 2], 0);
$half(10); // (10 / 2) === 5$teto = f::tuple("Teto Kasane", 31, "2008-04-01", "Baguette");
$ritsu = f::tuple("Ritsu Namine", 6, "2009-10-02", "Napa cabbage");
// index access
$teto[0]; // "Teto Kasane"
$teto[1]; // 31
$teto[2]; // "2008-04-01"
$teto[3]; // "Baguette"
// property access
$tetop = f::tuple("name", "Teto Kasane", "age", 31, "birthday", "2008-04-01", "item", "Baguette");
$ritsup = f::tuple("name", "Ritsu Namine", "age", 6, "birthday", "2009-10-02", "item", "Napa cabbage");
$tetop->pget("name"); // "Teto Kasane"
$tetop->pget("age"); // 31
$tetop->pget("birthday"); // "2008-04-01"
$tetop->pget("item"); // "Baguette"$fib = f::fix(function ($fib) {
return function ($x) use ($fib) {
return ($x < 2) ? 1 : $fib($x - 1) + $fib($x -2);
};
});
$fib(6); // 13// simple fibonacci function. But, very very slow.
$fib1 = function ($n) use (&$fib1) {
return ($n < 2) ? $n : $fib1($n - 1) + $fib1($n - 2);
};
// simple fibonacci function too. very fast!
$fib2 = f::memoize(function ($n) use (&$fib2) {
return ($n < 2) ? $n : $fib2($n - 1) + $fib2($n - 2);
}, [0, 1]);see ./LICENSE.
Functional toolbox
Copyright (c) 2015 USAMI Kenta <tadsan@zonu.me>
I love Teto Kasane. (ja: Teto Kasane official site)
r /
__ , --ヽ!-- .、_
! `/::::;::::ヽ l
!二二!::/}::::丿ハニ|
!ニニ.|:/ ノ/ }::::}コ
L二lイ 0´ 0 ,':ノコ
lヽノ/ヘ、 '' ▽_ノイ ソ
ソ´ /}`ス / ̄ ̄ ̄ ̄/
.(_:;つ/ 0401 / カタカタ
 ̄ ̄ ̄ ̄ ̄\/____/