Download the PHP package phamda/phamda without Composer
On this page you can find all versions of the php package phamda/phamda. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download phamda/phamda
More information about phamda/phamda
Files in phamda/phamda
Informations about the package phamda
Phamda
Phamda is an auto-curried function library for PHP, heavily inspired by the Javascript library Ramda. PHP 7.0+ or HHVM is required.
Installation
Using composer: composer require phamda/phamda
Documentation
Documentation is available on Read the Docs.
Examples
These examples highlight the major features of Phamda. Basic usage examples can also be found on the function list.
Currying
Nearly all of the functions use automatic partial application or currying. For example you can call the
filter
function with only the predicate callback and get a new function:
The final result is the same as using two arguments directly. Of course this new function could now be used to filter other lists as well.
It's also possible to create new curried functions, including from native PHP functions. The curry
function
takes a function and initial parameters and returns a new function:
Composition
Phamda functions are composable. The basic functions can be used to create new, more complex functions. There are
also several functions to help with function composition. For example the compose
function takes multiple argument
functions and returns a new function. Calling this new function applies the argument functions in succession:
Often the pipe
function is a more natural way to compose functions. It is similar to compose
, but the argument
functions are applied in reverse order:
Parameter order
When using functional techniques it's usually most convenient if data is the last parameter. Often native PHP and
library functions do not follow for this pattern. Phamda includes some tools to make it easier to use these functions
functionally. The simplest is flip
, it switches the order of the first two parameters:
twist
is somewhat more complicated and will return a new function where the original first parameter is now last:
Using twist
may not work well with variadic functions. This is where twistN
can be useful. It requires an additional
parameter to set the location of the replaced parameter.
All of these functions return curried functions.
Pipelines
Combining these techniques allows the building of function pipelines. In this example they are applied to processing a list of badly formatted product data:
License
MIT license, see LICENSE file.