<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Infrequently Noted &#187; lambda</title>
	<atom:link href="http://infrequently.org/tag/lambda/feed/" rel="self" type="application/rss+xml" />
	<link>http://infrequently.org</link>
	<description></description>
	<lastBuildDate>Tue, 01 May 2012 11:30:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>JS 1.8 Function Expressions: The Opposite of &#8220;Good&#8221;</title>
		<link>http://infrequently.org/2009/08/js-1-8-function-expressions-the-opposite-of-good/</link>
		<comments>http://infrequently.org/2009/08/js-1-8-function-expressions-the-opposite-of-good/#comments</comments>
		<pubDate>Thu, 06 Aug 2009 21:02:51 +0000</pubDate>
		<dc:creator>alex</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[1.8]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[lambda]]></category>
		<category><![CDATA[mozilla]]></category>

		<guid isPermaLink="false">http://alex.dojotoolkit.org/?p=1053</guid>
		<description><![CDATA[JavaScript is crying out for a way to write functions more tersely. I&#8217;ve added my suggestion to the debate, and was vaguely aware that Mozilla had implemented &#8220;function expressions&#8221;. It wasn&#8217;t until I saw their use in the (excellent) ProtoVis examples that I realized how stomach-churningly bad the syntax for them is. Instead of dropping [...]]]></description>
			<content:encoded><![CDATA[<p>JavaScript is crying out for a way to write functions more tersely. I&#8217;ve <a href="http://alex.dojotoolkit.org/2009/05/on-js-lambdas/">added my suggestion to the debate</a>, and was vaguely aware that Mozilla had implemented <a href="https://developer.mozilla.org/en/New_in_JavaScript_1.8">&#8220;function expressions&#8221;</a>. It wasn&#8217;t until I saw their use in the (excellent) <a href="http://vis.stanford.edu/protovis/ex/">ProtoVis examples</a> that I realized how stomach-churningly bad the syntax for them is. Instead of dropping the word <code>function</code> from the declaration of lambdas, they kill the <code>{ }</code> charachters, leaving the big visual turd at the front of the lambda while simultaneously omitting the symmetrical visual aid that allows programmers to more easily spot missing terminators.</p>
<p>Oof.</p>
<p>It&#8217;d be one thing of the feature saved enough effort (typing) to justify the confusion, but it doesn&#8217;t. Bitter syntactic sugar in a language that&#8217;s already complicated by whitespace issues should be avoided.</p>
]]></content:encoded>
			<wfw:commentRss>http://infrequently.org/2009/08/js-1-8-function-expressions-the-opposite-of-good/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>On JS &#8220;Lambdas&#8221;</title>
		<link>http://infrequently.org/2009/05/on-js-lambdas/</link>
		<comments>http://infrequently.org/2009/05/on-js-lambdas/#comments</comments>
		<pubDate>Wed, 20 May 2009 17:01:42 +0000</pubDate>
		<dc:creator>alex</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[lambda]]></category>

		<guid isPermaLink="false">http://alex.dojotoolkit.org/?p=976</guid>
		<description><![CDATA[The ES working group is hard at work on &#8220;Harmony&#8221;, the goals of which are significantly more sane than previous attempts to build a new language from JavaScript. Namely, they&#8217;re being careful to be able to express things in new syntax based on old syntax. This is referred to as &#8220;de-sugaring&#8221;. Many new bits of [...]]]></description>
			<content:encoded><![CDATA[<p>The ES working group is hard at work on &#8220;Harmony&#8221;, the goals of which are significantly more sane than previous attempts to build a new language from JavaScript. Namely, they&#8217;re being careful to be able to express things in new syntax based on old syntax. This is referred to as &#8220;de-sugaring&#8221;. Many new bits of syntax will be expressed in old syntax in a resulting spec, and so it will be with any new lambda syntax.</p>
<p>One of the things I find most persistently annoying about the language as we have it is the verbose and wordy way of saying &#8220;build me a new invokable thing&#8221;. Since JS builds dynamic scopes based on on functions, this is particularly annoying when writing event handlers, callbacks, and arguments to <code>forEach</code> and friends. Needless to say, these are the things we do <em>all the time</em> in JavaScript. If there was anything that deserved syntactic sugar, this is it.</p>
<p>This brings us to &#8220;lambdas&#8221;. Languages like Ruby have a great syntax for expressing &#8220;a thing you can invoke&#8221; in a terse way. Python has something along these lines based on the keyword <code>lambda</code>, but Guido insists on keeping them <a href="http://docs.python.org/tutorial/controlflow.html#lambda-forms">neutered</a> based on (AFAICT from in-person discussions) a dislike of functional programming. Java is <a href="http://www.javac.info/">dangerously close to getting useful closures</a> but it&#8217;s so syntactically handicapped that it&#8217;ll probably be <em>another</em> decade before the masses rebel and demand a terse way to say what they mean. Or maybe they&#8217;ll all have just defected to <a href="http://jruby.codehaus.org/">JRuby</a>. For most of these languages, being able to say &#8220;here&#8217;s a new invokable thing&#8221; tersely is a Nice To Have feature. No real-world program will be slowed down by the size of a keyword. Indeed, Java makes you do so much work for the compiler that it&#8217;s practically taken as a badge of honor that you have to type it all out over and over and over.</p>
<p>JavaScript is different. Unlike nearly every other language, the terseness of JavaScript code is a key determinant in the performance of a web application. The bigger your scripts, the slower your app loads. It&#8217;s as simple as that.</p>
<p>It&#8217;s a crime, then, that we still have to type:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">node.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;click&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>evt<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #009966; font-style: italic;">/* ... */</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// or</span>
&nbsp;
<span style="color: #009900;">&#91;</span><span style="color: #006600; font-style: italic;">/* ... */</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">forEach</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #009966; font-style: italic;">/* ... */</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The syntax for &#8220;an anonymous invokable thing&#8221; should be a lot shorter given how much exercise it gets in the real world. What real-world script authors don&#8217;t need is something other than &#8220;a shorter way to say <code>function</code>&#8220;. Anything different than this isn&#8217;t strictly useful. Anything that requires you to use a more wordy syntax to name arguments is a failure, and anything that uses a name longer than the shortest possible thing just re-introduces that bug that needs fixing in the first place. There have been proposals at the ES working group to date regarding &#8220;lambdas&#8221; and most assume that they will be something that serves to make implementer&#8217;s roles easier when it comes to de-sugaring but fail in one of these ways. Folks who write actual scripts should start speaking up and telling the Harmony working group what needs to be clearly and un-ambiguiously said:</p>
<h4>Don&#8217;t fuck with the semantics of <code>function</code>, just let me stop typing it so often</h4>
<p>What might a better syntax for <code>function</code> look like?</p>
<p>The best that <a href="http://erik.eae.net/">Erik</a> and I could come up with that is relatively unambiguious for parsers, doesn&#8217;t introduce hard-to-type characters on certain keyboard layouts, and doesn&#8217;t mess with the semantics of <code>function(){}</code> is this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="" style="font-family:monospace;">// empty function body, zero arguments
var lambda = #<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span> /*... */ <span class="br0">&#125;</span>;
&nbsp;
// empty function body, arguments list elided away
var lambda = #<span class="br0">&#123;</span> /* ... */ <span class="br0">&#125;</span>;
&nbsp;
// we can re-write the previous examples as:
node.addEventListener<span class="br0">&#40;</span>&quot;click&quot;, #<span class="br0">&#40;</span>evt<span class="br0">&#41;</span><span class="br0">&#123;</span> /* ... */ <span class="br0">&#125;</span>, false<span class="br0">&#41;</span>;
&nbsp;
// and
&nbsp;
<span class="br0">&#91;</span>/* ... */<span class="br0">&#93;</span>.forEach<span class="br0">&#40;</span>#<span class="br0">&#40;</span>i<span class="br0">&#41;</span><span class="br0">&#123;</span> /* ... */ <span class="br0">&#125;</span><span class="br0">&#41;</span>;</pre></td></tr></table></div>

<p>The long history of the word &#8220;lambda&#8221; coupled with the different interpretations that various languages place on them make using the word &#8220;lambda&#8221; to say &#8220;a short name for something you can invoke&#8221; particularly loaded. Perhaps instead we should just call this new syntax that crams a lot of meaning into a short &#8220;keyword&#8221; something else entirely.</p>
<p>&#8220;Cramdas&#8221;, anyone?</p>
<p><b>Update:</b> &#8230;and James Padolsey <a href="http://james.padolsey.com/javascript/custom-javascript-with-parsescripts/">makes it so!</a>. </p>
]]></content:encoded>
			<wfw:commentRss>http://infrequently.org/2009/05/on-js-lambdas/feed/</wfw:commentRss>
		<slash:comments>28</slash:comments>
		</item>
	</channel>
</rss>

