<?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; Functional Programming</title>
	<atom:link href="http://www.khelll.com/blog/category/functional-programming/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>
		<item>
		<title>Ruby and Functional Programming</title>
		<link>http://www.khelll.com/blog/ruby/ruby-and-functional-programming/</link>
		<comments>http://www.khelll.com/blog/ruby/ruby-and-functional-programming/#comments</comments>
		<pubDate>Sun, 24 May 2009 12:28:30 +0000</pubDate>
		<dc:creator>khelll</dc:creator>
				<category><![CDATA[Functional Programming]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.khelll.com/blog/?p=351</guid>
		<description><![CDATA[Ruby is known to support the functional paradigm. This article is going to walk you through the Functional Programming page on WikiPedia, to revise the general concepts of functional programming and to explain how Ruby supports them.
According to wikipedia, a functional programming can be described as follows:
In computer science, functional programming is a programming paradigm [...]]]></description>
			<content:encoded><![CDATA[<p>Ruby is known to support the functional paradigm. This article is going to walk you through the Functional Programming <a href="http://en.wikipedia.org/wiki/Functional_programming">page</a> on WikiPedia, to revise the general concepts of functional programming and to explain how Ruby supports them.<br />
According to wikipedia, a functional programming can be described as follows:</p>
<blockquote><p>In computer science, functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data. It emphasizes the application of functions, in contrast to the imperative programming style, which emphasizes changes in state. Functional programming has its roots in the lambda calculus, a formal system developed in the 1930s to investigate function definition, function application, and recursion. Many functional programming languages can be viewed as embellishments to the lambda calculus.</p></blockquote>
<p>That&#8217;s a clear definition, however and if it&#8217;s not, the following walk through some <strong>concepts</strong> of functional programming should result in a better understanding:  </p>
<h2>Pure functions</h2>
<blockquote><p>In practice, the difference between a mathematical function and the notion of a &#8220;function&#8221; used in imperative programming is that imperative functions can have side effects, changing the value of already calculated computations. Because of this they lack referential transparency, i.e. the same language expression can result in different values at different times depending on the state of the executing program. Conversely, in functional code, the output value of a function depends only on the arguments that are input to the function, so calling a function f twice with the same value for an argument x will produce the same result f(x) both times. Eliminating side-effects can make it much easier to understand and predict the behavior of a program, which is one of the key motivations for the development of functional programming.</p></blockquote>
<p>Pure functions support the mathematical definition of a function:</p>
<p><em> y = f(x)</em></p>
<p>Which means:<br />
1- A function should return the same exact value given the same exact input.<br />
2- A function does one thing exactly(has one clear mission), it has no side effects like changing some other value or write to stream or any other mission rather than its assigned one.</p>
<p>In fact Ruby doesn&#8217;t force you to write pure functions, but certainly it helps you to do so. Actually exercising yourself to write in pure functional style when possible helps you really in many ways:<br />
1- It makes your code clean, readable and self-documenting.<br />
2- It makes your code more &#8220;thread safe&#8221;<br />
3- It helps you more when it comes to TDD.</p>
<p>Let&#8217;s have a look on how Ruby helps you do Pure functional code:</p>
<h3>The <code>!</code> symbol</h3>
<p>Most of Ruby programmers know the <code>!</code> symbol that comes at the end of method names like <code>gsub!</code>, <code>merge!</code> and many other methods. The <code>!</code> symbol means: &#8220;Use it cautiously&#8221;. However If you focus more, you will notice that <strong>most of time</strong>, the <code>!</code> symbol means: &#8220;This is a method that has a side effect; it alters the object which is invoked on!&#8221;.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">s = <span style="color:#996600;">&quot;hello&quot;</span> <span style="color:#008000; font-style:italic;">#=&gt; &quot;hello&quot;</span>
up_s = s.<span style="color:#9900CC;">upcase</span> <span style="color:#008000; font-style:italic;">#=&gt; &quot;HELLO&quot;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> s.<span style="color:#9900CC;">upcase</span>! <span style="color:#008000; font-style:italic;">#=&gt; &quot;HELLO&quot;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> s <span style="color:#008000; font-style:italic;">#=&gt; &quot;HELLO&quot;</span></pre></div></div>

<p>As you can see, the <code>upcase!</code> method is not a pure functional method cause it has a side effect, it changes the string itself. On the other hand, the <code>upcase</code> method is a pure functional one as it returns the corresponding value with no other side effects.</p>
<p>On all hows, it&#8217;s highly recommended that you stick to this convention of method naming when you code in Ruby.</p>
<h3>Expressions</h3>
<p><strong>Everything is evaluated as an expression in Ruby</strong>: literals, method calls, variables&#8230; even a control structure has a value. For example you can do this in Ruby:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">if</span> num == <span style="color:#996600;">&quot;one&quot;</span> <span style="color:#9966CC; font-weight:bold;">then</span> val = <span style="color:#006666;">1</span>
<span style="color:#9966CC; font-weight:bold;">elsif</span> num == <span style="color:#996600;">&quot;two&quot;</span> <span style="color:#9966CC; font-weight:bold;">then</span> val = <span style="color:#006666;">2</span>
<span style="color:#9966CC; font-weight:bold;">else</span> <span style="color:#9966CC; font-weight:bold;">then</span> val = <span style="color:#006666;">3</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>But that is a weak use of Ruby&#8217;s power, instead you&#8217;d better do:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">val = <span style="color:#9966CC; font-weight:bold;">if</span> num == <span style="color:#996600;">&quot;one&quot;</span> <span style="color:#9966CC; font-weight:bold;">then</span>   <span style="color:#006666;">1</span>
<span style="color:#9966CC; font-weight:bold;">elsif</span> num == <span style="color:#996600;">&quot;two&quot;</span> <span style="color:#9966CC; font-weight:bold;">then</span>  <span style="color:#006666;">2</span>
<span style="color:#9966CC; font-weight:bold;">else</span> <span style="color:#006666;">3</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<h3>Return value</h3>
<p>Ruby returns the last expression value as the return value of the method invocation, so you don&#8217;t need an explicit <code>return</code> statement.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> greeting<span style="color:#006600; font-weight:bold;">&#40;</span>name<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#996600;">&quot;Hello #{name}!&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> greeting<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'khelll'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;">#=&gt; &quot;Hello khelll!&quot;</span></pre></div></div>

<p>Together, the auto returned value and evaluating code as expressions are indeed a big support for pure functional code as finally they both serve the fact of having a value out of your method.</p>
<h2>Higher-order functions</h2>
<p>Functions are higher-order when they can take other functions as arguments, and return them as results. This is done in Ruby using lambda and block logic. If you don&#8217;t know how to use blocks, please do yourself a favor and visit this <a href="http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas/">comprehensive article</a> by Robert Sosinski.</p>
<p>Blocks in Ruby can help you do very nice things, one of them is ability to add control structure like syntax to your code. Check the following example, where a loop control structure is being defined:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># a trivial example that adds a loop control structure</span>
<span style="color:#008000; font-style:italic;"># it takes a range and yields the passed block.</span>
<span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#CC0066; font-weight:bold;">loop</span><span style="color:#006600; font-weight:bold;">&#40;</span>x,<span style="color:#006600; font-weight:bold;">&amp;</span>b<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">for</span> i <span style="color:#9966CC; font-weight:bold;">in</span> x <span style="color:#9966CC; font-weight:bold;">do</span>
    b.<span style="color:#9900CC;">call</span><span style="color:#006600; font-weight:bold;">&#40;</span>i<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># use the above defined method</span>
<span style="color:#CC0066; font-weight:bold;">loop</span><span style="color:#006600; font-weight:bold;">&#40;</span>1..10<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> x
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p><a href="http://www.khelll.com/blog/ruby/ruby-and-internal-dsls/">Internal DSLs</a> is also one of the nice things that you can achieve with block syntax in Ruby.</p>
<h3>Currying and Partial Functions</h3>
<p>Higher-order functions enable <strong>Currying</strong>, which 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. A direct use of currying is the <strong>Partial Functions</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 this concept. check the following example:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># defining a proc that sums 2 numbers</span>
plus = <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>a,b<span style="color:#006600; font-weight:bold;">|</span> a <span style="color:#006600; font-weight:bold;">+</span> b<span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#008000; font-style:italic;">#=&gt; #&lt;Proc:0x8e236ac@(irb):1 (lambda)&gt; </span>
&nbsp;
<span style="color:#008000; font-style:italic;"># notice the new call to procs in ruby 1.9 using the period symbol</span>
plus.<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">3</span>,<span style="color:#006666;">5</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;">#=&gt; 8 </span>
&nbsp;
<span style="color:#008000; font-style:italic;"># i want a modified version of the above proc that fills the first parameter with the value '1'</span>
<span style="color:#008000; font-style:italic;"># so i generate the currying then i use a partial function by supplying the first parameter</span>
<span style="color:#008000; font-style:italic;"># with value '1'</span>
plus_one = plus.<span style="color:#9900CC;">curry</span>.<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;">#=&gt; #&lt;Proc:0x8dde908&gt;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># I can use the new proc as normal</span>
plus_one.<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">5</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;">#=&gt; 6</span></pre></div></div>

<p>For extended example about currying, check <a href="http://www.khelll.com/blog/ruby/ruby-currying/">Ruby Currying</a> blog post by me <img src='http://www.khelll.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  . </p>
<h2>Type systems and pattern matching</h2>
<p>Pattern matching allows complicated control decisions based on data structure to be expressed in a concise manner. in simpler words, it&#8217;s a data structure based matching. Pure functional languages should support this concept, however and afaik this is still not supported in Ruby, but there are several trials to add this concept. check this <a href="http://etorreborre.blogspot.com/2007/04/pattern-matching-with-ruby.html">blog post</a> by Eric Torreborre.</p>
<h2>Strict versus lazy evaluation</h2>
<p>Strict evaluation always fully evaluates function arguments before invoking the function. Lazy evaluation does not evaluate function arguments unless their values are required to be evaluated. One use of Lazy evaluation is the performance increases due to avoiding unnecessary calculations.</p>
<p>However as the following example shows, Ruby use Strict evaluation strategy:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">print</span> length<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">+</span><span style="color:#006666;">1</span>, <span style="color:#006666;">3</span><span style="color:#006600; font-weight:bold;">*</span><span style="color:#006666;">2</span>, <span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">/</span><span style="color:#006666;">0</span>, <span style="color:#006666;">5</span><span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">4</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span>ZeroDivisionError: divided by <span style="color:#006666;">0</span></pre></div></div>

<p>The third parameter of the passed array contains a division by zero operation and as Ruby is doing strict evaluation, the above snippet of code will raise an exception.</p>
<h2>Recursion</h2>
<p>Iteration (looping) in functional languages is usually accomplished via recursion. Ruby doesn&#8217;t force you to recursion but it allows you to do so. However following recursion style has it&#8217;s own tax: Performance.<br />
Ruby 1.9 comes with some &#8220;tail call optimizations&#8221;, more <a href="http://www.reddit.com/r/ruby/comments/8lz33/ruby_19_has_tail_call_optimization">here</a>.</p>
<h2>Conclusion</h2>
<p>1- As you can tell, Ruby helps you write in functional style but it doesn&#8217;t force you to it.<br />
2- Writing in functional style enhances your code and makes it more self documented. Actually it will make it more thread-safe also.<br />
3- The main support for FB in ruby comes from the use of blocks and lambdas, also from the fact that everything is evaluated as an expression.<br />
4- Ruby still lack an important aspect of FP: Pattern Matching and Lazy Evaluation.<br />
5- There should be more work on tail recursion optimization, to encourage developers to use recursion.<br />
6- Any other thoughts?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.khelll.com/blog/ruby/ruby-and-functional-programming/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>
