nicmart/functionals
Composer 安装命令:
composer require nicmart/functionals
包简介
Functionals is a collection of functionals for PHP
关键字:
README 文档
README
Functionals is a simple library that provides a set of functionals written in php.
What do you mean with "functionals"?
Here I use the term "functional" to denote Higher-Order functions, i.e. functions that take other functions as input and return a function as output.
Install
The best way to install Functionals is through composer.
Just create a composer.json file for your project:
{
"require": {
"nicmart/functionals": "dev-master"
}
}
Then you can run these two commands to install it:
$ curl -s http://getcomposer.org/installer | php
$ php composer.phar install
or simply run composer install if you have have already installed the composer globally.
Then you can include the autoloader, and you will have access to the library classes:
<?php require 'vendor/autoload.php'; use Functionals\Functionals;
What's new (2013-06-13)
Diagonalization!
It happens that you have a set indexed by two integers, and you want to completely traverse it using only one index (do you remember the proof of countability of rational numbers?) Then this function can help you, giving a complete enumeration of the set. This is done using the inverse of the Cantor's pairing function.
More formally, you have a function
f : N x N → A,
where N is the set of natural numbers, and A is another set. What you get is a function
g : N → A,
such that for each natural numbers l and m there exists an unique n such that
f (l, m) = g(n)
and the range of f is the same of the range of g.
Example:
$couples = function($x, $y) { return [$x, $y]; }; $diagonalized = Functionals::diagonalize($couples); $diagonalized(0); // [0, 0] $diagonalized(1); // [1, 0] $diagonalized(2); // [0, 1] $diagonalized(3); // [2, 0] ...
Usage
Composition of functions
If you have two functions f : A → B and g: B → C their composition is a function h: A → C that maps x to f(g(x)).
In Functionals you can compose an arbitrary number of
php callables through Functionals::compose():
$sum = function($a, $b) { return $a + $b; }; $half = function($n) { return $n/2; }; $middle = Functionals::compose($half, $sum); echo $middle(10, 16); //Prints 13 echo $middle(-10, 10); //Prints 0
You can compose an arbitrary long list of functions, and they can be any callable:
$beautifyString = Functionals::compose( function($s){ return str_replace('bad', 'good', $s); }, 'ucfirst', 'strtolower', 'trim' ); echo $beautifyString(' i\'m a reAlly Bad writTen STRING'); //prints "I'm a really good written string"
Piping
Piping is like composition, but arguments are given in the reverse order, like in a UNIX pipeline.
$sum = function($a, $b) { return $a + $b; }; $half = function($n) { return $n/2; }; $middle = Functionals::pipe($sum, $half); echo $middle(10, 16); //Prints 13 echo $middle(-10, 10); //Prints 0
Partial
A partial application of a function in several variables is obtained by fixing some arguments of the function and get a function of the remaining arguments.
For example, if you have a function f : X x Y → B, and you fix a x in X, then you get the partial
function g: Y → B that maps y to f(x,y).
In Functionals you can obtain a partial function application with the method Functionals::partial():
$sum = function($a, $b) { return $a + $b; }; $next = Functionals::partial($sum, array(1)); $next(2); // 3 $next(10); // 11
You can fix arguments in any position, specifying the right index for the fixed arguments array:
$if = function($condition, $ifTrue, $ifFalse) { return $condition ? $ifTrue : $ifFalse; }; $boolDump = Functionals::partial($if, array( 1 => 'TRUE!', 2 => 'FALSE!')) $boolDump(true); // TRUE! $boolDump(false); // FALSE!
Currying and uncurrying
To get a curried version of a function in several variables, use
the Functionals::curry method:
$sum = function($a, $b, $c) { return $a + $b + $c; }; $a = Functionals::curry($sum); $b = $a(1); $c = $b(2); $c(10); // 13 $c(101); // 104
You can also uncurry a function. This time you have to specify the number of the original function arguments:
$uncurried = Functionals::uncurry($a, 3); $uncurried(5, 7, 11); //23
Combine and uncombine
Given a set of functions f, g, h, ... that act on the same domain,
a combined version of that functions is the function
x → array(f(x), g(x), h(x), ...)
In Functionals you can easily combine callables with Functionals::combine:
$stringVersions = Functionals::combine('strtolower', 'strtoupper', 'ucfirst'); $stringVersions('hElLo'); // array('hello', 'HELLO', 'HElLo')
Conversely, you can uncombine a function that returns array values. In this case you have to specify the number of items in the array values:
$ops = function($a, $b) { return array($a + $b, $a * $b, $a - $b); }; list($sum, $multiplication, $difference) = Functionals::uncombine($ops, 3); $sum(10, 5); // 15 $multiplication(10, 5); // 50 $difference(10, 5); // 5
Args to array and array to args
You can convert a function that accept several arguments to a function that accept a single array
argument through the functional Functionals::args_to_array().
For example, if you have the function in two variables f(x, y) with this functional you obtain
the function (in pseudo-code)
Functionals::args_to_array(f) : array(x, y) → f(x, y)
In php:
$sum = function($a, $b) { return $a + $b; }; $sum2 = Functionals::args_to_array($sum); $sum2(array(2, 10)); // 12
This functional can be useful in conjunction with composition, since the functions in a composition chain that are not in the last position can recieve only one argument:
$sum = function() { return array_sum(func_get_args()); }; $numbersUntil = function($n) { $numbers = array(); for ($i = 0; $i <= $n; $i++) $numbers[] = $i; return $numbers; }; $sumUntil = Functionals::compose( Functionals::args_to_array($sum), $numbersUntil ); $sumUntil(1); // 1 $sumUntil(5); // 15 $sumUntil(100); // 5050 (=100 + 101 / 2)
The inverse of the previous functional is Functionals::array_to_args():
$sum = function(array $numbers) { return array_sum($numbers); }; $sum2 = Functionals::array_to_args($sum); $sum2(1, 2, 3); //6 $sum2(10, 20, 3); //33
Diagonalization
See the What's new section.
TODO
- Some security checks on inputs and array sizes
- Performance considerations
- Give more useful/amusing examples?
- Automatically detect end of chain for uncurrying
Tests
$ phpunit
License
MIT, see LICENSE.
nicmart/functionals 适用场景与选型建议
nicmart/functionals 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 120 次下载、GitHub Stars 达 24, 最近一次更新时间为 2013 年 01 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「functionals」 「closures」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 nicmart/functionals 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 nicmart/functionals 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 nicmart/functionals 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
An implementation of the Closure Tables pattern for Eloquent.
Configurable DI for the ZF service manager. Modeled after Symfony 2's DI config files.
PHP Active Record for MySQL. Connects to the your database and generates the Object Relational Mapping. Built in Database Query Processor for custom queries. Ability to use closures to process rows
Configurable DI for the ZF service manager. Modeled after Symfony 2's DI config files.
Alfabank REST API integration
Serializable Closure is a PHP package that allows closures to be serialized and stored, enabling their safe use in persistent storage or across requests.
统计信息
- 总下载量: 120
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 25
- 点击次数: 10
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2013-01-06