Comments for On JS "Lambdas"
I posted something not dissimilar to the ES lists some time ago, but it's not clear to a native JS parser what the {|
production should mean. As we were talking through it, Erik pointed out to me that the { ... }
syntax (maddeningly) seems to be taken by a anonymous block syntax that many JS interpreters already support but which doesn't actually DO anything. Grr.
As a result, both of those syntaxes would involve lots of look-ahead, which some implementers are (apparently) loathe to contemplate.
Regards
All languages have compilers to optimize their performance. Javascript has minifiers. I can have a large amount of human-readable source code while still sending a minimal amount of code to the browser. Please don't make things more complicated than they should be.
They address size-on-the wire. They don't address human factors.
Trav:
I wrote one of those minifiers. You don't need to lecture me on the topic of what is and isn't effective in terms of reducing byte count ;-)
As for readability, it's a human-factors argument, and I'm arguing that seeing the word "function" all over the place really isn't as clear as you think it is. Java tends to be difficult to comprehend for similar reasons...stuff gets munged into structures that don't fit the intent as a side-effect of insufficient semantics married to bad syntax. JS is better on the functional side of things (the semantics are fine), but the sytnax is lousy. I'll absolutely agree that a non-lame class composition system (traits, anyone?) would go a very long way to addressing some of the bigger issues in the language, but if the WG is going to chew of lambdas (and it looks like they are), we should propose something that doesn't suck.
We can absolutely make both sides of the language easier to use at the same time. There's no either/or here.
Regards
sed -E -e 's/\sfunction\s/#/g' -e 's/\sreturn\s//'
...and got a savings of 4,220 bytes. So either the library grew an awful lot, or you were using a version that had been minified with something like packer using base62 encoding (which would have already removed nearly all uses of "function" and "return" at the cost of eval time). If the latter case is true, I don't think that's a very fair test, since the packed version is effectively doing (to a much greater extent!) what Alex is suggesting already.
This isn't to say that I think the size over the wire is really the most important consideration here (gzipping both versions reduces the difference to 339 bytes). The function(){} form in JS is as annoying as the array() literal in PHP.
The minified version I used: http://code.google.com/p/jqueryjs/downloads/detail?name=jquery-1.3.2.min.js
Packer: http://dean.edwards.name/packer/
With that said, isnt there a javascript implementation of Ruby? HotRuby or something.
In the spirit of cathartic shortenings, may I suggest fu(ags){ ... }
?
;-)
Regards
In this case, "syntactic sugar" is really just "symbol replacement", and that doesn't seem to add much value on its own. Right now, "function" means "define a function"; I'm not sure how that could be any clearer.
Language expressiveness is a balance between good Huffman coding (common stuff should be shorter than longer stuff) and descriptivness. This syntax is not "descriptive" in the sense that it doesn't say "function". It is, however, superior for all but the most trivial users of the language. At some point the word "function" actually begins to obscure the meaning of "I'm trying to create a closure as an argument", which happens quite often.
I understand the argument that says you might not need it, but improvements in turing-complete languages are rarely about "needs".
Regards
Just don't add it to the old functions for backward-compatibility.
Yes. Really.
That is all.
if the semantics were to change as far as return values then you could use fn or function depending on your want.
it's also a good way to abbreviate some of those dirty "effin" curse words you use alex! perhaps some of your frustration could be released through your thoughts when everytime you needed a lambda, you would think "effin" and then you might be able to maintain a PG rating on your blog :P
I don't think that using the word "function" when declaring a function literal is a problem at all. It's not quite as concise as the syntax for other literals like objects, strings, numbers and regular expressions, but functions are a little more complicated, having arguments, a body and (sadly) optionally a name.
Perhaps it's just time to get an editor with macros :)
Personally I want lambdas to achieve one goal: less typing. It means removing/bypassing both "function" and "return", if possible. At least in simple cases.
On ES4 mailing list several ideas were floated and one of them was to use a backslash:
(a, b)
or something like that.
But anything that makes using lambdas easier would get my vote (I am not on the committee, so it is not counted officially).
my $lambda = sub
And Perl's @_ array works just like Javascript's arguments array.
Perl 6 goes further and has "blocks" and "pointy blocks":
my $lambda = { ... } my $lambda = -> $evt
http://msdn.microsoft.com/en-us/library/bb397687.aspx
There's another potential advantage to working this way; it avoids tight coupling of functions to their uses. If we ever get environments were reloading running code is possible and makes sense, have a layer of indirection between the functions will be nice. Otherwise all those folks with direct references to functions everywhere will find that those functions didn't get updated on the reload. At least, until someone gives us #become: (not holding my breath on that one).
Note that not all Smalltalk blocks were done this way, most weren't, but some were; my memory is that the UI toolkit I used most used them. (CommonWidgets - the Smalltalk version of today's SWT in Java, used them quite a bit)
http://osteele.com/sources/javascript/functional/
Implicit parameters, implicit returns, and a lots of handy functions taking advantage of short lambdas.
It doesn't do any DOM-y stuff by itself, but I've been using it along with Prototype for some pretty happy code.