Expression closures - use-cases for shortcut lambda syntax (blocks)

classic Classic list List threaded Threaded
101 messages Options
1234 ... 6
Reply | Threaded
Open this post in threaded view
|

Expression closures - use-cases for shortcut lambda syntax (blocks)

Vassily Gavrilyak
I like the expression closures were accepted into the spec, they will
nicely cover many use-cases for iterating, sorting, etc. using closures.
And I was the one who suggested Brendan on his blog to bring the issue of shortcut function syntax.
Actually I proposed a more simpler solution, just using lambda-like syntax from Haskell.

However I didn't provide more use-cases where it can be useful and allows to not add support for
such use-cases with some special language construct.

In short - look how Ruby uses blocks.

So, just a few from the top

1. Automatic resource deallocation.
using(File.open("test"), function(file){
   //do something with file here..
}); //auto-close of file here.

2.Transactions (mixed with resource usage) for frameworks like Phobos (Phobos on Rails? :-) )
connection.transaction( function( tx){
    using(connection.prepare("INSERT INTO ..."), function( statement){
        using(statement, function(recordset){
             processRecordset(recordset);
        }) // recordset closed
    }); //statement closed
})//commit-rollback depended on whether exception was thrown

3. CPS-style asynchronous processing
3.1 - AJAX
server.getEMails(function(emails){
   eMails.forEach(function(email){
      if(somethingInEmail(email)){
        server.forwardEMail(function(response){
              processForwarding(responce);
        })
      }
   })
})
3.2 animations (Flash or DHTM)
Something like - move ball, when it hits the wall, explode can be clearly expressed with cps-style too.

4. Some simplest idioms like 10.times do {} from Ruby.
(10).times(function(){
   print("Hi");
})

Now, all those "function" just doesn't look like functions, bat rather blocks.
So word function is meaningless here and unnecessary complicates the code.


Maybe the best solution here is too introduce some special syntax for resource allocation and async programming.
That what some languages do and ES is still not here. But I doubt that transaction support will ever be in language.
And there probably many more usages for blocks.

In our team we currently using modified SpiderMonkey for JS and  MTASC for  AS  with  modified
lexer (not parser) for support shortcut syntax. We use this syntax quite heavily and found it  very useful and not a false economy.
Modification is VERY simple (2 lines in both cases) - when lexer found "(\" replace it with "function("

This way ES could almost for free have a feature that many languages (C# 3, Ruby, Smalltalk, Perl) already have
and developers found this feature useful. Java seems going there too with closures proposal.

And yes, this is just syntax sugar and nothing new for ES that aren't already present.
But developers will fail to recognize that ES actually have blocks just because of syntax.
I am the example of such developer :-).

Hope it's probably not to late to address those issues.

Regards,
Vassily Gavrilyak


Reply | Threaded
Open this post in threaded view
|

Re: Expression closures - use-cases for shortcut lambda syntax (blocks)

Brendan Eich-2
On Mar 15, 2007, at 5:37 PM, Vassily Gavrilyak wrote:

> In our team we currently using modified SpiderMonkey for JS and  
> MTASC for  AS  with  modified
> lexer (not parser) for support shortcut syntax. We use this syntax  
> quite heavily and found it  very useful and not a false economy.
> Modification is VERY simple (2 lines in both cases) - when lexer  
> found "(\" replace it with "function("

Don't you mean \( instead of (\ above?

I am warming up to this use of backslash. What do others think?

> This way ES could almost for free have a feature that many  
> languages (C# 3, Ruby, Smalltalk, Perl) already have
> and developers found this feature useful. Java seems going there  
> too with closures proposal.
>
> And yes, this is just syntax sugar and nothing new for ES that  
> aren't already present.
> But developers will fail to recognize that ES actually have blocks  
> just because of syntax.
> I am the example of such developer :-).
>
> Hope it's probably not to late to address those issues.

Syntax matters, it's the user interface. It is not too late, but it  
is late. We should discuss pros and cons here in a thread.

/be

_______________________________________________
Es4-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es4-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Expression closures - use-cases for shortcut lambda syntax (blocks)

Jon Zeppieri
On 3/15/07, Brendan Eich <[hidden email]> wrote:
> I am warming up to this use of backslash. What do others think?

+1

> Syntax matters, it's the user interface. It is not too late, but it
> is late. We should discuss pros and cons here in a thread.

PLT Scheme allows λ for 'lambda'.  JS could allow λ for 'function' and
backslash as a poor-man's λ.  I know it's lame to add two synonyms for
function, but it's also lame to use backslash explicitly as a
lambda-lookalike in a language that already accepts lambdas in
identifier syntax.

_______________________________________________
Es4-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es4-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Expression closures - use-cases for shortcut lambda syntax (blocks)

Brendan Eich-2
On Mar 15, 2007, at 6:35 PM, Jon Zeppieri wrote:

> On 3/15/07, Brendan Eich <[hidden email]> wrote:
>> I am warming up to this use of backslash. What do others think?
>
> +1
>
>> Syntax matters, it's the user interface. It is not too late, but it
>> is late. We should discuss pros and cons here in a thread.
>
> PLT Scheme allows λ for 'lambda'.  JS could allow λ for 'function'  
> and
> backslash as a poor-man's λ.  I know it's lame to add two synonyms  
> for
> function, but it's also lame to use backslash explicitly as a
> lambda-lookalike in a language that already accepts lambdas in
> identifier syntax.

My Unicode implant is on back-order -- what code point is that? Any  
clue on how to type it on my Macbook Pro? Thanks,

/be


_______________________________________________
Es4-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es4-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Expression closures - use-cases for shortcut lambda syntax (blocks)

Peter Hall-4
In reply to this post by Jon Zeppieri
I think, once I've figured out how to actually type "λ" on my UK
keyboard (I actually don't know how to enter 0x03BB in my keyboard, I
had to paste from charmap), I might as well have typed "function(".

Also, "λ" is a perfectly permissable character for an identifier.
Wouldn't it cause problems to  give it special meaning? (for Greeks
especially)

Peter

On 3/16/07, Jon Zeppieri <[hidden email]> wrote:

> On 3/15/07, Brendan Eich <[hidden email]> wrote:
> > I am warming up to this use of backslash. What do others think?
>
> +1
>
> > Syntax matters, it's the user interface. It is not too late, but it
> > is late. We should discuss pros and cons here in a thread.
>
> PLT Scheme allows λ for 'lambda'.  JS could allow λ for 'function' and
> backslash as a poor-man's λ.  I know it's lame to add two synonyms for
> function, but it's also lame to use backslash explicitly as a
> lambda-lookalike in a language that already accepts lambdas in
> identifier syntax.
>
> _______________________________________________
> Es4-discuss mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/es4-discuss
>

_______________________________________________
Es4-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es4-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Expression closures - use-cases for shortcut lambda syntax (blocks)

Jon Zeppieri
In reply to this post by Brendan Eich-2
On 3/15/07, Brendan Eich <[hidden email]> wrote:
> My Unicode implant is on back-order -- what code point is that? Any
> clue on how to type it on my Macbook Pro? Thanks,

Code point: 0x03bb

There are at least three ways to enter the character on the Mac.

1) Select it from the character palette, which can be raised in
certain apps, like TextEdit and Safari, by pressing option-command-T.
Or it can appear as an option in the International menu (the one with
the flags) if you enable it in the International System Preferences.

2) In the International System Preferences, under the Input Menu tab, you can:

  a) select the Unicode Hex Input method, which, when active, allows
you to type code points in hex while holding down the option key, or

  b) select an input method that normally uses lambda, like a Greek keyboard.


I used the polytonic Greek keyboard.  Lambda is mapped to 'l'.

-Jon
_______________________________________________
Es4-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es4-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Expression closures - use-cases for shortcut lambda syntax (blocks)

Jon Zeppieri
In reply to this post by Peter Hall-4
On 3/15/07, Peter Hall <[hidden email]> wrote:
> I think, once I've figured out how to actually type "λ" on my UK
> keyboard (I actually don't know how to enter 0x03BB in my keyboard, I
> had to paste from charmap), I might as well have typed "function(".

Right -- which is why I also like backslash.

Good programming editors, of course, allow you to bind characters to
whatever keys you want.  In DrScheme, on the Mac, λ is mapped to
command-\ by default.  Mail clients tend not to be good programming
editors...

>
> Also, "λ" is a perfectly permissable character for an identifier.
> Wouldn't it cause problems to  give it special meaning? (for Greeks
> especially)

Good point.  It could break some code.

-Jon


>
> Peter
>
> On 3/16/07, Jon Zeppieri <[hidden email]> wrote:
> > On 3/15/07, Brendan Eich <[hidden email]> wrote:
> > > I am warming up to this use of backslash. What do others think?
> >
> > +1
> >
> > > Syntax matters, it's the user interface. It is not too late, but it
> > > is late. We should discuss pros and cons here in a thread.
> >
> > PLT Scheme allows λ for 'lambda'.  JS could allow λ for 'function' and
> > backslash as a poor-man's λ.  I know it's lame to add two synonyms for
> > function, but it's also lame to use backslash explicitly as a
> > lambda-lookalike in a language that already accepts lambdas in
> > identifier syntax.
> >
> > _______________________________________________
> > Es4-discuss mailing list
> > [hidden email]
> > https://mail.mozilla.org/listinfo/es4-discuss
> >
>
> _______________________________________________
> Es4-discuss mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/es4-discuss
>
>
_______________________________________________
Es4-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es4-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Expression closures - use-cases for shortcut lambda syntax (blocks)

Peter Hall-4
In reply to this post by Jon Zeppieri
> > I am warming up to this use of backslash. What do others think?
>
> +1


It seems to me that ES4 is rapidly using up every combination of
easily typable non-identifier characters. Apart from reducing the
choice of possible syntax for ES in the future (look at the type
parameters discussion), I think a lot of developers who are
comfortable with existing JS and AS versions will have a number of wtf
moments when they see this stuff used for the first time. Does it
really offer enough reward?

Peter
_______________________________________________
Es4-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es4-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Expression closures - use-cases for shortcut lambda syntax (blocks)

John Cowan
In reply to this post by Peter Hall-4
Peter Hall scripsit:
> I think, once I've figured out how to actually type "?" on my UK
> keyboard (I actually don't know how to enter 0x03BB in my keyboard, I
> had to paste from charmap), I might as well have typed "function(".

Hence the desire to allow \ as a synonym.

> Also, "?" is a perfectly permissable character for an identifier.
> Wouldn't it cause problems to  give it special meaning? (for Greeks
> especially)

Not really, no more than it causes problems to give "if" special
meaning.

--
John Cowan   http://ccil.org/~cowan  [hidden email]
[P]olice in many lands are now complaining that local arrestees are insisting
on having their Miranda rights read to them, just like perps in American TV
cop shows.  When it's explained to them that they are in a different country,
where those rights do not exist, they become outraged.  --Neal Stephenson
_______________________________________________
Es4-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es4-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Expression closures - use-cases for shortcut lambda syntax (blocks)

Dave Herman-2
In reply to this post by Brendan Eich-2
Also, DrScheme has a keybinding that allows you to type Ctrl-\ and it
inserts a lambda, which might make a nice tie-in between the two
different syntaxes.

Dave

Brendan Eich wrote:

> On Mar 15, 2007, at 6:35 PM, Jon Zeppieri wrote:
>
>> On 3/15/07, Brendan Eich <[hidden email]> wrote:
>>> I am warming up to this use of backslash. What do others think?
>>
>> +1
>>
>>> Syntax matters, it's the user interface. It is not too late, but it
>>> is late. We should discuss pros and cons here in a thread.
>>
>> PLT Scheme allows λ for 'lambda'.  JS could allow λ for 'function' and
>> backslash as a poor-man's λ.  I know it's lame to add two synonyms for
>> function, but it's also lame to use backslash explicitly as a
>> lambda-lookalike in a language that already accepts lambdas in
>> identifier syntax.
>
> My Unicode implant is on back-order -- what code point is that? Any clue
> on how to type it on my Macbook Pro? Thanks,
>
> /be
>
>
> _______________________________________________
> Es4-discuss mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/es4-discuss

_______________________________________________
Es4-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es4-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Expression closures - use-cases for shortcut lambda syntax (blocks)

Neil Mix
In reply to this post by Brendan Eich-2

> I am warming up to this use of backslash. What do others think?

I like it.

Although (prepare for gratuitous land grab), I have to say one of my  
top coding typos with lambda-as-argument is failing to remember the  
closing paren.  It would be *really* nice if by syntactic sugar a  
block following a function call was converted into a lambda parameter  
at the end of the arguments list, so that:

        ajax(url, \(response) {
                // do something
        });

could become:

        ajax(url) {
                var [response] = arguments;
                // do something
        }

Even though my version has more characters due to the arguments  
destructuring, I'd gladly trade that off for the enhanced readability.

*ducks*


_______________________________________________
Es4-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es4-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Expression closures - use-cases for shortcut lambda syntax (blocks)

Brendan Eich-2
In reply to this post by Peter Hall-4
On Mar 15, 2007, at 6:55 PM, Peter Hall wrote:

> I think, once I've figured out how to actually type "λ" on my UK
> keyboard (I actually don't know how to enter 0x03BB in my keyboard, I
> had to paste from charmap), I might as well have typed "function(".

Or at least "\" ;-).

> Also, "λ" is a perfectly permissable character for an identifier.
> Wouldn't it cause problems to  give it special meaning? (for Greeks
> especially)

Yes, you are right -- ES3 allows Unicode identifiers, so this would  
not be a backward compatible change. It seems excessive anyway, if \
(x)x is acceptable for (lambda (x) x).

/be

_______________________________________________
Es4-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es4-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Expression closures - use-cases for shortcut lambda syntax (blocks)

liorean
In reply to this post by Jon Zeppieri
On 16/03/07, Jon Zeppieri <[hidden email]> wrote:
> On 3/15/07, Brendan Eich <[hidden email]> wrote:
> > My Unicode implant is on back-order -- what code point is that? Any
> > clue on how to type it on my Macbook Pro? Thanks,
>
> Code point: 0x03bb
>
> There are at least three ways to enter the character on the Mac.

In addition to those, editors written specifically for editing
ECMAScript 4 will also be able supply an easy method on inserting it
via either text replacement (insert \( and it magics that into λ) or
via some keypress combination.

One problem here is that text documents default to non-unicode code
pages on Windows (no idea about other OSes), and servers don't usually
serve javascript as utf-8 or utf-16.
--
David "liorean" Andersson
_______________________________________________
Es4-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es4-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Expression closures - use-cases for shortcut lambda syntax (blocks)

Brendan Eich-2
In reply to this post by John Cowan
On Mar 15, 2007, at 7:38 PM, John Cowan wrote:

>> Also, "?" is a perfectly permissable character for an identifier.
>> Wouldn't it cause problems to  give it special meaning? (for Greeks
>> especially)
>
> Not really, no more than it causes problems to give "if" special
> meaning.

js> o = {if: 42}
[object Object]
js> o.if
42
js> function if(x){return 1+x}
js> if(42)
/* oops */;
js> function::if(42)
43

The scenario to consider is: existing ES3 content uses the lambda  
unichar as a single-character identifier of a function. Then λ(x) +x  
looks like a call to the λ function, then an evaluation of a variable  
x, then an add operator evaluation. If λ is short for function, then  
the example sentence is ambiguous.

/be
_______________________________________________
Es4-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es4-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Expression closures - use-cases for shortcut lambda syntax (blocks)

Daniel C. Wang-2
In reply to this post by Neil Mix
why not
{(x,y) \  x + y }
then it would be
ajax(url, { (response)  \
                   // do something });

... you can then consider introducing

{ (x,y) |   x % 2 == y}

which just constrains the body to be a predicated!




_______________________________________________
Es4-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es4-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Expression closures - use-cases for shortcut lambda syntax (blocks)

Brendan Eich-2
Ok, this is getting out of hand ;-).

Seriously, we are not going to inject any alien syntax. Vassily  
proposed a shorthand in expression closures: \ for function. Let's  
try to get back to that.

On Mar 16, 2007, at 1:49 AM, Daniel C. Wang wrote:

> why not
> {(x,y) \  x + y }
> then it would be
> ajax(url, { (response)  \
>                    // do something });
>
> ... you can then consider introducing
>
> { (x,y) |   x % 2 == y}


That's ambiguous: , | % and == are all dyadic operators.

> which just constrains the body to be a predicated!

This isn't a comprehension. Not sure why the syntax constrains the  
expression to be boolean.

/be

_______________________________________________
Es4-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es4-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Expression closures - use-cases for shortcut lambda syntax (blocks)

P T Withington
On 2007-03-16, at 04:58 EDT, Brendan Eich wrote:

> Seriously, we are not going to inject any alien syntax. Vassily
> proposed a shorthand in expression closures: \ for function. Let's
> try to get back to that.

I don't see the value, except for the 'obfuscated Javascript  
contest'.  If you make that many closures in a day, why not bind c-\  
to insert `function` in your editor?  I would rather programs were  
easy to read than easy to write, since I spend a lot more time doing  
the former.  [Another pet peeve:  I wish `!` were spelled `not`.  It  
is too easy to overlook.]
_______________________________________________
Es4-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es4-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Expression closures - use-cases for shortcut lambda syntax (blocks)

Igor Bukanov-2
In reply to this post by Brendan Eich-2
On 16/03/07, Brendan Eich <[hidden email]> wrote:
> Yes, you are right -- ES3 allows Unicode identifiers, so this would
> not be a backward compatible change. It seems excessive anyway, if \
> (x)x is acceptable for (lambda (x) x).

And if farther
  \(args) expression
would stand for
  function(args) { return expression; }

then JS would be close to beating ML and friends in shortness of
defining lambdas.

Regards, Igor
_______________________________________________
Es4-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es4-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Expression closures - use-cases for shortcut lambda syntax (blocks)

Igor Bukanov-2
In reply to this post by P T Withington
On 16/03/07, P T Withington <[hidden email]> wrote:

> On 2007-03-16, at 04:58 EDT, Brendan Eich wrote:
>
> > Seriously, we are not going to inject any alien syntax. Vassily
> > proposed a shorthand in expression closures: \ for function. Let's
> > try to get back to that.
>
> I don't see the value, except for the 'obfuscated Javascript
> contest'.  If you make that many closures in a day, why not bind c-\
> to insert `function` in your editor?  I would rather programs were
> easy to read than easy to write, since I spend a lot more time doing
> the former.

For me function(args) { .... } is just too verbose and makes code
harder to read especially in the case of many short lambdas.

Regards, Igor
_______________________________________________
Es4-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es4-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Expression closures - use-cases for shortcut lambda syntax (blocks)

Lars T Hansen-2
In reply to this post by Igor Bukanov-2
On 3/16/07, Igor Bukanov <[hidden email]> wrote:

> On 16/03/07, Brendan Eich <[hidden email]> wrote:
> > Yes, you are right -- ES3 allows Unicode identifiers, so this would
> > not be a backward compatible change. It seems excessive anyway, if \
> > (x)x is acceptable for (lambda (x) x).
>
> And if farther
>   \(args) expression
> would stand for
>   function(args) { return expression; }
>
> then JS would be close to beating ML and friends in shortness of
> defining lambdas.

Shortness isn't a reasonable metric by itself.  (Beating ML and
Haskell even less so.)  The language has to balance compactness and
clarity.  And though ES has aspects of a functional language (and ES4
will be even more so than ES3), it is more fundamentally imperative.
No doubt many uses will be found for the expression function syntax,
but going to this level of terseness does not seem to be justified.
As Tucker said, programs are read much more often than they're
written.  "function" provides all readers with a reasonable idea about
what follows.  "\" is overly terse, and makes sense only if you think
"lambda" would make sense in the same context (I don't, particularly
-- it's bad UI, to use Brendan's terminology).

--lars
_______________________________________________
Es4-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es4-discuss
1234 ... 6