Function composability is a core concept in functional programming that allows building complex operations from simpler ones. Phunkie provides several tools and patterns for working with composable functions.
Pure functions are functions that:
Example of a pure function:
// Pure function
$add = fn(int $a, int $b): int => $a + $b;
// Always returns the same result for same inputs
$add(2, 3); // 5
$add(2, 3); // 5 (always)
Higher-order functions are functions that either:
use Phunkie\Types\Function1;
// Function that takes a function as parameter
$map = Function1(fn($f) => fn($x) => $f($x));
// Function that returns a function
$multiply = fn($x) => fn($y) => $x * $y;
$times2 = $multiply(2);
$times2(4); // 8
Currying is the technique of converting a function that takes multiple arguments into a sequence of functions that each take a single argument. Phunkie provides built-in support for currying:
use function Phunkie\Functions\currying\curry;
use function Phunkie\Functions\currying\uncurry;
// Currying a two-argument function
$add = fn($a, $b) => $a + $b;
$curriedAdd = curry($add);
// Now we can partially apply arguments
$add5 = $curriedAdd(5);
$result = $add5(3); // 8
// Uncurrying converts back to multi-argument form
$normalAdd = uncurry($curriedAdd);
$result = $normalAdd(5, 3); // 8
// Partial application with placeholder
$result = applyPartially(
['message'],
['Hello'],
fn($msg) => "$msg World"
);
Function composition allows you to combine multiple functions into a single function. Phunkie provides several ways to compose functions:
use Phunkie\Types\Function1;
// Basic function composition
$f = Function1(fn($x) => $x + 1);
$g = Function1(fn($x) => $x * 2);
// Forward composition (f andThen g)
$h = $f->andThen($g);
$result = $h(5); // (5 + 1) * 2 = 12
// Backward composition (f compose g)
$h = $f->compose($g);
$result = $h(5); // (5 * 2) + 1 = 11
// Combining functions with semigroup
use function Phunkie\Functions\semigroup\combine;
$increment = fn($x) => $x + 1;
$double = fn($x) => $x * 2;
$subtract3 = fn($x) => $x - 3;
$composed = combine($increment, $double, $subtract3);
$result = $composed(5); // ((5 + 1) * 2) - 3 = 9
Function1 in Phunkie implements several type classes that enable powerful function compositions:
$f = Function1(fn($x) => $x + 1);
$g = $f->map(fn($x) => $x * 2); // Same as andThen
$f = Function1(fn($x) => $x + 1);
$g = Function1(fn($x) => fn($y) => $x + $y);
$h = $f->apply($g);
$f = Function1(fn($x) => $x + 1);
$g = Function1(fn($x) => $x + 1);
$areEqual = $f->eqv($g, Some(42)); // true