Allen's lambda syntax proposal

Maciej Stachowiak mjs at apple.com
Wed Dec 3 12:17:49 PST 2008


On Dec 2, 2008, at 6:57 PM, Brendan Eich wrote:

>
> This loses almost all connection with the rest of the language.  
> Arguments are passed in a comma separated list. Wrapped in  
> parentheses. The Smalltalk hommage loses the parens but keeps the  
> commas. Any other separator is just wrong, sorry.
>
> C# uses (a, b, c) => ... but in JS the comma operator makes that  
> nasty to parse top-down. I think the only candidates have to be of  
> the form
>
> ^(a, b, c) {...}
>
> (^ could be another character, but it seems to beat \ as others have  
> noted), or else the Smalltalky
>
> { |a, b, c| ... }
>
> At this point we need a bake-off, or a convincing argument against  
> the unusual vertical bar usage.

As a potential intuition pump, here's an example of some existing  
built-in JS control structures, and how one would write the equivalent  
with either form of lambda syntax. I am assuming the ^ version allows  
omitting the parameter list when empty. They are formatted to look as  
much as practical like the standard syntax.


if (x < 3) {
     handleSmallNum(x);
} else {
     handleLargeNum(x);
}

if_ (^{x < 3}, ^{
     handleSmallNum(x);
}, ^{
     handleLargeNum(x);
});

if_ ({ || x < 3}, { ||
     handleSmallNum(x);
}, { ||
     handleLargeNum(x);
});



while (x != null) {
    x.process();
    x = x.next();
}

while_ (^{x != null}, ^{
    x.process();
    x = x.next();
});

while_ ({ || x != null}, { ||
    x.process();
    x = x.next();
});


for (var i = 1; i < 10; i++) {
     total += vec[i];
}

for_ (^{var i = 1}, ^{i < 10}, ^{i++}, ^{
     total += vec[i];
});

for_ ({ || var i = 1}, { || i < 10}, { || i++}, { ||
     total += vec[i];
});


for (var prop in obj) {
     props.push(prop);
}

forIn_ (obj, ^(prop) {
     props.push(prop);
});

forIn_ (obj, { |prop|
     props.push(prop);
});


Here's examples with some built in JS functions that take function  
arguments:

arr.sort(function (a, b) { return a*a < b*b; });
arr.sort(^(a, b) { a*a < b*b });
arr.sort({ |a, b| a*a < b*b });

arr.map(function (x) { return x * (x - 1); });
arr.map(^(x) { x * (x-1)});
arr.map({ |x| x * (x-1) });

function doubleBs(str) {
     return str.replace(/b*/, function (substr) { return substr +  
substr; });
}

function doubleBs(str) {
     str.replace(/b*/, ^(substr) { substr + substr });
}

function doubleBs(str) {
     str.replace(/b*/, { |substr| substr + substr });
}


I like the ^ solution better because:

1) It makes parameter lists look like function parameter lists and  
call argument lists.
2) It separates the parameter from the body more clearly, which seems  
more readable, especially when there lambda is passed to something  
that also takes other arguments.
3) ^{ ... } seems like a less visually noisy way to express a 0- 
argument thunk than { || ... }


Regards,
Maciej


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20081203/a636148d/attachment.html>


More information about the Es-discuss mailing list