cypresslab/php-curry
最新稳定版本:0.5.0
Composer 安装命令:
composer require cypresslab/php-curry
包简介
Curried functions in PHP
README 文档
README
An implementation for currying in PHP
Currying a function means the ability to pass a subset of arguments to a function, and receive back another function that accepts the rest of the arguments. As soon as the last one is passed it gets back the final result.
Like this:
use Cypress\Curry as C; $adder = function ($a, $b, $c, $d) { return $a + $b + $c + $d; }; $firstTwo = C\curry($adder, 1, 2); echo $firstTwo(3, 4); // output 10 $firstThree = $firstTwo(3); echo $firstThree(14); // output 20
Currying is a powerful (yet simple) concept, very popular in other, more purely functional languages. In haskell for example, currying is the default behavior for every function.
In PHP we still need to rely on a wrapper to simulate the behavior
How to install
composer require cypresslab/php-curry
In your PHP scripts (with composer autoloader in place) just import the namespace and use it!
use Cypress\Curry as C; $chunker = C\curry('array_chunk', ['a', 'b']); var_dump($chunker(1)); // output [['a'], ['b']] var_dump($chunker(2)); // output [['a', 'b']]
Right to left
It's possible to curry a function from left (default) or from right.
$divider = function ($a, $b) { return $a / $b; }; $divide10By = C\curry($divider, 10); $divideBy10 = C\curry_right($divider, 10); echo $divide10By(10); // output 1 echo $divideBy10(100); // output 10
Parameters as an array
You can also curry a function and pass the parameters as an array, just use the *_args version of the function.
use Cypress\Curry as C; $divider = function ($a, $b) { return $a / $b; }; $division = C\curry_args($divider, [100, 10]); echo $division(); // output 10 $division2 = C\curry_right_args($divider, [100, 10]); echo $division2(); // output 0.1
Optional parameters
Optional parameters and currying do not play very nicely together. This library excludes optional parameters by default.
$haystack = "haystack"; $searches = ['h', 'a', 'z']; $strpos = C\curry('strpos', $haystack); // You can pass function as string too! var_dump(array_map($strpos, $searches)); // output [0, 1, false]
But strpos has an optional $offset parameter that by default has not been considered.
If you want to take this optional $offset parameter into account you should "fix" the curry to a given length.
$haystack = "haystack"; $searches = ['h', 'a', 'z']; $strpos = C\curry_fixed(3, 'strpos', $haystack); $finders = array_map($strpos, $searches); var_dump(array_map(function ($finder) { return $finder(2); }, $finders)); // output [false, 5, false]
curry_right has its own fixed version named curry_right_fixed
Placeholders
The function __() gets a special placeholder value used to specify "gaps" within curried functions, allowing partial application of any combination of arguments, regardless of their positions.
$add = function($x, $y) { return $x + $y; }; $reduce = C\curry('array_reduce'); $sum = $reduce(C\__(), $add); echo $sum([1, 2, 3, 4], 0); // output 10
Notes:
-
Placeholders should be used only for required arguments.
-
When used, optional arguments must be at the end of the arguments list.
cypresslab/php-curry 适用场景与选型建议
cypresslab/php-curry 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 130.78k 次下载、GitHub Stars 达 61, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「curry」 「functional-programming」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 cypresslab/php-curry 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 cypresslab/php-curry 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 cypresslab/php-curry 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Partial function application.
Non-standard PHP library (NSPL) - functional primitives toolbox and more
Auto-curried function library
Functional library for php with proper currying
Functional Programming utilities for PHP 5.4+
Rust-aligned Option, Result, and Either monads for type-safe error handling in PHP
统计信息
- 总下载量: 130.78k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 61
- 点击次数: 24
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 未知