Tuesday, August 24, 2010

transmogrified function composition

Using the transmogrifier something like function composition such as:


(define (compose . fs)
(if (null? fs) (lambda (x) x)
(lambda (x) ((car fs) ((apply compose (cdr fs)) x)))))


Becomes possible like:

$compose = [$funcs |
empty($funcs) ? [$_] :
[car($funcs)($compose(cdr($funcs))($_))]];


Which becomes:

$compose = function($funcs) use(&$compose) {
return empty($funcs) ? function($x) { return $x; } :
function($_ = false) use(&$funcs, &$compose) {
return apply(car($funcs), array(apply($compose(cdr($funcs)), array($_))));};};


I am not doing an exact copy here because I am not using ". rest" args as I would probably call this function like:


echo $compose({square negate cube})(500);
//shorter than
echo $compose('square', 'negate', 'cube')(500);


I think the above illustrates that really php IS capable of these sorts of things it's just that in it's incredibly gnarly. With a few syntax tweaks (square bracket closures w/ lexical scope, squiggly bracket arrays w/ barewords as strings, ability to use return value as function) - it's almost livable.

Friday, August 13, 2010

php quasiquote preprocessor

Sometimes itches must be scratched. Last nights musing leads to tonights finished quasiquote for php: http://github.com/shaunxcode/php-quasiquote-preprocessor. It was nice to implement just quasiquote ignoring the rest of the lisp reader as before I had tried to "Add" quasiquote to my nearly complete lisp. As I discovered this was a mistake. In the future I am going to start the reader with quasiquote and build as much of the system with macros as possible.

Thursday, August 12, 2010

Refactoring Voltron query DSL

This is an area of the framework I have not had a need to look at in a while and from this latest inspection it was blatantly not very well thought out. Admitting your own fallibility, I think, is probably the first step to being able to refactor your own code. Previously I had decided I really wanted to avoid seeing stuff like:


$UserModel->getWhere(array('type' => 'and', value' => array(
array('type' => '=', 'field' => 'name', 'value' => 'shaun'),
array('type' => 'or', 'value' => array(
array('type' => 'between', 'field' => 'age', 'value' => array(69, 100)),
array('type' => 'like', 'field' => 'l_name', 'value' => '%name%'))))));


So I went with a half baked dsl


$UserModel->getWhere(Q::andWhere(
Q::is('name', 'shaun'),
Q::orWhere(
Q::is('age', Q::between(array(69, 100))),
Q::is('l_name', Q::like('%name%')))));


On first inspection that is not too bad but it rapidly gets out of control if you need to actually do something more in depth (nested or statements etc.). Not only that but all of the :: is pretty ugly as is the fact I can't use words like 'and', 'or' '=' etc. as method names. I also had this idea that "is" should be used for all operators. I have now abandoned that in the name of consistency and to reduce the complexity of the code which actually generates the sql.

Taking a prefix notation approach and refactoring W (for where instead of Q for query, leaving room for say F for the from stuff for defining joins and S for select) to merely be a global function which returns arrays like in the first example:


$UserModel->getWhere(W('and',
W('=', 'name', 'shaun'),
W('or',
W('between', 'age', 69, 100),
W('like', 'l_name', '%name%'))));


I pine for quasiquote:

$userModel->getWhere(
`(and
(= name shaun)
(or
(between age 69 100)
(like l_name %name%))));

Tuesday, July 27, 2010

quasiquote for primitive lisp

I was searching for a basic quasiquote implementation in lisp which mentioned a paper by Alan Bawden. Anyway it was difficult to find via google so I figured I would type it up here for posterity. The original paper can be found: here.


(define (qq-expand x)
(cond ((tag-comma? x)
(tag-data x))
((tag-comma-atsign? x)
(error "Illegal"))
((tag-backquote? x)
(qq-expand (qq-expand (tag-data x))))
((pair? x)
`(append ,(qq-expand-list (car x))
,(qq-expand (cdr x))))
(else '', x)))

(define (qq-expand-list x)
(cond ((tag-comma? x)
`(list ,(tag-data x)))
((tag-comma-atsign? x)
(tag-data x))
((tag-backquote? x)
(qq-expand-list (qq-expand (tag-data x))))
((pair? x)
`(list (append ,(qq-expand-list (car x))
,(qq-expand (cdr x)))))
(else ''(,x))))

Monday, July 19, 2010

PHP to javascript fluent expressions

There are many times in Voltron views where I need to make a jquery/javascript call, initially I compromised and created the UI::ScriptSnippet in which I would put the typical "$(document).ready(function(){})" type deal in place. This was becoming increasingly more obnoxious particularly when the rest of the view code was so clean. So I give you the JSCallBuilder:


UI::JSCall('object')->method(array('a' => 'b'));
// renders to object.method({'a': 'b'});

UI::JSReady('object')->method(array('a' => 'b'));
//renders to <\script type="text/javascript">$(document).ready(function(){ object.method({'a', 'b'}) });<\/script>
*/note the escaped script tags for bloggers sake? */


The source to make this happens is relatively trivial: http://code.google.com/p/phpviewadapter/source/browse/trunk/UI/JSCallBuilder.php

Saturday, July 17, 2010

solving the word cube in voltron

In response to: http://programmingpraxis.com/2010/07/13/word-cube/


/* returns array of words found in the 9 letters provided */
function solveCube($letters) {
$words = newType(TFile, 'words.txt')->splitBy("\n");

return newType(TString, $letters)
->asArray
->powerSet('contains', $letters[4])
->map('permutations', I('asString')->in($words))
->map('join');
}

echo solveCube('ncbciune');


There are a few alternative ways that could be written and I am not sure if the idea of just having an optional filter as arguments to powerSet and permutations is entirely intuitive but the reality is that w/o the filter permutations will die on anything over 8 unless you crank the memory up in php.ini and thats just filthy.

Next post I will delve into iterative permutation and powerset internals on Array as that is obviously where the meat of that solution lies.

Monday, July 5, 2010

voltron model calculated fields

In the field definition of a voltron model you usually specify the field and the type as a key => val associative array. In the case that you have a calculated field the current practice is to specify field => array(Type::Calculated, 'methodNameOnRecord'). With fluent lambdas I have added a short cut:


class TimeRecord extends Voltron_Model
{
protected $table = 'time_record';

protected $fields = array(
'id' => Type::Primary,
'created' => Type::Timestamp,
'created_hour' => Type::Calculated('created')->asDateTime->formatAs('H'));
}


The magic lies with in the created_hour type definition which can also be written as Type::Calculated(I()->created->asDateTime->formatAs('H')).

Because of the oo and fluent nature of Voltron the majority of calculated fields can be defined this way rather than requiring an actual method definition in the record class.