<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/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>Tom Moertel's Weblog: Tag fp</title>
    <link>http://blog.moertel.com/articles/tag/fp?tag=fp</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Quality rants on programming theory and stuff geeks like</description>
    <item>
      <title>Oleg's great way of explaining delimited continuations</title>
      <description>&lt;p&gt;There&amp;#8217;s a great way to explain delimited continuations in the notes of &lt;a href="http://okmij.org/ftp/Computation/Continuations.html#shift-cgi"&gt;Oleg&amp;#8217;s Continuation Fest talk on using delimited continuations for &lt;span class="caps"&gt;CGI&lt;/span&gt; programming&lt;/a&gt;.  Just so it doesn&amp;#8217;t get overlooked, here it is:&lt;/p&gt;


&lt;blockquote&gt;I&amp;#8217;m obsessed in pointing out that every programmer already knows
and understands the delimited continuations; they might not know that
word though.  Everyone knows that when a process executes a system
call like &lt;code&gt;read&lt;/code&gt;, it gets &lt;em&gt;suspended&lt;/em&gt;. When the disk
delivers the data, the process is &lt;em&gt;resumed&lt;/em&gt;. That suspension of a
process is its continuation. It is delimited: it is not the
check-point of the whole OS, it is the check-point of a process only,
from the invocation of &lt;code&gt;main()&lt;/code&gt; up to the point
&lt;code&gt;main()&lt;/code&gt; returns. Normally these suspensions are resumed
only once, but can be zero times (&lt;code&gt;exit&lt;/code&gt;) or twice
(&lt;code&gt;fork&lt;/code&gt;).&lt;/blockquote&gt;

&lt;p&gt;I especially like the final part about &lt;code&gt;exit&lt;/code&gt; and
&lt;code&gt;fork&lt;/code&gt;, which drives home the notion that something more
subtle than returning from a typical function call is going on. If
anybody is confused over what &lt;em&gt;suspended&lt;/em&gt; means, that last part
ought to clear things up.&lt;/p&gt;

&lt;p&gt;The next time I need to explain delimited continuations, I know how
I&amp;#8217;m going to do it.&lt;/p&gt;</description>
      <pubDate>Mon, 05 May 2008 09:58:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:9030a189-530b-48b3-b5e1-ed41ab5c2467</guid>
      <author>Tom Moertel</author>
      <link>http://blog.moertel.com/articles/2008/05/05/olegs-great-way-of-explaining-delimited-continuations</link>
      <category>functional programming</category>
      <category>continuations</category>
      <category>fp</category>
      <category>oleg</category>
      <trackback:ping>http://blog.moertel.com/articles/trackback/724</trackback:ping>
    </item>
    <item>
      <title>Composing functions in Ruby</title>
      <description>&lt;p&gt;One of the things I miss when coding in Ruby is
inexpensive function composition.  In Haskell, for example,
I can compose functions using the dot (.) operator:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;inc        = (+1)
twice      = (*2)
twiceOfInc = twice . inc
&lt;/code&gt;&lt;/pre&gt;

Because of Ruby&amp;#8217;s open classes, however, I can easily
add the feature to the language.  In
the code below, I introduce
&lt;code&gt;Proc.compose&lt;/code&gt; and overload the
star (&lt;code&gt;*&lt;/code&gt;) operator for the purpose:

&lt;pre&gt;&lt;code&gt;# func_composition.rb
class Proc
  def self.compose(f, g)
    lambda { |*args| f[g[*args]] }
  end
  def *(g)
    Proc.compose(self, g)
  end
end
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;And that&amp;#8217;s all it takes:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;$ irb --simple-prompt -r func_composition.rb

&amp;gt;&amp;gt; inc = lambda { |x| x + 1 }
=&amp;gt; #&amp;lt;Proc:0x00002aaaaaad7810@(irb):1&amp;gt;

&amp;gt;&amp;gt; twice = lambda { |x| x * 2 }
=&amp;gt; #&amp;lt;Proc:0x00002aaaaabd2d18@(irb):2&amp;gt;

&amp;gt;&amp;gt; inc[1]
=&amp;gt; 2

&amp;gt;&amp;gt; twice[2]
=&amp;gt; 4

&amp;gt;&amp;gt; twice_of_inc = twice * inc
=&amp;gt; #&amp;lt;Proc:0x00002aaaaab32458@./func_composition.rb:3&amp;gt;

&amp;gt;&amp;gt; twice_of_inc[1]
=&amp;gt; 4

&amp;gt;&amp;gt; twice_of_inc[2]
=&amp;gt; 6
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Now, isn&amp;#8217;t that refreshing?&lt;/p&gt;


&lt;div class="update"&gt;
&lt;strong&gt;Update:&lt;/strong&gt; Vincent Foley &lt;a href="http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/6102f784210bdb32/05dfa12e07513a2c#05dfa12e07513a2c"&gt;pointed out on comp.lang.ruby&lt;/a&gt; that &lt;a href="http://facets.rubyforge.org/"&gt;Ruby Facets&lt;/a&gt; has a &lt;a href="http://facets.rubyforge.org/api/core/classes/Proc.html"&gt;nearly identical implementation&lt;/a&gt; that also uses the star operator for composition.  (Its version of &lt;em&gt;compose&lt;/em&gt;, however, is an instance method whereas my version is a class method.)
&lt;/div&gt;</description>
      <pubDate>Fri, 07 Apr 2006 11:55:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:0d98557f896ddc1d75ff1f85eb283602</guid>
      <author>Tom Moertel</author>
      <link>http://blog.moertel.com/articles/2006/04/07/composing-functions-in-ruby</link>
      <category>functional programming</category>
      <category>ruby</category>
      <category>ruby</category>
      <category>functional_programming</category>
      <category>fp</category>
      <trackback:ping>http://blog.moertel.com/articles/trackback/62</trackback:ping>
    </item>
  </channel>
</rss>
