<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Functional - Code of Serge]]></title><description><![CDATA[Throw enough spaghetti at the wall, maybe something will stick.]]></description><link>http://codeofserge.com/</link><generator>Ghost 0.5</generator><lastBuildDate>Thu, 10 Sep 2026 22:58:21 GMT</lastBuildDate><atom:link href="http://codeofserge.com/tag/functional/rss/" rel="self" type="application/rss+xml"/><ttl>60</ttl><item><title><![CDATA[y combinator]]></title><description><![CDATA[<p>Every now and again you hear developers at some programming event talking about this fabled myth of a function, the understanding of which will transcend you to a new plane of programming.   Well, challenge accepted.  Spoiler, I am still among us mortals.</p>

<p><strong>Thinking About the Problem</strong></p>

<p>The first place you go: <a href="http://en.wikipedia.org/wiki/Fixed-point_combinator#Derivation_of_the_Y_combinator">wiki</a>.  If you are anything like me,  lambda calculus hurts your head and only paints a fuzzy picture.  Heavy math aside, what problem does it solve? It lets us have recursion of anonymous functions.</p>

<p><strong>Factorial function to the rescue</strong></p>

<pre><code class="language-javascript">function factorial(num) {  
  return num === 0 ? 1 : num * factorial(num -1);  
}  
</code></pre>

<p>To define recursion you must first define recursion.  </p>

<p>But seriously, in order to be able to make this function anonymous, we need to get rid of our dependency on "<em>factorial</em>"  I agree, crazy.  But lets give it a try.  Some closure?  Maybe we can  wrap it in a function? We no longer call the function by its name, does that help? Maybe if we can somehow call the wrapper function with the result of itself.</p>

<pre><code class="language-javascript">function(factorial) {  
  return function(num) {   
    return num === 0 ? 1 : num * factorial(num -1);  
  };
}
</code></pre>

<p>And queue the entrance of the Y Combinator!</p>

<pre><code class="language-javascript">function Y(bindFn) {  
  return function () {  
    return bindFn(Y(bindFn)).apply(null, arguments);  
  };  
}  
</code></pre>

<p><strong>Untwisting the Pretzel</strong></p>

<p>Right, that's what I thought, that a bit crazy. The factorial wrapper gets passed into it self, is that even possible?  Seems so, here is a trace: (<a href="http://jsbin.com/rakica/edit?js,console">jsbin</a>)  </p>

<p>There is a beauty to this almost mechanical algorithm, reminiscent of the sewing machine or the RNA copy mechanism.  </p>

<ul>
<li>Wrap the anonymous function.</li>
<li>Send in the wrapped anonymous function into the Y combinator function.</li>
<li>Unpack the anonymous function and execute it, by passing the Y combinator result of the wrapped function into the wrapped function.</li>
</ul>

<p><img src="http://materialmama.files.wordpress.com/2007/01/ani_lockstitch2.gif?w=371&amp;h=387" alt="sewing machine"></p>]]></description><link>http://codeofserge.com/y-combinator/</link><guid isPermaLink="false">4544635e-53b9-49e7-8fab-723e1d85ed79</guid><category><![CDATA[Theory]]></category><category><![CDATA[Functional]]></category><dc:creator><![CDATA[Sergei Golos]]></dc:creator><pubDate>Fri, 09 Jan 2015 16:59:20 GMT</pubDate></item></channel></rss>