<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Anonymous Recursive Functions</title>
	<atom:link href="http://blog.thejit.org/2008/12/27/anonymous-recursive-functions/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.thejit.org/2008/12/27/anonymous-recursive-functions/</link>
	<description>Data Visualization, JavaScript and Computer Science related stuff</description>
	<lastBuildDate>Mon, 15 Feb 2010 17:30:00 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Nicolas</title>
		<link>http://blog.thejit.org/2008/12/27/anonymous-recursive-functions/comment-page-1/#comment-309</link>
		<dc:creator>Nicolas</dc:creator>
		<pubDate>Tue, 30 Dec 2008 10:49:52 +0000</pubDate>
		<guid isPermaLink="false">http://blog.thejit.org/?p=204#comment-309</guid>
		<description>@Kyle, Mike: I agree with you. Actually, my response to salty-horse talks about namespacing and how to define a named function without messing around with namespacing (just by creating an &quot;outside&quot; closure). I haven&#039;t tested that code, but it should work.

Your code is nicer though :) 
As you pointed out, just by putting an outside closure there one could protect it from namespacing:

&lt;pre name=&quot;code&quot; class=&quot;js:nogutter:nocontrols&quot;&gt;
(function() {
  (function trav(node, val) {
      if(node == null) return trav;
      node.selected = val;
      return trav(node.parent, val);
  })(nodePrev, false)(nodeNew, true); 
})();
&lt;/pre&gt; 

However, I&#039;m not sure about the performance of that code.</description>
		<content:encoded><![CDATA[<p>@Kyle, Mike: I agree with you. Actually, my response to salty-horse talks about namespacing and how to define a named function without messing around with namespacing (just by creating an &#8220;outside&#8221; closure). I haven&#8217;t tested that code, but it should work.</p>
<p>Your code is nicer though <img src='http://blog.thejit.org/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
As you pointed out, just by putting an outside closure there one could protect it from namespacing:</p>
<pre name="code" class="js:nogutter:nocontrols">
(function() {
  (function trav(node, val) {
      if(node == null) return trav;
      node.selected = val;
      return trav(node.parent, val);
  })(nodePrev, false)(nodeNew, true);
})();
</pre>
<p>However, I&#8217;m not sure about the performance of that code.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kyle Simpson</title>
		<link>http://blog.thejit.org/2008/12/27/anonymous-recursive-functions/comment-page-1/#comment-308</link>
		<dc:creator>Kyle Simpson</dc:creator>
		<pubDate>Tue, 30 Dec 2008 01:17:04 +0000</pubDate>
		<guid isPermaLink="false">http://blog.thejit.org/?p=204#comment-308</guid>
		<description>@Nicolas-
I think Mike McNally&#039;s post was meant to suggest that you can do this, which is the syntax often called &quot;named functions&quot; or &quot;named anonymous functions&quot;:

&lt;pre name=&quot;code&quot; class=&quot;js:nogutter:nocontrols&quot;&gt;
(function trav(node, val) {
    if(node == null) return trav;
    node.selected = val;
    return trav(node.parent, val);
})(nodePrev, false)(nodeNew, true); 
&lt;/pre&gt;

You&#039;ll notice the use of the name &quot;trav&quot; right after the function keyword allows you to refer to it by name inside itself for the recursion (as well as the chainability &quot;return&quot;). I&#039;ve read that arguments.callee is less performant than a named function reference, which would especially be an important factor in regards to recursion. I&#039;ve never tested this myself, and honestly, I usually use &quot;arguments.callee&quot; in these cases, but I figured it was helpful to contrast it with a slight alternative.

The only downfall here is that with only the above, &quot;trav&quot; becomes a defined function reference outside the function block scope in question. You could of course wrap the above in an outer &quot;(function(){...})();&quot; scoping block, and get both better performance as well as namespace protection, preventing &quot;trav&quot; from leaking out into the outer variable scope.</description>
		<content:encoded><![CDATA[<p>@Nicolas-<br />
I think Mike McNally&#8217;s post was meant to suggest that you can do this, which is the syntax often called &#8220;named functions&#8221; or &#8220;named anonymous functions&#8221;:</p>
<pre name="code" class="js:nogutter:nocontrols">
(function trav(node, val) {
    if(node == null) return trav;
    node.selected = val;
    return trav(node.parent, val);
})(nodePrev, false)(nodeNew, true);
</pre>
<p>You&#8217;ll notice the use of the name &#8220;trav&#8221; right after the function keyword allows you to refer to it by name inside itself for the recursion (as well as the chainability &#8220;return&#8221;). I&#8217;ve read that arguments.callee is less performant than a named function reference, which would especially be an important factor in regards to recursion. I&#8217;ve never tested this myself, and honestly, I usually use &#8220;arguments.callee&#8221; in these cases, but I figured it was helpful to contrast it with a slight alternative.</p>
<p>The only downfall here is that with only the above, &#8220;trav&#8221; becomes a defined function reference outside the function block scope in question. You could of course wrap the above in an outer &#8220;(function(){&#8230;})();&#8221; scoping block, and get both better performance as well as namespace protection, preventing &#8220;trav&#8221; from leaking out into the outer variable scope.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nicolas</title>
		<link>http://blog.thejit.org/2008/12/27/anonymous-recursive-functions/comment-page-1/#comment-307</link>
		<dc:creator>Nicolas</dc:creator>
		<pubDate>Mon, 29 Dec 2008 23:10:51 +0000</pubDate>
		<guid isPermaLink="false">http://blog.thejit.org/?p=204#comment-307</guid>
		<description>Very interesting post Matt, thanks for pointing that :)</description>
		<content:encoded><![CDATA[<p>Very interesting post Matt, thanks for pointing that <img src='http://blog.thejit.org/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Matt Might</title>
		<link>http://blog.thejit.org/2008/12/27/anonymous-recursive-functions/comment-page-1/#comment-306</link>
		<dc:creator>Matt Might</dc:creator>
		<pubDate>Sat, 27 Dec 2008 17:51:26 +0000</pubDate>
		<guid isPermaLink="false">http://blog.thejit.org/?p=204#comment-306</guid>
		<description>If you take the Y combinator approach, then you can also memoize recursive functions:

http://matt.might.net/articles/implementation-of-recursive-fixed-point-y-combinator-in-javascript-for-memoization/</description>
		<content:encoded><![CDATA[<p>If you take the Y combinator approach, then you can also memoize recursive functions:</p>
<p><a href="http://matt.might.net/articles/implementation-of-recursive-fixed-point-y-combinator-in-javascript-for-memoization/" rel="nofollow">http://matt.might.net/articles/implementation-of-recursive-fixed-point-y-combinator-in-javascript-for-memoization/</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nicolas</title>
		<link>http://blog.thejit.org/2008/12/27/anonymous-recursive-functions/comment-page-1/#comment-305</link>
		<dc:creator>Nicolas</dc:creator>
		<pubDate>Sat, 27 Dec 2008 17:16:45 +0000</pubDate>
		<guid isPermaLink="false">http://blog.thejit.org/?p=204#comment-305</guid>
		<description>Namespacing. By using anonymous functions you are not polluting the namespace with function names. The equivalent way of doing this with named functions would be to use a closure like this:
&lt;pre name=&quot;code&quot; class=&quot;js:nogutter:nocontrols&quot;&gt;
  (function() {
    var selectPath = function(node, val) {  
      if(node == null) return;  
      node.selected = val;  
      return selectPath(node.parent, val);  
    };  
    selectPath(nodePrev, false);
    selectPath(nodeNew, true);
  })();
&lt;/pre&gt;

That way you&#039;d be avoiding stepping into some other defined variable with the same name.</description>
		<content:encoded><![CDATA[<p>Namespacing. By using anonymous functions you are not polluting the namespace with function names. The equivalent way of doing this with named functions would be to use a closure like this:</p>
<pre name="code" class="js:nogutter:nocontrols">
  (function() {
    var selectPath = function(node, val) {
      if(node == null) return;
      node.selected = val;
      return selectPath(node.parent, val);
    };
    selectPath(nodePrev, false);
    selectPath(nodeNew, true);
  })();
</pre>
<p>That way you&#8217;d be avoiding stepping into some other defined variable with the same name.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: salty-horse</title>
		<link>http://blog.thejit.org/2008/12/27/anonymous-recursive-functions/comment-page-1/#comment-304</link>
		<dc:creator>salty-horse</dc:creator>
		<pubDate>Sat, 27 Dec 2008 14:35:32 +0000</pubDate>
		<guid isPermaLink="false">http://blog.thejit.org/?p=204#comment-304</guid>
		<description>What are the benefits of using the function anonymously instead of naming it?</description>
		<content:encoded><![CDATA[<p>What are the benefits of using the function anonymously instead of naming it?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mike McNally</title>
		<link>http://blog.thejit.org/2008/12/27/anonymous-recursive-functions/comment-page-1/#comment-303</link>
		<dc:creator>Mike McNally</dc:creator>
		<pubDate>Sat, 27 Dec 2008 14:09:23 +0000</pubDate>
		<guid isPermaLink="false">http://blog.thejit.org/?p=204#comment-303</guid>
		<description>I know this may take the fun out of your post, but it&#039;s possible to give your function a name. After the &quot;function&quot; keyword can come an identifier, and that&#039;s bound in a way similar to what &quot;letrec&quot; does in Scheme.</description>
		<content:encoded><![CDATA[<p>I know this may take the fun out of your post, but it&#8217;s possible to give your function a name. After the &#8220;function&#8221; keyword can come an identifier, and that&#8217;s bound in a way similar to what &#8220;letrec&#8221; does in Scheme.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
