Functional Programming Helpers

Installs: 0

Dependents: 0

Stars: 13

Watchers: 2

Forks: 2

0.2.1 2014-08-12 23:39 UTC



A curryable function returns a new function when called with less arguments than the curryable function requires. The new function returned will have the arguments applied, and will also be a curryable function.

This programming pattern can be used to build up more complex functions from less complex functions.


// Create a curryable function
$concat = fp\curry(function ($a, $b) { return $a . $b; });

// Create a new function with 'Mr. ' applied
$addTitle = $concat('Mr. ');

echo $addTitle('Spiers');
// Mr. Spiers


$h = fp\compose($f, $g);

Function composition will return a new function ($h) which will first apply the second function ($g), pass its result into the first ($f).


Turning normal functions into curryable functions

$map = fp\curry('array_map');

Create non-closure functions that are curryable

function _tag($tag, $text) {
    return "<$tag>$text</$tag>";

function tag(...$args) {
    return fp\curry('_tag')->__invoke(...$args);

// We now have a paragraph function
$p = tag('p');

// We now have a div function
$div = tag('div');

echo $div($p("Some text"));
// <div><p>Some text</p></div>