sergiors / functional
PHP函数式编程,没有循环
dev-php7
2016-11-01 16:28 UTC
Requires
- php: ^7.0
Requires (Dev)
- pdepend/pdepend: ~2.0
- phing/phing: ~2.10
- phploc/phploc: ~2.1
- phpmd/phpmd: ~2.2
- phpunit/phpunit: ^4.8
- sebastian/phpcpd: ~2.0
- squizlabs/php_codesniffer: ~2.1
This package is not auto-updated.
Last update: 2024-09-15 01:57:28 UTC
README
动机
我创建这个项目是为了探索 函数式编程。
这个项目不使用任何循环!
安装
composer require sergiors/functional "dev-master"
如何使用
use Sergiors\Functional as F; $addFourNumbers = function ($a, $b, $c, $d) { return $a + $b + $c + $d; }; $partialAddFourNumbers = F\partial($addFourNumbers); $f = $partialAddFourNumbers(1, 2); $g = $f(3); echo $g(4); // => 10
你可以使用 Sergiors\Funcional\hold
来持有原生函数。
use Sergiors\Functional as F; $implode = F\hold('implode', ','); echo $implode(['a', 'b']); // => a,b // something more complex $pipe = F\pipe( F\hold('array_slice', F\_, 2), F\hold('array_map', function ($x) { return $x * 2; }), F\hold('implode', ',') ); echo $pipe([10, 20, 30, 40, 50]); // => 60,80,100
Sergiors\Functional\_
是负载的占位符。如果你没有设置占位符,负载将是最后一个参数。
集合
你可以用 Sergiors\Functional\Collection
包装一个数组来使用流畅的接口。
use Sergiors\Functional\Collection; $ls1 = new Collection([1, 2, 3, 4, 5, 6]); $ls2 = $ls1->filter(function ($x) { return $x > 2; })->map(function ($x) { return $x + 1; }); print_r($ls1->toArray()); // => [1, 2, 3, 4, 5, 6] print_r($ls2->toArray()); // => [4, 5, 6, 7]
API
所有函数都自动应用了偏应用。
always()
append()
compose()
concat()
diff()
drop()
each()
equals()
filter()
find()
flatten()
get()
getin()
gt()
gte()
has()
head()
hold()
id()
ifelse()
last()
lt()
lte()
map()
not()
partial()
pipe()
prop()
prepend()
reduce()
sort()
tail()
take()
takewhile()
Collection::concat()
Collection::filter()
Collection::map()
Collection::each()
Collection::prepend()
Collection::append()
Collection::reduce()
Collection::count()
Collection::getIterator()
Collection::toArray()
许可证
MIT