Currying¶
Currying is the transformation of a function that takes multiple arguments into a sequence of functions that each take a single argument. The name comes from mathematician Haskell Curry.
In a language with native currying, calling a multi-argument function with fewer arguments than it expects does not produce an error: it returns a new function that expects the remaining arguments. This allows building specialised functions from general ones with no extra syntax.
Native currying is available in Haskell, F#, OCaml, and Erlang, where all functions are curried by default. Scala and Kotlin support it explicitly.
PHP does not support currying natively. It can be approximated by returning closures manually, but there is no syntactic or runtime support.
<?php
// Manual simulation of currying with closures
$add = fn($x) => fn($y) => $x + $y;
$add5 = $add(5);
echo $add5(3); // 8
?>
See also Currying in Haskell and Currying in F#.
Related : Partial Function, Anonymous Function, Arrow Functions, Functional Programming, Closure