<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Khaled alHabache's official blog &#187; currying</title>
	<atom:link href="http://www.khelll.com/blog/tag/currying/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.khelll.com/blog</link>
	<description>What web development means....</description>
	<lastBuildDate>Tue, 16 Mar 2010 23:21:41 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Ruby Currying</title>
		<link>http://www.khelll.com/blog/ruby/ruby-currying/</link>
		<comments>http://www.khelll.com/blog/ruby/ruby-currying/#comments</comments>
		<pubDate>Mon, 25 May 2009 04:36:46 +0000</pubDate>
		<dc:creator>khelll</dc:creator>
				<category><![CDATA[Functional Programming]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[currying]]></category>

		<guid isPermaLink="false">http://www.khelll.com/blog/?p=401</guid>
		<description><![CDATA[Update: This post was updated to show the difference between Currying and Partial Functions.
Currying is a concept in Functional Programming that&#8217;s enabled by Higher-order functions. It&#8217;s best described as: the ability to take a function that accepts n parameters and turns it into a composition of n functions each of them take 1 parameter.  [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Update: This post was updated to show the difference between Currying and Partial Functions.</strong><br />
<strong>Currying</strong> is a concept in Functional Programming that&#8217;s enabled by Higher-order functions. It&#8217;s best described as: the ability to take a function that accepts n parameters and turns it into a composition of <code>n</code> functions each of them take 1 parameter.  Check this function <em>f</em> which takes 3 params <em>x</em>,<em>y</em>,<em>z</em></p>
<p><code>f(x,y,z) = 4*x+3*y+2*z</code></p>
<p>Currying means that we can rewrite the function as a composition of 3 functions(a function for each param):</p>
<p><code>f(x)(y)(z) = 2*z+(3*y+(4*x))</code></p>
<p>The direct use of this is what is called <strong>Partial Function</strong> where if you have a function that accepts n parameters then you can generate from it one of more functions with some parameter values already filled in.  Ruby 1.9 comes with support for currying concept(through the Proc#curry method) and this blog post is explaining how you can use it effectively.<br />
I&#8217;m going to walk you through a simple example to explain the concept, however i need to mention few things:<br />
1- The main example is taken from the free <a href="http://www.scala-lang.org/sites/default/files/linuxsoft_archives/docu/files/ScalaByExample.pdf">Scala By Example</a> book(First-Class Functions chapter), however i have replaced recursion calls by simple <code>upto</code> iterators.<br />
2- In Ruby 1.9 you can use block.(*args) just like you use <code>block.call(*args)</code> or <code>block[*args]</code> in Ruby 1.8, so i&#8217;ll stick to  <code>block.(*)</code> notation.<br />
3- I could have used the <code>inject</code> method, but i preferred readability to concise code. </p>
<p>Let&#8217;s start the simple tutorial by adding three methods:<br />
1- A method to sum all integers between two given numbers <code>a</code> and <code>b</code>.<br />
2- A method to sum the squares of all integers between two given numbers <code>a</code> and <code>b</code>.<br />
3- A method to to sum the powers <code>2^n</code> of all integers <code>n</code> between two given numbers <code>a</code> and <code>b</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">###################################</span>
<span style="color:#008000; font-style:italic;"># Normal definitions</span>
<span style="color:#008000; font-style:italic;">###################################</span>
sum_ints = <span style="color:#CC0066; font-weight:bold;">lambda</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>a,b<span style="color:#006600; font-weight:bold;">|</span>
  s = <span style="color:#006666;">0</span> ; a.<span style="color:#9900CC;">upto</span><span style="color:#006600; font-weight:bold;">&#40;</span>b<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>n<span style="color:#006600; font-weight:bold;">|</span> s <span style="color:#006600; font-weight:bold;">+</span>= n <span style="color:#006600; font-weight:bold;">&#125;</span> ; s 
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
sum_of_squares = <span style="color:#CC0066; font-weight:bold;">lambda</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>a,b<span style="color:#006600; font-weight:bold;">|</span>
  s = <span style="color:#006666;">0</span> ; a.<span style="color:#9900CC;">upto</span><span style="color:#006600; font-weight:bold;">&#40;</span>b<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>n<span style="color:#006600; font-weight:bold;">|</span> s <span style="color:#006600; font-weight:bold;">+</span>= n<span style="color:#006600; font-weight:bold;">**</span><span style="color:#006666;">2</span> <span style="color:#006600; font-weight:bold;">&#125;</span> ;s 
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
sum_of_powers_of_2 = <span style="color:#CC0066; font-weight:bold;">lambda</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>a,b<span style="color:#006600; font-weight:bold;">|</span>
  s = <span style="color:#006666;">0</span> ; a.<span style="color:#9900CC;">upto</span><span style="color:#006600; font-weight:bold;">&#40;</span>b<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>n<span style="color:#006600; font-weight:bold;">|</span> s <span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">**</span>n <span style="color:#006600; font-weight:bold;">&#125;</span> ; s 
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> sum_ints.<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1</span>,<span style="color:#006666;">5</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;">#=&gt; 15</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> sum_of_squares.<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1</span>,<span style="color:#006666;">5</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;">#=&gt; 55</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> sum_of_powers_of_2.<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1</span>,<span style="color:#006666;">5</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;">#=&gt; 62</span></pre></div></div>

<p>Cool, however if you focus on the 3 methods, you will notice that these methods are all instances of a pattern &Sigma;f(n) for a range of  values a -> b. We can factor out the common pattern by defining a method sum:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">############################################</span>
<span style="color:#008000; font-style:italic;"># Some refactoring to make some abstraction</span>
<span style="color:#008000; font-style:italic;"># Passing the sum mechanism itself</span>
<span style="color:#008000; font-style:italic;">############################################</span>
&nbsp;
sum = <span style="color:#CC0066; font-weight:bold;">lambda</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>f,a,b<span style="color:#006600; font-weight:bold;">|</span>
  s = <span style="color:#006666;">0</span> ; a.<span style="color:#9900CC;">upto</span><span style="color:#006600; font-weight:bold;">&#40;</span>b<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>n<span style="color:#006600; font-weight:bold;">|</span> s <span style="color:#006600; font-weight:bold;">+</span>= f.<span style="color:#006600; font-weight:bold;">&#40;</span>n<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span> ; s 
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> sum.<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">lambda</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span> x<span style="color:#006600; font-weight:bold;">&#125;</span>,<span style="color:#006666;">1</span>,<span style="color:#006666;">5</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;">#=&gt; 15</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> sum.<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">lambda</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span> x<span style="color:#006600; font-weight:bold;">**</span><span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#125;</span>,<span style="color:#006666;">1</span>,<span style="color:#006666;">5</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;">#=&gt; 55</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> sum.<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">lambda</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">**</span>x<span style="color:#006600; font-weight:bold;">&#125;</span>,<span style="color:#006666;">1</span>,<span style="color:#006666;">5</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;">#=&gt; 62</span></pre></div></div>

<p>Ok, but what about having the formal definitions for the 3 methods? How can we have those definitions out of the <code>sum</code> method? Well that&#8217;s the use of currying and partial functions, in our case we just need to pass the first param to the <code>sum</code> method to specify what type of sum is it:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">###################################</span>
<span style="color:#008000; font-style:italic;"># More refactoring using currying</span>
<span style="color:#008000; font-style:italic;"># Currying was added to Ruby 1.9</span>
<span style="color:#008000; font-style:italic;"># Via Proc#curry</span>
<span style="color:#008000; font-style:italic;">###################################</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># generate the currying</span>
currying = sum.<span style="color:#9900CC;">curry</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Generate the partial functions</span>
sum_ints = currying.<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">lambda</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span> x<span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
sum_of_squares = currying.<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">lambda</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span> x<span style="color:#006600; font-weight:bold;">**</span><span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
sum_of_powers_of_2 = currying.<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">lambda</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">**</span>x<span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> sum_ints.<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1</span>,<span style="color:#006666;">5</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;">#=&gt; 15</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> sum_of_squares.<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1</span>,<span style="color:#006666;">5</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;">#=&gt; 55</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> sum_of_powers_of_2.<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1</span>,<span style="color:#006666;">5</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;">#=&gt; 62</span></pre></div></div>

<p>That&#8217;s it! I hope I could clarify the use of currying, if not just add your comment here <img src='http://www.khelll.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  </p>
]]></content:encoded>
			<wfw:commentRss>http://www.khelll.com/blog/ruby/ruby-currying/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>
