2017 © Pedro Peláez
 

library php-curry

Curried functions in PHP

image

cypresslab/php-curry

Curried functions in PHP

  • Friday, March 10, 2017
  • by matteosister
  • Repository
  • 2 Watchers
  • 50 Stars
  • 1,031 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 6 Versions
  • 10 % Grown

The README.md

php-curry

Code Coverage Build Status SensioLabsInsight, (*1)

An implementation for currying in PHP, (*2)

Currying a function means the ability to pass a subset of arguments to a function, and receive back another function that accepts the rest of the arguments. As soon as the last one is passed it gets back the final result., (*3)

Like this:, (*4)

``` php use Cypress\Curry as C;, (*5)

$adder = function ($a, $b, $c, $d) { return $a + $b + $c + $d; };, (*6)

$firstTwo = C\curry($adder, 1, 2); echo $firstTwo(3, 4); // output 10, (*7)

$firstThree = $firstTwo(3); echo $firstThree(14); // output 20, (*8)


Currying is a powerful (yet simple) concept, very popular in other, more purely functional languages. In haskell for example, currying is the default behavior for every function. In PHP we still need to rely on a wrapper to simulate the behavior ## How to install ``` bash composer require cypresslab/php-curry

In your PHP scripts (with composer autoloader in place) just import the namespace and use it!, (*9)

``` php use Cypress\Curry as C;, (*10)

$chunker = C\curry('array_chunk', ['a', 'b']); var_dump($chunker(1)); // output [['a'], ['b']] var_dump($chunker(2)); // output [['a', 'b']], (*11)


### Right to left It's possible to curry a function from left (default) or from right. ``` php $divider = function ($a, $b) { return $a / $b; }; $divide10By = C\curry($divider, 10); $divideBy10 = C\curry_right($divider, 10); echo $divide10By(10); // output 1 echo $divideBy10(100); // output 10

Parameters as an array

You can also curry a function and pass the parameters as an array, just use the *_args version of the function., (*12)

``` php use Cypress\Curry as C;, (*13)

$divider = function ($a, $b) { return $a / $b; };, (*14)

$division = C\curry_args($divider, [100, 10]); echo $division(); // output 10, (*15)

$division2 = C\curry_right_args($divider, [100, 10]); echo $division2(); // output 0.1, (*16)


### Optional parameters Optional parameters and currying do not play very nicely together. This library excludes optional parameters by default. ``` php $haystack = "haystack"; $searches = ['h', 'a', 'z']; $strpos = C\curry('strpos', $haystack); // You can pass function as string too! var_dump(array_map($strpos, $searches)); // output [0, 1, false]

But strpos has an optional $offset parameter that by default has not been considered., (*17)

If you want to take this optional $offset parameter into account you should "fix" the curry to a given length., (*18)

``` php $haystack = "haystack"; $searches = ['h', 'a', 'z']; $strpos = C\curry_fixed(3, 'strpos', $haystack); $finders = array_map($strpos, $searches); var_dump(array_map(function ($finder) { return $finder(2); }, $finders)); // output [false, 5, false], (*19)


*curry_right* has its own fixed version named *curry_right_fixed* ### Placeholders The function `__()` gets a special placeholder value used to specify "gaps" within curried functions, allowing partial application of any combination of arguments, regardless of their positions. ```php $add = function($x, $y) { return $x + $y; }; $reduce = C\curry('array_reduce'); $sum = $reduce(C\__(), $add); echo $sum([1, 2, 3, 4], 0); // output 10

Notes:, (*20)

  • Placeholders should be used only for required arguments., (*21)

  • When used, optional arguments must be at the end of the arguments list., (*22)

The Versions

10/03 2017

dev-master

9999999-dev

Curried functions in PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

The Development Requires

curry functional-programming

10/03 2017

0.5.0

0.5.0.0

Curried functions in PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

The Development Requires

curry functional-programming

05/07 2016

0.4.0

0.4.0.0

Curried functions in PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

The Development Requires

curry functional-programming

27/06 2016

0.3.0

0.3.0.0

Curried functions in PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

The Development Requires

curry functional-programming

31/07 2015

0.2.0

0.2.0.0

Curried functions in PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

The Development Requires

curry functional-programming

09/02 2015

0.1.0

0.1.0.0

Curried functions in PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

The Development Requires

curry functional-programming