This is a followup of a few of my previous posts (lisp-like programming, aspects and honey, patterns & languages, closures), which describe some programming idioms typical for lisp-like languages.
Here I will add some very short examples for how to do partial evaluation in php. The most straight forward approach would be something like:
function peval() {
$symbol = gen_symbol( $args = function_get_args() );
$f_ind = count($symbol);
return create_function('',"
global \$symbol;
return call_user_function_array(array_pop(\$symbol[$f_ind]), \$symbol);
');";
}
gen_symbol() is a utility function which generates a unique global symbol. And before you start, yes, this technique does pollute the global scope with extra symbols. So what? As long as we don't notice it why should we care. Just imagine it's hidden somewhere by magic.
If we presume a definition of generic functions that they implement an algorithm, which later might be specialised for a type we might get something like:
function generic_function_1($func, $arg1, $arg2) {
.... do something ....
$func( $arg1, $arg2 );
.... do something ....
}
It is not something new, quite a few functions in php are using this idiom anyway. the type specialiastion is done via the $func variable which must hold a callable value. The only 'strict' requirements are for the arguments of the famililyt of $func - like functions. Using this techniques, we could for example, implement algebraic datatypes in php. For an example usage have a look at aspects and honey