$list = (1 2 3);
$list.each {$x | echo $x};
$number = let ($x = 10){
(add:{$y | $x += $y}
sub:{$y | $x -= $y});
#note if we wanted the method to be named add: we would do 'add:':{$y | $x += $y}
}
$number.add 10; #20
$number.sub 5; #15
#And from last time:
$decide = {$char | $char == a ? sum : mul};
$num = 10;
$letter = a;
$result = [$decide char:$letter] 6 6 ($num + 50);
Notice in the last one that this makes square brackets the equivelant of "apply" and keeps open brackets for grouping and or boolean operation which is what people coming from php would expect. The { } also feels more natural for anon code blocks for php-centric people as well.
Another approach would be using square brackets for "messages" sort of like obj c but more like sugar for dropping dots in objects i.e.
$range = (1..5);
[$list reverse each {echo _}]
#becomes
function block_name($arg0){echo $arg0;}
$list->reverse->each('block_name');
No comments:
Post a Comment