<?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>Trypticon: Higher Order Messages for Mapping</title>
    <link>http://trypticon.org/articles/2005/11/04/higher-order-messages-for-mapping</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>If it ain't broke, break it.</description>
    <item>
      <title>Higher Order Messages for Mapping</title>
      <description>&lt;p&gt;Say you want to convert an array of strings to uppercase. You could write&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;result = names.map { |name| name.upcase }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://blogs.pragprog.com/cgi-bin/pragdave.cgi/Tech/Ruby/ToProc.rdoc" title="PragDave (Blog)"&gt;PragDave&lt;/a&gt; suggests an alternative which uses the &lt;a href="http://extensions.rubyforge.org/" title="Ruby Extensions Project (Web Site)"&gt;Ruby Extensions Project&lt;/a&gt;&amp;#8217;s Symbol#to_proc method to pass a symbol.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;result = names.map(&amp;amp;:upcase)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This is all well and good, but can you really call it elegant?  Suppose you want to add 1 to all elements in a list.  How can this method let you pass in that parameter?  It seems that it can&amp;#8217;t.&lt;/p&gt;

&lt;p&gt;Higher Order Messages (HOM) provide a better way, although a way which seems even more like magic.&lt;/p&gt;

&lt;p&gt;The &lt;a href="http://rubyforge.org/projects/homer" title="Homer: Higher Order Message Extension to Ruby"&gt;Homer&lt;/a&gt; project offers HOM for Ruby, and although it didn&amp;#8217;t have a method which did something like map, it was relatively simple to whip something together.&lt;/p&gt;

&lt;p&gt;My own version of the above functionality:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;result = names.map_using.upcase
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Isn&amp;#8217;t that better?  I couldn&amp;#8217;t call it map without causing a little mayhem, having to alias the old version and still provide complete compatibility was a bit much for me at this point in time.&lt;/p&gt;

&lt;p&gt;But better yet, this version &lt;em&gt;does&lt;/em&gt; permit you to use parameters:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;costs_with_gst = costs.map_using * 1.1
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Don&amp;#8217;t bother asking for the source code for this&amp;#8230; I&amp;#8217;ll be shuffling it up to the Homer project as soon as I hit submit. ;-)&lt;/p&gt;</description>
      <pubDate>Fri, 04 Nov 2005 10:29:00 +1100</pubDate>
      <guid isPermaLink="false">urn:uuid:eebc6dfa-f369-4556-92d9-b28b223bdaa3</guid>
      <author>Trejkaz</author>
      <link>http://trypticon.org/articles/2005/11/04/higher-order-messages-for-mapping</link>
      <category>programming</category>
      <category>ruby</category>
    </item>
  </channel>
</rss>
