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
- Host: GitHub
- URL: https://github.com/phpfn/curry
- Owner: phpfn
- License: mit
- Created: 2018-04-13T21:23:51.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2020-11-22T21:18:15.000Z (about 5 years ago)
- Last Synced: 2025-08-04T04:24:37.481Z (6 months ago)
- Topics: curry, functional, partial-application, php
- Language: PHP
- Homepage: https://github.com/phpfn/phpfn
- Size: 19.5 KB
- Stars: 6
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
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