Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/nunomaduro/curryable

An elegant and simple curry(f) implementation in PHP.
https://github.com/nunomaduro/curryable

curry functional-programming laravel php

Last synced: about 2 months ago
JSON representation

An elegant and simple curry(f) implementation in PHP.

Awesome Lists containing this project

README

        


Curryable


Build Status
Total Downloads
Latest Version
License

## 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](https://github.com/nunomaduro), 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+](https://php.net/releases/)**

Create your package using [Composer](https://getcomposer.org):

```bash
composer require nunomaduro/curryable
```

This helper usage is best described through example in the [Laravel](https://laravel.com) framework:

### On routing

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

// Instead of
Route::get('/', function () {
return view('welcome');
});
```

```php
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`:

```php
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`:
```php
$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`:
```php
// 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:
```php
dispatch(curry(Artisan::class)->call('horizon:terminate'));

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

### Curry on class instance methods

With global helper:

```php
$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`:

```php
$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

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

```php
// 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](CONTRIBUTING.md).

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

## Support the development
**Do you like this project? Support it by donating**

- Github sponsors: [Donate](https://github.com/sponsors/nunomaduro)
- PayPal: [Donate](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=66BYDWAT92N6L)
- Patreon: [Donate](https://www.patreon.com/nunomaduro)

## License

curryable is an open-sourced software licensed under the [MIT license](LICENSE.md).