php curry,Curryable - PHP中优雅而简单的curry(f)实现

0e13315a5a3808495368ced9ee32fd45.png

68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f6e756e6f6d616475726f2f637572727961626c652f6d61737465722e73766768747470733a2f2f706f7365722e707567782e6f72672f6e756e6f6d616475726f2f637572727961626c652f642f746f74616c2e73766768747470733a2f2f706f7365722e707567782e6f72672f6e756e6f6d616475726f2f637572727961626c652f762f737461626c652e73766768747470733a2f2f706f7365722e707567782e6f72672f6e756e6f6d616475726f2f637572727961626c652f6c6963656e73652e737667

About Curryable

This package is under development, please don't use it on production and wait for the stable release!

Curryable was created by, and is maintained by Nuno Maduro, and is an elegant and simple curry(f) implementation in PHP. Currying is an advanced technique of working with functions. It wraps the given expressions and arguments into a new function that resolves a value.

Installation & Usage

Requires PHP 7.2+

Create your package using Composer:

composer require nunomaduro/curryable

This helper usage is best described through example in the Laravel framework:

On routing

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);

});

On macros

Renaming the lower method to 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);

});

On collections

Using the global strtoupper:

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

// Instead of

$collection = collect(['nuno'])->map(function ($name) {

return strtoupper($name);

});

Here is another example using the 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);

});

Dispatching jobs:

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

// Instead of

dispatch(function () {

Artisan::call('horizon:terminate');

});

Curry on class instance methods

With global helper:

$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

With trait 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 on class static methods

// 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 on functions

// 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

Contributing

Thank you for considering to contribute to Curryable. All the contribution guidelines are mentioned here.

You can have a look at the CHANGELOG for constant updates & detailed information about the changes. You can also follow the twitter account for latest announcements or just come say hi!: @enunomaduro

Support the development

Do you like this project? Support it by donating

Github sponsors: Donate

PayPal: Donate

Patreon: Donate

License

curryable is an open-sourced software licensed under the MIT license.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值