Topics

programming

php drupal scheme scheming macros design patterns da la

design

design css

random thoughts

scribbles

alter ego

other me 'em that link us my space me linked in

Collections

Programmable web
PHP design patterns

Similar things

  • Random thoughts on web application internals
  • Emulating closures in PHP
  • Some ways to use saved state with closures in php
  • Wizardy, druidy, wiz
  • An alternative for multipage forms
  • da code, types of functions
  • x|s|p expressions mixing
  • aspects and honey in php
  • Scoped php functions or more ways to abuse php
  • Updated design pattern collection

Home » blogs » vlado's blog

Partial evaluation and generics in php

Submitted by vlado on Mon, 2006-07-03 13:11.php | programming | scheming | very shorts

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.

simple partial evaluation

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.

generic functions and algorithms

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

vlado's blog | add new comment
Home » blogs » vlado's blog

dikini.net

spreading confusion by accident since 1970