nunomaduro/curryable

PHP 中优雅且简单的 curry(f) 实现。

dev-master 2021-11-21 18:23 UTC

This package is auto-updated.

Last update: 2024-09-22 01:01:14 UTC


README

Curryable

Build Status Total Downloads Latest Version License

关于 Curryable

此包处于开发中,请勿在生产环境中使用,并等待稳定版发布!

Curryable 由 Nuno Maduro 创建并维护,是一个 PHP 中的优雅且简单的 curry(f) 实现。Currying 是一种高级的函数处理技术。它 将给定的表达式和参数包装成一个新函数,以解决一个值。

安装 & 使用

需要 PHP 7.2+

使用 Composer 创建您的包

composer require nunomaduro/curryable

此辅助器的用法最好通过 Laravel 框架中的示例来描述

在路由中

Route::get('/', curry('view', 'welcome'));

// Instead of
Route::get('/', function () {
    return view('welcome');
});
Route::get('user/{id}', curry(User::class)->find());
// Or with Eloquent macro
Route::get('user/{id}', User::curry()->find());

// Instead of
Route::get('user/{id}', function ($id) {
    return User::find($id);
});

在宏定义中

lower 方法重命名为 toLower

Str::macro('toLower', curry()->lower());
// or with the global `strtolower`
Str::macro('toLower', curry('strtolower'));

// Instead of
Str::macro('toLower', function ($value) {
    return Str::lower($value);
});

在集合中

使用全局的 strtoupper

$collection = collect(['nuno'])->map(curry('strtoupper')); // ['NUNO']

// Instead of
$collection = collect(['nuno'])->map(function ($name) {
    return strtoupper($name);
});

这是另一个使用 each 的示例

// Calls User::create($user) foreach user
collect($users)->each(User::curry()->create());

// Instead of
$collection = collect($users)->map(function ($user) {
    return User::create($user);
});

调度作业

dispatch(curry(Artisan::class)->call('horizon:terminate'));

// Instead of
dispatch(function () {
    Artisan::call('horizon:terminate');
});

在类实例方法上使用 Curry

使用全局辅助器

$closure = curry($instance)->instanceMethodName();
$closure($first, $second);

$closure = curry($instance)->instanceMethodName($first);
$closure($second); // just need for the second argument

$closure = curry($instance)->instanceMethodName($first, $second);
$closure(); // no need for arguments

使用特性 NunoMaduro\Curryable\Curryable

$closure = $instance->curry()->instanceMethodName();
$closure($first, $second);

$closure = $instance->curry()->instanceMethodName($first);
$closure($second); // just need for the second argument

$closure = $instance->curry()->instanceMethodName($first, $second);
$closure(); // no need for arguments

在类静态方法上使用 Curry

// Curry on instance methods
$closure = curry(Instance::class)->staticMethodName();
$closure($first, $second);

$closure = curry(Instance::class)->staticMethodName($first);
$closure($second); // just need for the second argument

$closure = curry(Instance::class)->staticMethodName($first, $second);
$closure(); // no need for arguments

在函数上使用 Curry

// Curry on instance methods
$closure = curry('function_name');
$closure($first, $second);

$closure = curry('function_name', $first);
$closure($second); // just need for the second argument

$closure = curry('function_name', $first, $second);
$closure(); // no need for arguments

贡献

感谢您考虑为 Curryable 做出贡献。所有贡献指南都在 此处 提到。

您可以通过查看 CHANGELOG 获取持续更新和关于更改的详细信息。您还可以关注推特账号以获取最新公告或只是打个招呼:@enunomaduro

支持开发

喜欢这个项目吗?通过捐赠支持它

许可协议

curryable 是一个开源软件,根据 MIT 许可协议 许可。