Currying a function

Currying a function

Post by Rico Huijb » Thu, 11 Nov 2004 00:47:30


I'd like to know if it's possible to curry a function in PHP? That is,
is there some built-in mechanism for it, or is it possible to create a
function that does the currying?

I've tried something myself, and come up with the following:


function curry($fnc, $args) {
$callargs = "";
foreach ($args as $arg) {
if (is_string($arg))
$callargs .= '"' . $arg . '"';
$callargs .= $arg;

$callargs .= ', ';

$callargs .= '$x';

return create_function('$x', sprintf('return %s(%s);', $fnc,


Basically, this function builds a new (lambda) function that calls the
previous function, but with the parameters to the curry transformed to
literals (causing the number of arguments to be reduced). This works
fine; I can now do the following:


function times($a, $b) {
return $a * $b;

$t5 = curry("times", array(5));

echo $t5(3); // Outputs `15'


This works alright, but only for strings and integers. At the moment, I
need to curry an object, and the function only receives the string
`Object' as a parameter (which is of course logical). And actually, the
method I used has another limitation; the resulting function only has
one parameter left. For what I'm using it right now, that's not a
problem, but it's not very generic.

Ultimately, I guess that for my purposes, I could resort to using
classes instead of functions, but I would very much like to get the
currying approach working, if at all possible.

So my question is: can currying be done in PHP with any variable type
(and preferably with any amount of parameters)?

Thanks in advance.

Kind regards,
Rico Huijbers

Currying a function

Post by Brandon Bl » Thu, 11 Nov 2004 00:56:01

For people who (like myself) have never heard of "currying" before
today, here is a link which explains what "currying" is (it does not
have anything to do with Indian cuisine):

In this case, I think using classes would be much easier, but perhaps it
is because those are familiar to me, while "currying" is not.



Currying a function

Post by Phil Rober » Thu, 11 Nov 2004 01:42:08

Rico Huijbers < XXXX@XXXXX.COM > treated the
lovely people of comp.lang.php with the following stuff:

Take a look at the func_get_args() function. If you haven't already
that is.

Phil Roberts |

You're wrong. And you're a grotesquely ugly freak.

Currying a function

Post by Andy Hassa » Thu, 11 Nov 2004 04:47:46

n Tue, 09 Nov 2004 16:47:30 +0100, Rico Huijbers
< XXXX@XXXXX.COM > wrote:

Not built in.

Looks like a good start.

You're building up your function at the moment with string concatenation -
which as well as not handling objects, may well fall over with escaping issues
(quotes and backslashes and so on).

What about using serialisation, since that can store objects; pass the
serialised form of the curried arguments into the lambda function, which then
at call time deserialises them and appends any further arguments, before
calling the original base function.

How about:

function curry($fnc) {
$args = func_get_args();

$lambda = sprintf(
'$args = func_get_args(); ' .
'return call_user_func_array(\'%s\', array_merge(unserialize(\'%s\'),
$fnc, serialize($args)
return create_function('', $lambda);

function times($a, $b) {
return $a * $b;

$t5 = curry('times', 5);
echo $t5(3); // Outputs '15'

print "<hr>";

class ExampleObject {
var $attr = 1;

$obj = new ExampleObject();

function printAttr($title, $obj)
print "$title {$obj->attr}<br>";

$paAttr = curry('printAttr', 'title');
echo $paAttr($obj); // outputs 'title 1'

print "<hr>";

function add($a, $b, $c, $d)
return $a + $b + $c + $d;

$a = curry('add', 1, 2);
echo $a(3, 4); // outputs 10

Andy Hassall / < XXXX@XXXXX.COM > / <>
<> Space: disk usage analysis tool

Currying a function

Post by Rico Huijb » Thu, 11 Nov 2004 08:06:56

You are truly a master of the black arts ;).

Thanks a bunch! Using serialization would never have occurred to me.
Your solution is great, though. I noticed you didn't try including an
object in the curry, but I just tested it and it works like a charm as
well (except for the minor detail that it will be a copy, and not a

I like it. Thanks again.


Currying a function

Post by Andy Hassa » Thu, 11 Nov 2004 09:04:21

On Wed, 10 Nov 2004 00:06:56 +0100, Rico Huijbers

Been a while since I'd last heard of currying, since university. We did
currying in the Haskell functional programming course, appropriate since both
Haskell and currying are named after the same person, Haskell Curry.

I'm not sure if there's a way you can pass a reference into the curry, since
serialisation always makes a copy. Perhaps you could do it if you resorted to
storing it in a global array and passing the key in - *bleh* :-)

Andy Hassall / < XXXX@XXXXX.COM > / < >
< ; Space: disk usage analysis tool

Currying a function

Post by Brion Vibb » Thu, 11 Nov 2004 16:34:36

Remember that you can create a callable object+method reference by
array( &$object, 'methodname' ). Objects can have member variables which
can be references...

-- brion vibber (brion @

Currying a function

Post by Andy Hassa » Fri, 12 Nov 2004 04:37:46

But you can't get that into the curry; you've got to get it across the
create_function() interface, which means it's got to go as a string. You're
then still serialising it and so end up with a copy on the other side?

Andy Hassall / < XXXX@XXXXX.COM > / < >
< ; Space: disk usage analysis tool

Currying a function

Post by Brion Vibb » Fri, 12 Nov 2004 09:01:18

create_function() shouldn't be necessary; the function itself is a
pretty generic wrapper so a single class with a predefined method should
be sufficient. The bigger problem is that func_get_args() doesn't pass
through references, at least in PHP4.

-- brion vibber (brion @