<?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>Yellow™ Lab Blog &#187; proc</title>
	<atom:link href="http://yellowlab.com.au/blog/tag/proc/feed/" rel="self" type="application/rss+xml" />
	<link>http://yellowlab.com.au/blog</link>
	<description></description>
	<lastBuildDate>Tue, 15 Sep 2009 00:25:50 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Functional Programming in Ruby</title>
		<link>http://yellowlab.com.au/blog/2009/09/15/functional-programming-in-ruby/</link>
		<comments>http://yellowlab.com.au/blog/2009/09/15/functional-programming-in-ruby/#comments</comments>
		<pubDate>Tue, 15 Sep 2009 00:25:50 +0000</pubDate>
		<dc:creator>jeremy.mawson</dc:creator>
				<category><![CDATA[Dev]]></category>
		<category><![CDATA[block]]></category>
		<category><![CDATA[functional programming]]></category>
		<category><![CDATA[lambda]]></category>
		<category><![CDATA[proc]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://yellowlab.com.au/blog/?p=134</guid>
		<description><![CDATA[After many years of software development in the imperative style under my belt, I have in recent years developed a keen interest in functional programming as an alternative paradigm. First via the confusion of Functional Java, then Scala and now some forays into Haskell. Having experienced first hand the improvement in legibility and comprehension the [...]]]></description>
			<content:encoded><![CDATA[<div><span style="background-color: #ffffff;">After many years of software development in the imperative style under my belt, I have in recent years developed a keen interest in functional programming as an alternative paradigm. First via the confusion of <a href="http://functionaljava.org/" target="_blank">Functional Java</a>, then <a href="http://www.scala-lang.org/" target="_blank">Scala</a> and now some forays into <a href="http://www.haskell.org/" target="_blank">Haskell</a>. Having experienced first hand the improvement in legibility and comprehension the functional style can bring, I naturally wondered how FP could be applied to our Ruby work on Yellow Lab.</span></div>
<div><span style="background-color: #ffffff;"><br />
</span></div>
<div><span style="background-color: #ffffff;">Ruby&#8217;s strength is in its loose typing and imperative style. I like to think of it as the new and improved Perl. A rapid prototyping and scripting language for the modern developer and technical SME alike. Whilst it lacks the rigour of a purely functional language (as all imperative languages do) it does have functional capabilities.</span></div>
<div><span style="background-color: #ffffff;"><br />
</span></div>
<div><span style="background-color: #ffffff;">The essence of the functional style is the application of functions instead of variable mutation. It follows that functional languages must support function expressions for there to be any prospect of applying the consistent behaviour over an enumeration. Ruby supports three types of function expressions:</span></div>
<div><span style="background-color: #ffffff;"><br />
</span></div>
<h3><span style="background-color: #ffffff;">Blocks</span></h3>
<div><span style="background-color: #ffffff;">Anyone who has used <em>collect</em> or<em> inject</em> on a map knows what a block is. They are anonymous function expressions which are applied in a method when the <em>yield</em> keyword is encountered.</span></div>
<p><span style="background-color: #ffffff;"><br />
<code>fruits.each {|fruit| eat fruit } # delicious!</code></span></p>
<div><span style="background-color: #ffffff;">In this example the brackets denote the block, the pipes delimit the input parameter list and the remainder is the block body to be executed. Blocks cannot be assigned to a variable so their reuse is limited. One of the Yellow Lab developers has written <a href="http://www.skorks.com/2009/09/using-ruby-blocks-and-rolling-your-own-iterators/" target="_blank">more about Ruby blocks</a> on his blog.</span></div>
<div><span style="background-color: #ffffff;"><br />
</span></div>
<h3><span style="background-color: #ffffff;">Procs</span></h3>
<div><span style="background-color: #ffffff;">Procs are references to blocks. The are constructed explicitly with <strong>Proc</strong>.<em>new</em> or implicitly when ampersand prefixed method parameters receive anonymous blocks. They are invoked with a call to <em>call</em>.</span></div>
<p><span style="background-color: #ffffff;"><br />
<code>def do_something_with_fruit(fruit, &amp;fruit_related_activity)<br />
</code> <code>fruit_related_activity.call(fruit)<br />
end</code></span></p>
<p><code> </code></p>
<p><code>do_something_with_fruit(banana) {|fruit| eat fruit} # =&gt; the banana is eaten!</code></p>
<div><span style="background-color: #ffffff;">On first sight they seem fit for purpose. But they have some strong limitations.</span></div>
<div><span style="background-color: #ffffff;"><br />
</span></div>
<div><span style="background-color: #ffffff;">1. Flow control statements, such as return and break, will affect the flow in the invoking method, not just in the proc.</span></div>
<div><span style="background-color: #ffffff;"><br />
</span></div>
<div><span style="background-color: #ffffff;">2. They can happily be invoked with an incorrect numbers of parameters. Some may think this is a benefit, but I believe it is an open invitation for bugs.</span></div>
<div><span style="background-color: #ffffff;"><br />
</span></div>
<h3>Lambdas</h3>
<div><span style="background-color: #ffffff;">Lambdas are akin to Procs, but correct the two limitations listed above. That is, flow control statements take effect within the scope of the lambda only and the number of parameters must be correct. They are the best fit for reusable references to function expressions.</span></div>
<div><span style="background-color: #ffffff;"><br />
</span></div>
<p><code>blend = lambda {|fruit| blender.blend(fruit)}<br />
freeze = lambda {|fruit| freezer &lt;&lt; fruit}</code></p>
<p><code> </code></p>
<p><code>basketOfFruit.each &amp;blend # => a smoothie<br />
basketOfFruit.each &amp;freeze # => now icecream<br />
canOfWorms.each &amp;freeze # => um ...</code></p>
<div><span style="background-color: #ffffff;"><br />
</span></div>
<h3>Utility</h3>
<div><span style="background-color: #ffffff;">Our application uses the Rails framework and has many of the artefacts you&#8217;d expect to see in a Rails application: controllers, models, views, some rake and capistrano tasks. Much of it is elegant and simple imperative style code. Blocks are used frequently within Labs to simplify the application of behaviour across an enumerable &#8211; especially in rake and capistrano tasks that prepare our data for runtime. There seems little necessity in this kind of application for the inclusion of lambdas. (I expect this would be quite a different situation if we were building a framework instead).</div>
<div><span style="background-color: #ffffff;"><br />
</span></div>
<div><span style="background-color: #ffffff;">Ruby is far from a functional language. No one could possibly argue otherwise. The lack of typing and immutability ensure that it is too tempting to revert to a less rigid style. But, as this post has shown, there are some functional aspects of the language and it is certainly worth keeping these in the toolbox.</div>
]]></content:encoded>
			<wfw:commentRss>http://yellowlab.com.au/blog/2009/09/15/functional-programming-in-ruby/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
