Logo

Programming-Idioms

History of Idiom 37 > diff from v31 to v32

Edit summary for version 32 by Hayley:
New PHP implementation by user [Hayley]

Version 31

2018-09-06, 19:58:30

Version 32

2018-09-19, 00:07:19

Idiom #37 Currying

Transform a function that takes multiple arguments into a function for which some of the arguments are preset.

Idiom #37 Currying

Transform a function that takes multiple arguments into a function for which some of the arguments are preset.

Extra Keywords
curry
Extra Keywords
curry
Code
function curry($f, ...$argsCurried) {	
	return function(...$args) use($f, $argsCurried) {
                return $f(...$argsCurried, ...$args);
	};
}

function add($n1, $n2) {
	return $n1 + $n2;
}

$addFive = curry('add', 5);

echo $addFive(2), PHP_EOL;
echo $addFive(-5), PHP_EOL;