loophp / combinator
精选组合子列表
2.0.2
2021-08-04 20:13 UTC
Requires
- php: >= 8
Requires (Dev)
- drupol/php-conventions: ^5.0.3
- friends-of-phpspec/phpspec-code-coverage: ^5
- infection/infection: >= 0.21
- infection/phpspec-adapter: ^0.1.2
- phpspec/phpspec: ^7.1
- phpstan/phpstan: ^0.12
- phpstan/phpstan-strict-rules: ^0.12
README
组合子
描述
本包提供了一系列知名的组合子。
组合子是一种 高阶函数,它仅使用函数应用和先前定义的组合子来定义其参数的结果。它在1920年由 莫塞·舒恩菲克尔 和 哈斯凯尔·库瑞 提出,近年来在计算机科学中作为一种计算理论模型,也被用作 函数式编程语言设计的基石。舒恩菲克尔在1920年引入的组合子,其初衷是提供一个类似于构建函数的方法 - 并且消除对变量的任何提及 - 尤其是在谓词逻辑中。
要求
- PHP >= 8
安装
composer require loophp/combinator
可用的组合子
使用方法
简单组合子
<?php declare(strict_types=1); include 'vendor/autoload.php'; use loophp\combinator\Combinators; // Lambda calculus: I combinator correspond to λa.a Combinators::I()('a'); // a // Lambda calculus: K combinator correspond to λa.λb.a (or λab.a) Combinators::K()('a')('b'); // a // Lambda calculus: C combinator correspond to λf(λa(λb(fba))) // and thus: C K a b = b Combinators::C()(Combinators::K())('a')('b'); // b // Lambda calculus: Ki combinator correspond to λa.λb.b (or λab.b) Combinators::Ki()('a')('b'); // b
Y 组合子
<?php declare(strict_types=1); namespace Test; include __DIR__ . '/vendor/autoload.php'; use Closure; use loophp\combinator\Combinators; // Example 1 $factorialGenerator = static fn (Closure $fact): Closure => static fn (int $n): int => (0 === $n) ? 1 : ($n * $fact($n - 1)); $factorial = Combinators::Y()($factorialGenerator); var_dump($factorial(6)); // 720 // Example 2 $fibonacciGenerator = static fn (Closure $fibo): Closure => static fn (int $number): int => (1 >= $number) ? $number : $fibo($number - 1) + $fibo($number - 2); $fibonacci = Combinators::Y()($fibonacciGenerator); var_dump($fibonacci(10)); // 55
更多内容请参阅 维基百科页面。
推荐阅读和资源
- 《模仿乌鸦的乌鸦》
- http://dkeenan.com/Lambda/
- https://gist.github.com/Avaq/1f0636ec5c8d6aed2e45
- https://zh.wikipedia.org/wiki/%E7%BB%84%E5%90%88%E5%AD%90%E8%AE%BA
- https://github.com/sanctuary-js/sanctuary
- https://zh.wikipedia.org/wiki/%E5%8A%A8%E6%80%81%E8%AE%A1%E7%AE%97
- https://hackage.haskell.org/package/data-aviary-0.4.0/docs/src/Data-Aviary-BirdsInter.html
- https://github.com/fantasyland/fantasy-birds/blob/master/README.md
- https://www.cis.upenn.edu/~bcpierce/tapl/
- https://plato.stanford.edu/entries/lambda-calculus/
- https://github.com/glebec/lambda-talk
- https://www.youtube.com/watch?v=seVSlKazsNk
贡献
请随意通过发送拉取请求来贡献。我们通常是一个非常响应的团队,我们将帮助您从开始到结束完成您的拉取请求。
由于某些原因,如果您不能对代码进行贡献但愿意帮助,赞助是一种好、可靠且安全的方式来表达我们对投入此包所花费时间的感激之情。
谢谢
作者
变更日志
请参阅 CHANGELOG.md 以查看基于 git 提交 的变更日志。
有关更详细的变更日志,请检查 发行变更日志。