nemorize/curried
最新稳定版本:0.0.1
Composer 安装命令:
composer require nemorize/curried
包简介
A utility to create curried functions that its parameters are reordered by user-defined order.
README 文档
README
A utility to create curried functions that its parameters are reordered by user-defined order.
Inspired by https://github.com/andjsrk/create-curried.
Requirements
- PHP 8.1 or later
Installation
composer require nemorize/curried
Usage
Curried
Curried::from(Closure $fn): Context
Returns a Context for a given closure.
use Nemorize\Curried\Curried; $context = Curried::from(function ($foo, $bar) { return $foo . $bar; });
Context
Context::takes(int $position): Context
Takes a parameter at the given position.
use Nemorize\Curried\Curried; $context = Curried::from(function ($foo, $bar) { return $foo . $bar; })->takes(0)->takes(1);
Context::takesRest(): Context
Takes the rest of parameters.
use Nemorize\Curried\Curried; $context = Curried::from(function ($foo, $bar, $baz) { return $foo . $bar . $baz; })->takes(0)->takesRest();
Context::withStatic(int $position): Closure
Binds a parameter at the given position to the closure.
Returned closure returns a Context for the next parameter.
use Nemorize\Curried\Curried; $context = Curried::from(function ($foo, $bar) { return $foo . $bar; })->takes(0)->withStatic(1)('bar');
Context::generate(): Closure
Generates a curried function.
use Nemorize\Curried\Curried; $curried = Curried::from(function ($foo, $bar) { return $foo . $bar; })->takes(0)->takes(1)->generate(); $curried('foo')('bar'); // 'foobar'
Examples
Plus one to each element of an array
use Nemorize\Curried\Curried; $map = Curried::from(array_map(...)) ->takes(0) ->takes(1) ->generate(); $plusOneEach = $map(fn (int $x) => $x + 1); $plusOneEach([1, 2, 3]); // [2, 3, 4]
Sum of an array
use Nemorize\Curried\Curried; $sum = Curried::from(array_reduce(...)) ->takes(1) ->withStatic(0)(fn (int $x, int $y) => $x + $y) ->generate(); $sum([1, 2, 3]); // 6
统计信息
- 总下载量: 7
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-02-17