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.