Expression closures - use-cases for shortcut lambda syntax (blocks)
Vassily Gavrilyak
gavrilyak at gmail.com
Sun Mar 18 09:07:46 PDT 2007
On 3/17/07, Brendan Eich <brendan at mozilla.org> wrote:
> On Mar 16, 2007, at 10:38 PM, Neil Mix wrote:
>
> > On Mar 16, 2007, at 8:57 PM, Robert Sayre wrote:
> >
> >> Seems to me that "function(){" is a little more painful when nested.
> >
> > Great examples -- you've changed my mind about the \( syntax. I
> > would argue that the painfulness is a result of densely packed
> > alternating/nested curlies and parens and has little to do with the
> > function keyword.
>
> Test that claim: show those Mochikit examples rewritten to use
> expression closures as proposed for ES4.
>
> /be
>
>
Here the examples rewritten uses functions and => syntax
// forEach used instead of for
addLoadEvent(() => {
var page = "rounded_corners/";
var elems = getElementsByTagAndClassName("A", "view-source");
elems.forEach(elem => {
var href = elem.href.split(/\//).pop();
elem.target = "_blank";
elem.href = "../view-source/view-source.html#" + page + href;
})
});
//usage of 2 different for's isn't very nice here
//there should be some way to do it with comrehensions
//, but forEach will do it too
addLoadEvent(() => {
DomDeco.registry.forEach((registry,key) => {
registry.forEach(value => DomDeco.apply(key, it))
})
}
//here add*Callback API is not neccessary, CPS will do it nicely too.
//bonus - no framework knowledge needed, just a convention
addLoadEvent(() => {
wait(0.5, {data: [["getUsers", "getUsers"], ["foo", "bar"]]},
res=>showSelectData(res), err=>showError(res));
})
//CPS again, example is actually not complete,
//showPageList is already refactored out, that is not always so in real code
addLoadEvent(()=>{
pagelist.onclick = e => {
e.preventDefault();
loadJSONDoc("${std.url('/pagelist', tg_format='json')}",
list=>showPageList(list));
}
})
//here the whole slot-signal framework (untyped)
//can be replaced with redable and typed version
addLoadEvent(()=>{
bad_example.onclick = _:Event => bad_example.showvalue("explicit_value");
bad_example.showvalue = s:String => window.alert(s);
good_example.onclick = _:Event => randomObj.showvalue("explicit_value");
randomObj.showvalue = s:String => window.alert(s);
});
Regard
Vassily Gavrilyak
More information about the Es4-discuss
mailing list