Wednesday, January 6, 2010

PHP -- much better than using switch statements

PHP's syntax is manic muddled mess. It is not object oriented programming, but there are a few things that make it easy to do things better than the average hacker.

Function pointers, if you are a C person, Funs, if you are an Erlang cat, Procs or lambdas for Ruby, delegates in C#, lambdas in my beloved Python is what I am talking about.

PHP too, supports this, but in a completely straight forward way.

take the following PHP code:

function iMRichBitch() { compute48thMersennePrime(); }


you can simply refer to pass around a reference (or pointer) to 'iMRichBitch' using a string by the same name. Of course, when the function is to be invoked, you simply can say:

$func = 'iMRichBitch';
$func();


So this begs the question, "if I have function pointers", then I ought to use those instead of case statements and maybe even nested conditionals, right?" The answer is a big 'YES'.


so instead of this:

switch($conditional) { case 1: do1(); break; case 2: do2(); break; default: break; }


you might think about doing this (with a little defensive code for garnishment):

$conditional();

Sunday, January 3, 2010

Lambda Calculus

Can anyone fully understand lambda calculus? If so, what would they do with that power?