<?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: Composing functions in Ruby</title>
    <link>http://blog.moertel.com/articles/2006/04/07/composing-functions-in-ruby</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Quality rants on programming theory and stuff geeks like</description>
    <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>
    <item>
      <title>"Composing functions in Ruby" by TaQ</title>
      <description>&lt;p&gt;Hi there!&lt;/p&gt;


	&lt;p&gt;I think you&amp;#8217;d like to see something I did based on the problem described here:&lt;/p&gt;


	&lt;p&gt;&lt;a href="http://eustaquiorangel.com/blog/show/460"&gt;http://eustaquiorangel.com/blog/show/460&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;It&amp;#8217;s a Brazilian Portuguese post but the code speaks for itself. :-)&lt;/p&gt;


	&lt;p&gt;Best regards,&lt;/p&gt;</description>
      <pubDate>Mon, 10 Sep 2007 14:37:31 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:81013a3c-98a4-4583-b655-391f57ba8e2f</guid>
      <link>http://blog.moertel.com/articles/2006/04/07/composing-functions-in-ruby#comment-575</link>
    </item>
    <item>
      <title>"Composing functions in Ruby" by Peter</title>
      <description>&lt;p&gt;Thanks for this, Tom. I hope to see more Haskell constructs translated to Ruby in   the future!&lt;/p&gt;</description>
      <pubDate>Tue, 06 Mar 2007 09:40:16 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:4b075dc5-1ce6-40c3-8e63-f06c6a7aee70</guid>
      <link>http://blog.moertel.com/articles/2006/04/07/composing-functions-in-ruby#comment-400</link>
    </item>
  </channel>
</rss>
