<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Matt Stine&#039;s Blog &#187; functional-programming</title>
	<atom:link href="http://mattstine.com/category/functional-programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://mattstine.com</link>
	<description>Thoughts on Java, Groovy, Grails, Agile Development, etc. etc. etc.</description>
	<lastBuildDate>Tue, 17 May 2011 17:02:53 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='mattstine.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Matt Stine&#039;s Blog &#187; functional-programming</title>
		<link>http://mattstine.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://mattstine.com/osd.xml" title="Matt Stine&#039;s Blog" />
	<atom:link rel='hub' href='http://mattstine.com/?pushpress=hub'/>
		<item>
		<title>Making Java &#8220;Groovier&#8221; with LambdaJ</title>
		<link>http://mattstine.com/2009/10/09/making-java-groovier-with-lambdaj/</link>
		<comments>http://mattstine.com/2009/10/09/making-java-groovier-with-lambdaj/#comments</comments>
		<pubDate>Fri, 09 Oct 2009 14:37:06 +0000</pubDate>
		<dc:creator>mattstine</dc:creator>
				<category><![CDATA[functional-programming]]></category>
		<category><![CDATA[groovy]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[loty]]></category>

		<guid isPermaLink="false">http://www.mattstine.com/?p=257</guid>
		<description><![CDATA[I spent the better part of yesterday tracing my way through the codebase for a large-scale enterprise application that my team is building right now, and I happened upon the following piece of code: As I read this code, I thought &#8220;This just SCREAMS a need for Groovy&#8217;s closure iterators.&#8221; So, last night I quickly [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mattstine.com&amp;blog=58954&amp;post=257&amp;subd=mattstine&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I spent the better part of yesterday tracing my way through the codebase for a large-scale enterprise application that my team is building right now, and I happened upon the following piece of code:</p>
<p><pre class="brush: java;">
//...imports excluded for clarity
public class BusinessActivityBinMetaClassHelper {
//...
   public static List&lt;Long&gt; getSrmMetaClassIdListJava(List&lt;BusinessActivityBinMetaClass&gt; businessActivityBinMetaClassList) {
      List&lt;Long&gt; srmMetaClassIdList = new ArrayList&lt;Long?();

      if(businessActivityBinMetaClassList != null) {
         for(BusinessActivityBinMetaClass businessActivityBinMetaClass : businessActivityBinMetaClassList) {
             if(businessActivityBinMetaClass.getSrmMetaClass() != null &amp;&amp; businessActivityBinMetaClass.getSrmMetaClass().getSrmMetaClassId() != null) {
               srmMetaClassIdList.add
                  (businessActivityBinMetaClass.getSrmMetaClass().getSrmMetaClassId());
            }
         }
      }

      return srmMetaClassIdList;
   }
//...
}
</pre></p>
<p>As I read this code, I thought &#8220;This just SCREAMS a need for Groovy&#8217;s closure iterators.&#8221; So, last night I quickly hacked out fully-equivalent Groovy version of the code:</p>
<p><pre class="brush: groovy;">
class GroovyExample {

   static def getSrmMetaClassIdListGroovy(def businessActivityBinMetaClassList) {
      businessActivityBinMetaClassList?.collect { it?.srmMetaClass?.srmMetaClassId }.findAll { it != null } ?: new ArrayList&lt;Long&gt;();
   }
}
</pre></p>
<p>Whew! Much nicer. What did we get out of this? Well&#8230;</p>
<ul>
<li>Groovy&#8217;s dynamic typing cleaned up all of the unnecessary type declarations &#8211; the only static typing is where we return an empty ArrayList of Longs if the argument to our method is null (a bit of odd behavior, but required to make this code equivalent to the parent Java code.</li>
<li>We were saved 4 painful null checks by the use of Groovy&#8217;s null safe dereferencing operator (?.) and the simplified ternary operator otherwise affectionately known as the &#8220;Elvis Operator&#8221; (?:).</li>
<li>Using Groovy&#8217;s collect method, we were able to transform the original list into a list containing Long values by passing in a closure that pulls out the property value of interest.</li>
<li>Because we&#8217;re using null safe dereferencing, we are actually inserting nulls into our list if any of the dereferencing fails. Therefore, Groovy&#8217;s findAll Collection filtering method comes to the rescue. We simply provide it with a closure that returns true for all of the values we want to keep &#8211; in this case, &#8220;it != null.&#8221;</li>
<li>Perhaps most importantly, we&#8217;ve shorted our code from 11 lines (including whitespace lines for clarity) to ONE LINE that much more clearly expresses the intent of the code.</li>
</ul>
<p>Great &#8211; why don&#8217;t we just rewrite the entire application is Groovy? Well, hold on just a minute. At the time we started this application, as much as some of us loved Groovy, we just didn&#8217;t have enough Groovy mindshare to go there yet. On top of that, we were already experimenting with several new architectural ideas and technologies, and Groovy would have added yet one more risk to the puzzle. I say all this to acknowledge that sometimes you just can&#8217;t move to another language for one reason or another, regardless of how attractive its features may be.</p>
<p>But let&#8217;s take a queue from the <em>Pragmatic Programmer</em> and explore the LOTY (Language of the Year) concept one more time. One of the reasons that you&#8217;re encouraged to learn new languages is to change the way you program in your main language. You may learn Groovy, Scala, Clojure, Ruby, etc., etc., etc. and never use them in your day job &#8211; and yet the experience of coding in a new language with new constructs and idioms will necessarily change the way you THINK about programming in every other language.</p>
<p>So, let&#8217;s think about the possibility of coding something that is much more similar to the Groovy version and yet stick with regular Java code. Fortunately, there are several libraries out there that bring much of the flavor and power of Groovy&#8217;s closure iterators to Java. I&#8217;d like to focus in on one of them, LambdaJ (<a href="http://code.google.com/p/lambdaj/">http://code.google.com/p/lambdaj/</a>).</p>
<p>LambdaJ provides constructs that allow us to &#8220;&#8230;manipulate collections in a pseudo-functional and statically typed way.&#8221; Let&#8217;s take a look at this example implementing using LambdaJ:</p>
<p><pre class="brush: java;">
//...some imports excluded for clarity
import ch.lambdaj.function.convert.Converter
import static ch.lambdaj.Lambda.*
import static org.hamcrest.Matchers.*

public class BusinessActivityBinMetaClassHelper {
//...
   public static List&lt;Long&gt; getSrmMetaClassIdListJava(List&lt;BusinessActivityBinMetaClass&gt; businessActivityBinMetaClassList) {
      return (businessActivityBinMetaClassList != null) ? filter(notNullValue(),convert(businessActivityBinMetaClassList, new IdExtractor())) : new ArrayList&lt;Long&gt;();
   }

   class IdExtractor implements Converter&lt;BusinessActivityBinMetaClass,Long&gt; {
	Long convert(BusinessActivityBinMetaClass from) {
	   if (from.getSrmMetaClass() != null &amp;&amp; from.getSrmMetaClass().getSrmMetaClassId() != null) {
	      return from.getSrmMetaClass().getSrmMetaClassId();
	   }
   }
}
</pre></p>
<p>Not quite as nice as the Groovy code &#8211; we got A LOT out of the null-safe dereference and Elvis operators. However, our overall intent is still a bit clearer. Let&#8217;s analyze:</p>
<ul>
<li>First we need to implement one of LambdaJ&#8217;s Converters. A Converter is nothing more than a generic Interface that defines one method: T convert(F from), where F is the type we&#8217;re converting from and T is the type we&#8217;re converting to. In this case, we want to convert an object of type BusinessActivityBinMetaClass to an object of type Long. Our implementation determines how this conversion takes place, in this case by extracting the id property from its child.</li>
<li>Next, after statically importing the methods of ch.lambdaj.Lambda, we call the convert method, passing it our List and our newly implemented Converter. This gives us the equivalent of Groovy&#8217;s collect method, with the Converter taking the place of the closure.</li>
<li>We&#8217;re still shoving nulls into our List with this implementation, so we further filter our list using LambdaJ&#8217;s &#8220;filter&#8221; method, passing it the list returned by &#8220;filter,&#8221; and a <a href="http://code.google.com/p/hamcrest/">Hamcrest</a> matcher that returns only notNullValue()&#8217;s.</li>
<li>Finally, we use our old friend the ternary operator to return the empty ArrayList of Long values if our method argument is null.</li>
</ul>
<p>If you don&#8217;t count the Converter implementation, we&#8217;ve gotten ourselves down to 2 lines of code (1 if you don&#8217;t mind long lines). In this case I implemented IdExtractor as a named inner class &#8211; you could do this with an anonymous inner class and it would look a lot more like a closure, but the added noise of all of the null checking made the undesirable for me. Perhaps if your code has less noise (or guarantees that values aren&#8217;t null), that would be a better approach.</p>
<p>Another alternative is to make IdExtractor a top-level class that, if general enough, could be reused throughout the codebase. The benefit of this is that you now have a nice code unit rather than a battery of static methods in a utility class, and unit testing becomes much more clean and elegant.</p>
<p>So, we&#8217;ve still made some progress and made our code a bit more Groovy. I encourage you to explore LambdaJ&#8217;s feature set and see how it might make your code a bit more concise with clearer intent. And just to stir up a little controversy, look what would have happened in Java 7 had the null safe dereference and Elvis operator&#8217;s made <a href="http://blogs.sun.com/darcy/entry/project_coin_final_five">the Project Coin cut</a>:</p>
<p><pre class="brush: java;">
//...some imports excluded for clarity
import ch.lambdaj.function.convert.Converter
import static ch.lambdaj.Lambda.*
import static org.hamcrest.Matchers.*

public class BusinessActivityBinMetaClassHelper {
//...
   public static List&lt;Long&gt; getSrmMetaClassIdListLambdaJ(List&lt;BusinessActivityBinMetaClass&gt; businessActivityBinMetaClassList) {
      return filter(notNullValue(),convert(businessActivityBinMetaClassList,
         new Converter&lt;BusinessActivityBinMetaClass,Long&gt; {
            Long apply(BusinessActivityBinMetaClass from) { return from?.getSrmMetaClass()?.getSrmMetaClassId()}
         })) ?: new ArrayList&lt;Long&gt;();
   }
}
</pre></p>
<p>Nice, huh? <img src='http://s1.wp.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mattstine.wordpress.com/257/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mattstine.wordpress.com/257/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mattstine.wordpress.com/257/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mattstine.wordpress.com/257/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mattstine.wordpress.com/257/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mattstine.wordpress.com/257/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mattstine.wordpress.com/257/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mattstine.wordpress.com/257/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mattstine.wordpress.com/257/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mattstine.wordpress.com/257/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mattstine.wordpress.com/257/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mattstine.wordpress.com/257/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mattstine.wordpress.com/257/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mattstine.wordpress.com/257/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mattstine.com&amp;blog=58954&amp;post=257&amp;subd=mattstine&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://mattstine.com/2009/10/09/making-java-groovier-with-lambdaj/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1eaa45fee6a2b0c4b479b2982a4274f4?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mattstine</media:title>
		</media:content>
	</item>
		<item>
		<title>Functional programming blog aggregator in the works&#8230;</title>
		<link>http://mattstine.com/2009/04/30/functional-programming-blog-aggregator-in-the-works/</link>
		<comments>http://mattstine.com/2009/04/30/functional-programming-blog-aggregator-in-the-works/#comments</comments>
		<pubDate>Thu, 30 Apr 2009 18:31:58 +0000</pubDate>
		<dc:creator>mattstine</dc:creator>
				<category><![CDATA[clojure]]></category>
		<category><![CDATA[functional-programming]]></category>
		<category><![CDATA[goals]]></category>
		<category><![CDATA[loty]]></category>
		<category><![CDATA[scala]]></category>
		<category><![CDATA[toty]]></category>

		<guid isPermaLink="false">http://www.mattstine.com/?p=155</guid>
		<description><![CDATA[So I&#8217;m in the midst of planning my next toy project, specifically with an aim to build something for the cloud, be it Google App Engine, Mor.ph, EC2, etc. (that part is still up for grabs). What I&#8217;d like to do is an opt-in blog aggregation site focused around functional programming. It would be very [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mattstine.com&amp;blog=58954&amp;post=155&amp;subd=mattstine&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>So I&#8217;m in the midst of planning my next toy project, specifically with an aim to build something for the cloud, be it <a href="http://code.google.com/appengine">Google App Engine</a>, <a href="http://mor.ph">Mor.ph</a>, <a href="http://aws.amazon.com/ec2/">EC2</a>, etc. (that part is still up for grabs). What I&#8217;d like to do is an opt-in blog aggregation site focused around functional programming. It would be very similar to what <a href="http://blogs.bytecode.com.au/glen/">Glen Smith</a> has done with <a href="http://groovyblogs.org">groovyblogs.org</a>. Groovyblogs is currently my number one referral site and is generating about 20% of the traffic for this blog. I think it is a great way to find out what blogs are out there in a given space, and there doesn&#8217;t seem to be an equivalent for the functional space. Since my LOTY interest is focused in the functional space, particularly around Scala and Clojure, and my TOTY interest is the cloud technologies out there, it seems like the perfect marriage.</p>
<p>So here&#8217;s my question. Would you be interested in using such a site? As a reader, blogger, or both? Please sound off. Thanks!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mattstine.wordpress.com/155/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mattstine.wordpress.com/155/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mattstine.wordpress.com/155/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mattstine.wordpress.com/155/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mattstine.wordpress.com/155/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mattstine.wordpress.com/155/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mattstine.wordpress.com/155/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mattstine.wordpress.com/155/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mattstine.wordpress.com/155/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mattstine.wordpress.com/155/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mattstine.wordpress.com/155/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mattstine.wordpress.com/155/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mattstine.wordpress.com/155/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mattstine.wordpress.com/155/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mattstine.com&amp;blog=58954&amp;post=155&amp;subd=mattstine&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://mattstine.com/2009/04/30/functional-programming-blog-aggregator-in-the-works/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1eaa45fee6a2b0c4b479b2982a4274f4?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mattstine</media:title>
		</media:content>
	</item>
		<item>
		<title>Joel&#8217;s BuilderBuilder in Groovy</title>
		<link>http://mattstine.com/2009/04/27/joels-builderbuilder-in-groovy/</link>
		<comments>http://mattstine.com/2009/04/27/joels-builderbuilder-in-groovy/#comments</comments>
		<pubDate>Tue, 28 Apr 2009 02:54:07 +0000</pubDate>
		<dc:creator>mattstine</dc:creator>
				<category><![CDATA[functional-programming]]></category>
		<category><![CDATA[groovy]]></category>

		<guid isPermaLink="false">http://www.mattstine.com/?p=149</guid>
		<description><![CDATA[Joel Neely started a series of posts over the weekend detailing a proposed exploration of what Functional Programming means &#8220;to me as a practicing OO software developer?&#8221; The task at hand is to look at the generation of Data Transfer Objects which include a static inner class that functions as a builder. Since I&#8217;m exploring [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mattstine.com&amp;blog=58954&amp;post=149&amp;subd=mattstine&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://joelneely.wordpress.com/">Joel Neely</a> started <a href="http://joelneely.wordpress.com/2009/04/25/builderbuilder-the-agenda/">a series</a> <a href="http://joelneely.wordpress.com/2009/04/27/builderbuilder-the-task/">of posts</a> over the weekend detailing a proposed exploration of what Functional Programming means &#8220;to me as a practicing OO software developer?&#8221; The task at hand is to look at the generation of Data Transfer Objects which include a static inner class that functions as a builder. Since I&#8217;m exploring both Clojure and Scala right now, Joel has asked me to play along.</p>
<p>After reading the task definition today, I couldn&#8217;t resist taking a crack at the problem in Groovy, especially since tackling a Builder DSL is not something I&#8217;ve gone after before.</p>
<p>So, here&#8217;s how I&#8217;ll use my DTOBuilder to produce the output Joel described:</p>
<pre class="brush: groovy">
def bldr = new DtoBuilder()

println bldr.build {
	packageName 'edu.bogusu.registration'
	name 'Student'
	field(name:'id', type:'String')
	field(name:'firstName', type:'String')
	field(name:'lastName', type:'String')
	field(name:'hoursEarned', type:'int')
	field(name:'gpa', type:'float')
}
</pre>
<p>Pretty concise, eh? <a href="http://github.com/mstine/BuilderBuilder/blob/1008235f88177910eb94af165ade3aedae2955a3/src/DtoBuilder.groovy">Check out the implementation of it at GitHub</a>. I&#8217;ll be posting all of my code related to this series at this location (and hopefully Joel will join in as well).</p>
<p>One thing you&#8217;ll notice is that I&#8217;m still operating primarily in OO style. My next task is to refactor this code, still written in Groovy, but using as much functional-style as I can squeeze out of the language. Until next time&#8230;</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mattstine.wordpress.com/149/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mattstine.wordpress.com/149/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mattstine.wordpress.com/149/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mattstine.wordpress.com/149/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mattstine.wordpress.com/149/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mattstine.wordpress.com/149/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mattstine.wordpress.com/149/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mattstine.wordpress.com/149/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mattstine.wordpress.com/149/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mattstine.wordpress.com/149/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mattstine.wordpress.com/149/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mattstine.wordpress.com/149/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mattstine.wordpress.com/149/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mattstine.wordpress.com/149/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mattstine.com&amp;blog=58954&amp;post=149&amp;subd=mattstine&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://mattstine.com/2009/04/27/joels-builderbuilder-in-groovy/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1eaa45fee6a2b0c4b479b2982a4274f4?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mattstine</media:title>
		</media:content>
	</item>
	</channel>
</rss>
