Hello,
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 . '"';
else
$callargs .= $arg;
$callargs .= ', ';
}
$callargs .= '$x';
return create_function ('$x', sprintf('return %s(%s);', $fnc,
$callargs));
}
---
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
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 . '"';
else
$callargs .= $arg;
$callargs .= ', ';
}
$callargs .= '$x';
return create_function ('$x', sprintf('return %s(%s);', $fnc,
$callargs));
}
---
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
Comment