<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Talk Like A Duck: Tag closures</title>
    <link>http://talklikeaduck.denhaven2.com/articles/tag/closures</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>In Ruby, it's not the dog, it's the tricks!</description>
    <item>
      <title>Update on Continuations</title>
      <description>Robert Dober discovered that the continuation example which I gave in &lt;a href="http://talklikeaduck.denhaven2.com/articles/2007/06/05/closing-in-on-closures-and-jumping-into-continuations"&gt;last night&amp;#8217;s article&lt;/a&gt; behaves badly if you run it with ruby rather than irb.&lt;/p&gt;
&lt;p&gt;I&amp;#8217;ve made note of this in the original article, and I&amp;#8217;ve added a simpler example which does work under ruby. I&amp;#8217;ll try to come back with a new article which shows an example of how continuations are normally used.&lt;/p&gt;</description>
      <pubDate>Wed, 06 Jun 2007 17:00:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:d75926cc-6f91-41e4-a722-633ed35ed5d5</guid>
      <author>Rick DeNatale</author>
      <link>http://talklikeaduck.denhaven2.com/articles/2007/06/06/update-on-continuations</link>
      <category>ruby_for_nubys</category>
      <category>ruby</category>
      <category>closures</category>
      <category>continuations</category>
    </item>
    <item>
      <title>Closing in on Closures and Jumping into Continuations</title>
      <description>Recently on ruby-talk someone asked if continuations and closures were the same thing.
&lt;p&gt;They are related in that they both are tied to a particular point in time in the execution of a program.  The difference is that closures are like a souvenir of a trip, while continuations are like a kind of time-machine.

&lt;h2&gt;Closures&lt;/h2&gt;
The general meaning of the term closure in computer science is a function which captures one or more variable bindings in effect at the time it was created.  Closures are created in Ruby using lambda or its alias proc:
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;irb(main):001:0&amp;gt; def closure(a)
irb(main):002:1&amp;gt;   lambda {a}
irb(main):003:1&amp;gt;   end
=&amp;gt; nil
irb(main):004:0&amp;gt; cl1 = closure(1)
=&amp;gt; #&amp;lt;Proc:0xb7b3cf10@(irb):2&amp;gt;
irb(main):005:0&amp;gt; cl2 = closure(2)
=&amp;gt; #&amp;lt;Proc:0xb7b3cf10@(irb):2&amp;gt;
irb(main):006:0&amp;gt; cl1.call
=&amp;gt; 1
irb(main):007:0&amp;gt; cl2.call
=&amp;gt; 2&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Notice that the block in line 2 refers to the method parameter a, and returns the value of a as the blocks value.  The Kernel#lamba method turns the block into a Proc object which is returned as the value of the call.&lt;/p&gt;
&lt;p&gt;in lines 4 and 5 we make two calls to the method closure with different arguments, and save the resultant closures in cl1 and cl2 respectively.&lt;/p&gt;
&lt;p&gt;Notice that when we call each closure in lines 6 and 7, each one returns the value passed to cont when that closure was created.
&lt;h2&gt;Continuations&lt;/h2&gt;
A continuation, on the other hand, represents a particular point of execution, not [just] variable bindings.  When a continuation is called, it acts like (the dreaded) goto, we jump to the point right after the creation of the continuation.  In Ruby we create a continuation with the Kernel#callcc method.  This method takes a block and yields it a new continuation object.
&lt;p&gt;The mystical aspect of continuations is that they allow us to jump back into the middle of the execution of a method which has already returned:
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;irb(main):008:0&amp;gt; def continuation
irb(main):009:1&amp;gt;   i = 0
irb(main):010:1&amp;gt;   lola = nil
irb(main):011:1&amp;gt;   callcc {|cc| puts &amp;quot;In callcc&amp;quot;;lola = cc}
irb(main):012:1&amp;gt;   puts &amp;quot;i is now #{i}&amp;quot;
irb(main):013:1&amp;gt;   i += 1
irb(main):014:1&amp;gt;   lola
irb(main):015:1&amp;gt;   end
=&amp;gt; nil
irb(main):016:0&amp;gt; cont = continuation
In callcc
i is now 0
=&amp;gt; #&amp;lt;Continuation:0xb7b727a0&amp;gt;
irb(main):017:0&amp;gt; cont.call
i is now 1
=&amp;gt; #&amp;lt;Continuation:0xb7b727a0&amp;gt;
irb(main):018:0&amp;gt; cont.call
i is now 2
=&amp;gt; #&amp;lt;Continuation:0xb7b727a0&amp;gt;                             &lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Line 10 makes the ruby interpreter see that lola is a local variable of the method and so the block can set it and the method can return it. &lt;/p&gt;
&lt;p&gt;Note that the call to the continuation method in line 16 is the only time we actually evaluate callcc and the block.  The call to callcc returns and the rest of the continuation method is excuted and the method returns.&lt;/p&gt;
&lt;p&gt;Each time we call the continuation referenced by the variable cont, we execute the code afer the callcc which created it.  It acts like a closure in a sense since it captures the last state of the variables i and lola each time. But this is because the continuation represents a particular execution state which includes not only the variebles in scope at that time, but also the program counter.
&lt;h2&gt;&lt;span class="caps"&gt;FLASH&lt;/span&gt; Breaking News: Rick has a Ruby Red Face&lt;/h2&gt;
&lt;p&gt;Robert Dober pointed out that if you run the above code in ruby rather than in irb it loops on the first cont.call, printing out:
&lt;p&gt;i is now 1&lt;/p&gt;
&lt;p&gt;i is now 1&lt;/p&gt;
&lt;p&gt;i is now 1&lt;/p&gt;
&lt;p&gt;ad infinitum (or at least ad ctrl-C)&lt;/p&gt;
&lt;p&gt;I was actually abusing continuations, since in general they aren&amp;#8217;t meant to be reused.  I&amp;#8217;ll write a new posting in the near future on how continuations are used in the Ruby standard library Generator class, In the meantime, here&amp;#8217;s a simpler example which runs in ruby without trying to reuse the continuation:
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;continuation&lt;/span&gt;
  &lt;span class="ident"&gt;puts&lt;/span&gt; &lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;Before callcc&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;
  &lt;span class="ident"&gt;callcc&lt;/span&gt; &lt;span class="punct"&gt;{|&lt;/span&gt;&lt;span class="ident"&gt;lola&lt;/span&gt;&lt;span class="punct"&gt;|&lt;/span&gt; &lt;span class="ident"&gt;puts&lt;/span&gt; &lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;In callcc&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;;&lt;/span&gt;&lt;span class="keyword"&gt;return&lt;/span&gt; &lt;span class="ident"&gt;lola&lt;/span&gt;&lt;span class="punct"&gt;}&lt;/span&gt;
  &lt;span class="ident"&gt;puts&lt;/span&gt; &lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;After callcc&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;

&lt;span class="ident"&gt;puts&lt;/span&gt; &lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;Begin&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;
&lt;span class="ident"&gt;cont&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="ident"&gt;continuation&lt;/span&gt;
&lt;span class="ident"&gt;puts&lt;/span&gt; &lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;Got cont &lt;span class="expr"&gt;#{cont.inspect}&lt;/span&gt;&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;
&lt;span class="keyword"&gt;if&lt;/span&gt; &lt;span class="ident"&gt;cont&lt;/span&gt;
  &lt;span class="ident"&gt;cont&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;call&lt;/span&gt;
  &lt;span class="ident"&gt;puts&lt;/span&gt; &lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;After call&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;
&lt;span class="ident"&gt;puts&lt;/span&gt; &lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;All done&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If we run this using ruby we see:&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;$ ruby testcontin.rb
Begin
Before callcc
In callcc
Got cont #&amp;lt;Continuation:0xb7de3840&amp;gt;
After callcc
Got cont nil
All done&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;Continuations in other disciplines&lt;/h2&gt;
You might get a sense of deja-vu from this discussion of continuations since a similar idea pops up in the movies from time to time. Two examples which come to mind are the American movie &amp;#8220;Groundhog Day,&amp;#8221; and the German movie &amp;#8220;Lola rennt&amp;#8221; or in English, &amp;#8220;Run Lola, Run.&amp;#8221; 
&lt;h2&gt;Another explanation&lt;/h2&gt;
SamRuby &lt;a href="http://www.intertwingly.net/blog/2005/04/13/Continuations-for-Curmudgeons"&gt;this explanation of continuations&lt;/a&gt; a couple of years ago.  It&amp;#8217;s valuable as a companion to this article since it has a somewhat different approach.  I do have a quibble with Sam in that I don&amp;#8217;t agree with his statement that a closure is a &amp;#8220;continuation&#8217;s more general cousin.&amp;#8221;  Some continuations are, or act a little like, closures, but closures aren&amp;#8217;t continuations since they lack the aspect of holding a point of executioncont</description>
      <pubDate>Tue, 05 Jun 2007 21:41:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:728a7e00-76b4-44df-8faa-701c1a006c9e</guid>
      <author>Rick DeNatale</author>
      <link>http://talklikeaduck.denhaven2.com/articles/2007/06/05/closing-in-on-closures-and-jumping-into-continuations</link>
      <category>ruby_for_nubys</category>
      <category>ruby</category>
      <category>closures</category>
      <category>continuations</category>
    </item>
  </channel>
</rss>
