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

https://github.com/phpfn/curry

[READONLY] Convenient implementation of function currying and partial application
https://github.com/phpfn/curry

curry functional partial-application php

Last synced: 30 days ago
JSON representation

[READONLY] Convenient implementation of function currying and partial application

Awesome Lists containing this project

README

          

# Curry

Convenient implementation of function
currying and partial application.

- [Installation](#installation)
- [Usage](#usage)
- [Left currying](#left-currying)
- [Right currying](#right-currying)
- [Partial application](#partial-application)
- [Api](#api)
- [Functions](#functions)
- [Methods of the curried function](#curried)

## Installation

Library can be installed into any PHP application:
- Using [`Composer`](https://getcomposer.org/) dependency manager
- [The Force](https://www.youtube.com/watch?v=o2we_B6hDrY) for the Jedi Developers

```sh
$ composer require phpfn/curry
```

In order to access library make sure to include `vendor/autoload.php`
in your file.

```php
lcurry(42); // ($a = 42) + ($b = 3) * ($c = ?)
$fn->rcurry(23); // ($a = ?) + ($b = 3) * ($c = 23)
```

```php
$fn = \curry(function($a, $b, $c) { return $a + $b * $c; });

$sum = $fn(7, 9); // 7 + 9 * ?
$sum(6); // 7 + 9 * 6

$mul = $fn(_, 7, 9); // ? + 7 * 9
$mul(6); // 6 + 7 * 9

$test = $fn(_, 7, _); // ? + 7 * ?
$test(6); // 6 + 7 * ?

$test = $fn(_, 7); // ? + 7 * ?
$test->rcurry(6); // ? + 7 * 6
```

## Api

### Functions

- `lcurry(callable $fn, ...$args): Curried` or `curry`
> Left currying (like array_push arguments)

- `rcurry(callable $fn, ...$args): Curried`
> Right currying

- `uncurry(callable $fn): mixed`
> Returns result or partially applied function

### Curried

- `$fn->__invoke(...$args)` or `$fn(...$args)`
> The magic method that allows an object to a callable type

- `$fn->lcurry(...$args)`
> A method that returns a new function with left currying

- `$fn->rcurry(...$args)`
> A method that returns a new function with right currying

- `$fn->reduce()`
> Reduction of the composition of functions. Those. bringing the function to a single value - the result of this function.

- `$fn->uncurry()`
> An attempt is made to reduce, or bring the function to another, which will return the result

- `$fn->__toString()`
> Just a function dump