simple shorter function syntax

Dmitry A. Soshnikov dmitry.soshnikov at gmail.com
Mon Jul 26 01:15:21 PDT 2010


On 26.07.2010 12:06, Cameron McCormack wrote:
> Dmitry A. Soshnikov:
>    
>> In JS |x| can be placed outside, and then some special name won't be
>> needed at all:
>>
>> [1, 2, 3].map((x) {x * x}).
>>      
> Wouldn’t this have the same problem that Oliver mentioned?  Only in this
> case the ambiguity would be whether the “(x)” is a parenthesized
> expression or the introduction of the anonymous function.
>
> Sam Ruby:
>    
>> [0,1,2,3].map {|x| x*x}
>>
>> (try it in 'irb' to see what I mean)
>>
>> While I don't believe that would fly here, perhaps adding parens
>> around the function would:
>>
>> [0,1,2,3].map({|x| x*x})
>>      
> I think having the braces on the outside would help with the
> aforementioned problem.  The vertical bars don’t particularly grab me
> (not being a Rubyist), but this does:
>
>    [0, 1, 2, 3].map({ x =>  x * x }); // optionally { (x) =>  x * x }?
>
>    function mapPairs(o, f) {
>      for (var p in o) {
>        f(p, o[p]);
>      }
>    }
>
>    mapPairs(someObject, { (a, b) =>  a + '=' + b) });
>
>    

That's look interesting, yeah, I (personally) like it. Also beside => 
can be used "traditional" function definition sign of some functional 
(and not only functional) languages: ->

Coffee (while using -> for functions) uses => sugar for binding the 
`this` value with defining a function at the same time: 
http://jashkenas.github.com/coffee-script/#fat_arrow which I think also 
a good sugar for `.bind` method.

> (Sorry for adding more options to the discussion — quick rebuttals as to
> why the above wouldn’t work welcome.)
>
>    



More information about the es-discuss mailing list