Sunday, December 7, 2008

duality of syntax

So I have hit a bit of a conondrum with my current approach to introducing a new syntax for lambdas in LET/php.

The problem comes from the current use of square brackets to access array elements mixed with my desire for them to be lambda expressions. Consider the following:

$array = (a b c d);
$array [0]; #a

#the function expecting the yield block is passed a block which returns 0
$expecting_yield [0];

#one solution as we have dropped ( and ) for calling functions would be using
#that for array indexes, this would sort of fit with the fact that it is used
#for defining arrays.

$array = (a b c d);
$array(0); #a

$array (a:(b:(c:cat)));
$array(a)(b)(c); #cat

#what about methods stored in arrays?
$number = let($num = 5){
(mul:[$x | $x * $num]

#note that _ is for anon vars and _1 _2 is if there are many

$number (mul) 10; #50
#that feels odd.

#using magic __get and keyword param
$number.mul x:10;

#that works well for keys which are strings but what of numeric index?
$array = (1 2 3 4);
$array.0 #1

#perhaps we could be clever enough to turn numeric dots into

#if all arrays are objects then why would you not use an accessor method?
$array = (1 2 cat 4);

#or using an accessor method - thats only one extra char (the .)
$ 2; #cat

#maybe removing the [ ] notation will encourage functional programming
#instead of iterating over for loops anyway?

#but what about look ups in a hash table?
$actions = (cat:walks rat:crawls bat:flies);
(cat rat bat).each [$creature |
echo "the $creature {$actions.$creature}";

#normal php
$actions = Array('cat'=>'walks', 'rat'=>'crawls', 'bat'=>'flies');
foreach(Array('cat','rat','bat') as $creature){
echo "the $creature {$actions[$creature]}";

No comments: