<?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>JCranky&#039;s Blog!</title>
	<atom:link href="http://jcranky.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://jcranky.com</link>
	<description>Java, Scala, Agile and stuff, by JCranky =)</description>
	<lastBuildDate>Mon, 19 Dec 2011 18:50:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='jcranky.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://0.gravatar.com/blavatar/a2edfd1ac837f723e9b475c8eb5a5bbd?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>JCranky&#039;s Blog!</title>
		<link>http://jcranky.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://jcranky.com/osd.xml" title="JCranky&#039;s Blog!" />
	<atom:link rel='hub' href='http://jcranky.com/?pushpress=hub'/>
		<item>
		<title>Matching a combination of class and trait in Scala</title>
		<link>http://jcranky.com/2011/12/19/matching-a-combination-of-class-and-trait-in-scala/</link>
		<comments>http://jcranky.com/2011/12/19/matching-a-combination-of-class-and-trait-in-scala/#comments</comments>
		<pubDate>Mon, 19 Dec 2011 18:43:57 +0000</pubDate>
		<dc:creator>Paulo "JCranky" Siqueira</dc:creator>
				<category><![CDATA[scala]]></category>
		<category><![CDATA[AnyRef]]></category>
		<category><![CDATA[case]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[match]]></category>
		<category><![CDATA[pattern matching]]></category>
		<category><![CDATA[trait]]></category>
		<category><![CDATA[with]]></category>

		<guid isPermaLink="false">http://jcranky.com/?p=952</guid>
		<description><![CDATA[While doing some development with Scala I discovered that it is possible to match combinations of a class and traits. It might seem obvious if you already know it, but I didn&#8217;t. What happened is that I needed something like matching that kind of combination&#8230; and this speaks to something about Scala that I love: [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jcranky.com&amp;blog=4406641&amp;post=952&amp;subd=jcranky&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>While doing some development with Scala I discovered that it is possible to match combinations of a class and traits. It might seem obvious if you already know it, but I didn&#8217;t.</p>
<p>What happened is that I needed something like matching that kind of combination&#8230; and this speaks to something about Scala that I love: it becomes intuitive after a while. So how did I find that such matching is possible? I simply tried&#8230; =)</p>
<p>This is what this feature looks like:</p>
<p><pre class="brush: scala;">
msg match {
  case x: X with Z =&gt; println(&quot;matching combination!&quot;)
}
</pre></p>
<p>In this case, X would be a class, and Z a trait. The <em>case x</em> will be triggered only if <em>msg</em> is an instance of <em>class X</em> and mixes in <em>trait Z</em>. With this, we make sure to have access to all members both from the class and from the trait, while being really type safe about the matching. No unsupported combinations will pass through, and no need for castings, like would be necessary in Java.</p>
<p>Lets try a dummy example. Imagine we have a <em>Superman</em> class, and two traits: <em>BlueCape</em> and <em>RedCape</em>, like the following code:</p>
<p><pre class="brush: scala;">
class Superman(val realname: String)
trait BlueCape
trait RedCape
</pre></p>
<p>Now, we want to match on a Superman instance, but we want to know if it is fake or not. Finally, we also want to print the guy&#8217;s name, which means we need access to the <em>realname</em> field. This is how you could do that:</p>
<p><pre class="brush: scala;">
def whichSuperman(superman: AnyRef) = superman match {
  case s: Superman with RedCape =&gt; println(&quot;real superman: &quot; + s.realname)
  case f: Superman with BlueCape =&gt; println(&quot;fake superman: &quot; + f.realname)
}
</pre></p>
<p>Flexible and type safe! And if the traits had any field, we would be able to access them as well. Quick sample test code:</p>
<p><pre class="brush: scala;">
val mys = new Superman(&quot;clark&quot;) with RedCape
val myf = new Superman(&quot;clurk&quot;) with BlueCape

whichSuperman(mys)
whichSuperman(myf)
</pre></p>
<p>The part that matches the class is not very useful in this example, since this could be defined in the function parameter, but it was in my local scenario, where we are using actors and the messages can be <em>AnyRef</em>.</p>
<p>If you want to know a bit more about pattern matching, <a title="Scala Pattern Matching" href="http://jcranky.com/2010/04/29/scala-pattern-matching-the-reason-to-fall-in-love-with-scala/" target="_blank">I wrote about it a while ago here</a>. I also described <a title="Matching a range of numbers" href="http://jcranky.com/2011/03/17/matching-a-range-of-numbers-in-scala/" target="_blank">how to match a range of numbers here</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jcranky.wordpress.com/952/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jcranky.wordpress.com/952/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jcranky.wordpress.com/952/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jcranky.wordpress.com/952/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jcranky.wordpress.com/952/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jcranky.wordpress.com/952/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jcranky.wordpress.com/952/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jcranky.wordpress.com/952/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jcranky.wordpress.com/952/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jcranky.wordpress.com/952/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jcranky.wordpress.com/952/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jcranky.wordpress.com/952/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jcranky.wordpress.com/952/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jcranky.wordpress.com/952/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jcranky.com&amp;blog=4406641&amp;post=952&amp;subd=jcranky&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jcranky.com/2011/12/19/matching-a-combination-of-class-and-trait-in-scala/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3ebbb64cafa4d784a6f09a1886eaa5e7?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">paulosiqueira</media:title>
		</media:content>
	</item>
		<item>
		<title>scaladores first meeting</title>
		<link>http://jcranky.com/2011/11/08/scaladores-first-meeting/</link>
		<comments>http://jcranky.com/2011/11/08/scaladores-first-meeting/#comments</comments>
		<pubDate>Tue, 08 Nov 2011 13:24:00 +0000</pubDate>
		<dc:creator>Paulo "JCranky" Siqueira</dc:creator>
				<category><![CDATA[scala]]></category>
		<category><![CDATA[meeting]]></category>
		<category><![CDATA[sao paulo]]></category>
		<category><![CDATA[scaladores]]></category>

		<guid isPermaLink="false">http://jcranky.com/?p=948</guid>
		<description><![CDATA[Right to the point: we are starting a Scala user group down  here in São Paulo, called Scaladores. The idea is to for people interested in the Scala language to meet in person and talk about it. If you are in the region, please drop by &#8211; the first meeting happens this thurday, at 7pm. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jcranky.com&amp;blog=4406641&amp;post=948&amp;subd=jcranky&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Right to the point: we are starting a Scala user group down  here in São Paulo, called <em>Scaladores</em>. The idea is to for people interested in the Scala language to meet in person and talk about it. If you are in the region, please drop by &#8211; the first meeting happens this thurday, at 7pm. The portuguese page with <a title="Scaladores first meeting" href="http://scaladores.com.br/2011/11/07/1a-reuniao-scaladores-10-de-novembro-de-2011-na-adaptworks/" target="_blank">more information can be found here</a>.</p>
<p>I&#8217;ll come back after the meeting for a &#8216;report&#8217; of how it went =)</p>
<p>In time: I should write more posts about scala soon, just need to get some stuff in place before that.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jcranky.wordpress.com/948/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jcranky.wordpress.com/948/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jcranky.wordpress.com/948/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jcranky.wordpress.com/948/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jcranky.wordpress.com/948/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jcranky.wordpress.com/948/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jcranky.wordpress.com/948/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jcranky.wordpress.com/948/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jcranky.wordpress.com/948/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jcranky.wordpress.com/948/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jcranky.wordpress.com/948/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jcranky.wordpress.com/948/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jcranky.wordpress.com/948/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jcranky.wordpress.com/948/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jcranky.com&amp;blog=4406641&amp;post=948&amp;subd=jcranky&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jcranky.com/2011/11/08/scaladores-first-meeting/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3ebbb64cafa4d784a6f09a1886eaa5e7?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">paulosiqueira</media:title>
		</media:content>
	</item>
		<item>
		<title>Acidental scala upgrade with sbaz</title>
		<link>http://jcranky.com/2011/09/19/acidental-scala-upgrade-with-sbaz/</link>
		<comments>http://jcranky.com/2011/09/19/acidental-scala-upgrade-with-sbaz/#comments</comments>
		<pubDate>Mon, 19 Sep 2011 22:04:40 +0000</pubDate>
		<dc:creator>Paulo "JCranky" Siqueira</dc:creator>
				<category><![CDATA[scala]]></category>
		<category><![CDATA[netbeans]]></category>
		<category><![CDATA[sbaz]]></category>
		<category><![CDATA[upgrade]]></category>
		<category><![CDATA[package manager]]></category>
		<category><![CDATA[scala-library]]></category>

		<guid isPermaLink="false">http://jcranky.com/?p=942</guid>
		<description><![CDATA[And then I was playing around with the scala tools that come in the installation&#8217;s bin folder. There is something there called sbaz, and this name sounds so different that I just HAD to play with it to find out what it does. Lets first explain what I discovered later: what sbaz does. It is [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jcranky.com&amp;blog=4406641&amp;post=942&amp;subd=jcranky&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>And then I was playing around with the <a title="Scala Language" href="http://scala-lang.org" target="_blank">scala</a> tools that come in the installation&#8217;s bin folder. There is something there called <em>sbaz</em>, and this name sounds so different that I just HAD to play with it to find out what it does.</p>
<p>Lets first explain what I discovered later: what <em>sbaz</em> does. It is some kind of scala package manager for your local scala installation. If you issue a <em>sbaz available</em>, it will list all packages it is able to install for you, for example. Also, and this was the somewhat tricky part for me, it is able to upgrade all current packages to their latest versions, including the main scala stuff, like the scala-library.</p>
<p>This is all great.. as long as you don&#8217;t run <em>sbaz upgrade</em> by mistake. This happened some weeks ago to me&#8230; And I was lucky enough to not have any major problem. I was running scala 2.8.0 at the time, and 2.9.0 was already out. Result: I upgraded to scala 2.9.0 before I intended to&#8230; and one day before a speech I gave at a conference, where I presented plenty of scala code examples.</p>
<p>In the end all went well, but I had to update some sample code, and to upgrade the Netbeans scala plugin to the version that supports 2.9.0. The other option would be to erase the installation folder and install scala from scratch again, but I chose to use this &#8220;event&#8221; to upgrade to 2.9.0 for real =)</p>
<p>Bottom line: learn that scala has a very nice package manager tool called <em>sbaz</em>, but use it with care <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/jcranky.wordpress.com/942/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jcranky.wordpress.com/942/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jcranky.wordpress.com/942/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jcranky.wordpress.com/942/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jcranky.wordpress.com/942/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jcranky.wordpress.com/942/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jcranky.wordpress.com/942/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jcranky.wordpress.com/942/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jcranky.wordpress.com/942/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jcranky.wordpress.com/942/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jcranky.wordpress.com/942/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jcranky.wordpress.com/942/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jcranky.wordpress.com/942/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jcranky.wordpress.com/942/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jcranky.com&amp;blog=4406641&amp;post=942&amp;subd=jcranky&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jcranky.com/2011/09/19/acidental-scala-upgrade-with-sbaz/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3ebbb64cafa4d784a6f09a1886eaa5e7?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">paulosiqueira</media:title>
		</media:content>
	</item>
		<item>
		<title>Scala&#8217;s Parallel Collections are useful even in the most simple cases</title>
		<link>http://jcranky.com/2011/08/26/scalas-parallel-collections-are-useful-even-in-the-most-simple-cases/</link>
		<comments>http://jcranky.com/2011/08/26/scalas-parallel-collections-are-useful-even-in-the-most-simple-cases/#comments</comments>
		<pubDate>Fri, 26 Aug 2011 23:58:09 +0000</pubDate>
		<dc:creator>Paulo "JCranky" Siqueira</dc:creator>
				<category><![CDATA[scala]]></category>
		<category><![CDATA[amazon]]></category>
		<category><![CDATA[amazon s3]]></category>
		<category><![CDATA[collections]]></category>
		<category><![CDATA[foreach]]></category>
		<category><![CDATA[par]]></category>
		<category><![CDATA[parallel collections]]></category>
		<category><![CDATA[s3]]></category>

		<guid isPermaLink="false">http://jcranky.com/?p=928</guid>
		<description><![CDATA[One of the main features that Scala version 2.9.x brought was Parallel Collections. If you think about it quickly, you might consider using such a feature only in complex, process intensive scenarios. Although those are certainly the main places where you would consider using parallel collections, there are more simpler ways you can leverage them. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jcranky.com&amp;blog=4406641&amp;post=928&amp;subd=jcranky&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>One of the main features that Scala version 2.9.x brought was Parallel Collections. If you think about it quickly, you might consider using such a feature only in complex, process intensive scenarios.</p>
<p>Although those are certainly the main places where you would consider using parallel collections, there are more simpler ways you can leverage them. The code to use this feature is so simple that it&#8217;s easy to come up with reasons to use it everywhere:</p>
<p><pre class="brush: scala;">
myCol.par foreach(println)
</pre></p>
<p>Now how could that be easier??</p>
<p>So where did I use that for real, you might ask. The current company I work on stores files in Amazon S3. Before that, those files where stored in the local disk. So, when we migrated to S3, I wrote a script, in Scala, to upload all the files&#8230; thousands of them. My first solution looked something like this:</p>
<p><pre class="brush: scala;">
listOfFiles foreach(file =&gt; sendToS3(file))
</pre></p>
<p>And then suddenly it occurred me that it&#8217;s ridiculously simple to parallelize the upload of the files:</p>
<p><pre class="brush: scala;">
listOfFiles.par foreach(file =&gt; sendToS3(file))
</pre></p>
<p>Result: half the time for uploading the files, having to do almost nothing! Pretty neat, ain&#8217;t it?</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jcranky.wordpress.com/928/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jcranky.wordpress.com/928/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jcranky.wordpress.com/928/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jcranky.wordpress.com/928/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jcranky.wordpress.com/928/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jcranky.wordpress.com/928/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jcranky.wordpress.com/928/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jcranky.wordpress.com/928/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jcranky.wordpress.com/928/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jcranky.wordpress.com/928/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jcranky.wordpress.com/928/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jcranky.wordpress.com/928/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jcranky.wordpress.com/928/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jcranky.wordpress.com/928/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jcranky.com&amp;blog=4406641&amp;post=928&amp;subd=jcranky&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jcranky.com/2011/08/26/scalas-parallel-collections-are-useful-even-in-the-most-simple-cases/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3ebbb64cafa4d784a6f09a1886eaa5e7?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">paulosiqueira</media:title>
		</media:content>
	</item>
		<item>
		<title>Scala Problem Number Six</title>
		<link>http://jcranky.com/2011/08/21/scala-problem-number-six/</link>
		<comments>http://jcranky.com/2011/08/21/scala-problem-number-six/#comments</comments>
		<pubDate>Sun, 21 Aug 2011 04:40:53 +0000</pubDate>
		<dc:creator>Paulo "JCranky" Siqueira</dc:creator>
				<category><![CDATA[scala]]></category>
		<category><![CDATA[list]]></category>
		<category><![CDATA[list reverse]]></category>
		<category><![CDATA[list.head]]></category>
		<category><![CDATA[list.last]]></category>
		<category><![CDATA[palindrome]]></category>
		<category><![CDATA[s-99]]></category>
		<category><![CDATA[scala 99 problems]]></category>

		<guid isPermaLink="false">http://jcranky.com/?p=869</guid>
		<description><![CDATA[From the Scala 99 Problems, lets face the problem number six: P06 (*) Find out whether a list is a palindrome. Example: scala&#62; isPalindrome(List(1, 2, 3, 2, 1)) res0: Boolean = true The first thing I had to do was to find out the meaning of palindrome. From the example, it&#8217;s easy to guess, but [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jcranky.com&amp;blog=4406641&amp;post=869&amp;subd=jcranky&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>From the <a title="Scala 99 Problems" href="http://aperiodic.net/phil/scala/s-99/" target="_blank">Scala 99 Problems</a>, lets face the problem number six:</p>
<dl>
<dt><strong>P06 (*) Find out whether a list is a palindrome.</strong></dt>
<dd>Example:</p>
<pre>scala&gt; isPalindrome(List(1, 2, 3, 2, 1))
res0: Boolean = true</pre>
</dd>
</dl>
<p>The first thing I had to do was to find out the meaning of <em>palindrome</em>. From the example, it&#8217;s easy to guess, but I prefer to be sure. So, <a title="Palindrome at Dictionary.com" href="http://dictionary.reference.com/browse/palindrome" target="_blank">from dictionary.com</a>:</p>
<div><em>1. a word, line, verse, number, sentence, etc., reading the same backward as forward, as Madam, I&#8217;m Adam  or Poor Dan is in a droop.</em></div>
<p>Interesting, so this is valid not only for numbers =)</p>
<p>Now, to some coding! My first solution:</p>
<p><pre class="brush: scala;">
def isPalindrome(list: List[Any]): Boolean = list match {
  case Nil =&gt; true
  case head :: Nil =&gt; true
  case head :: rest if head == rest.last =&gt; isPalindrome(rest.dropRight(1))
  case _ =&gt; false
}
</pre></p>
<p>This first solution uses pattern matching to investigate the list recursively. It checks if the list is empty or if it contains only one element and returns true. Otherwise, it checks if the head of the list equals the last element. If so, the rest of the list (its middle part) is passed recursively to the function. This is one more case where we can see the great power of scala&#8217;s pattern matching in action.</p>
<p>Also, the solution will work with strings as well, so it matches the dictionary definition &#8211; just call <em>toList</em> on the string. Sample execution from the <em>REPL</em>:</p>
<p><pre class="brush: scala;">
scala&gt; isPalindrome(&quot;oio&quot;.toList)
res11: Boolean = true

scala&gt; isPalindrome(&quot;oioo&quot;.toList)
res12: Boolean = false
</pre></p>
<p>To be honest, I don&#8217;t feel too much creative when thinking about this problem, so the next solution is quite similar to the previous one, but without pattern matching:</p>
<p><pre class="brush: scala;">
def isPalindrome(list: List[Any]): Boolean = {
  if (list.isEmpty || list.size == 1) true
  else if (list.head == list.last) isPalindrome(list.slice(1, list.size-1))
  else false
}
</pre></p>
<p>Now lets take a look at the <a title="Scala Problem Number Six - Solution" href="http://aperiodic.net/phil/scala/s-99/p06.scala" target="_blank">original solution</a> and&#8230; I&#8217;m stunned by its simplicity. While I went all fancy at things like pattern matching, the original solution goes like this:</p>
<p><pre class="brush: scala;">
def isPalindrome[A](ls: List[A]): Boolean = ls == ls.reverse
</pre></p>
<p>Yeah, I should have thought of that&#8230;</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jcranky.wordpress.com/869/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jcranky.wordpress.com/869/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jcranky.wordpress.com/869/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jcranky.wordpress.com/869/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jcranky.wordpress.com/869/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jcranky.wordpress.com/869/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jcranky.wordpress.com/869/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jcranky.wordpress.com/869/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jcranky.wordpress.com/869/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jcranky.wordpress.com/869/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jcranky.wordpress.com/869/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jcranky.wordpress.com/869/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jcranky.wordpress.com/869/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jcranky.wordpress.com/869/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jcranky.com&amp;blog=4406641&amp;post=869&amp;subd=jcranky&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jcranky.com/2011/08/21/scala-problem-number-six/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3ebbb64cafa4d784a6f09a1886eaa5e7?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">paulosiqueira</media:title>
		</media:content>
	</item>
		<item>
		<title>The Anagram Puzzle in Scala</title>
		<link>http://jcranky.com/2011/08/02/the-anagram-puzzle-in-scala/</link>
		<comments>http://jcranky.com/2011/08/02/the-anagram-puzzle-in-scala/#comments</comments>
		<pubDate>Tue, 02 Aug 2011 12:09:23 +0000</pubDate>
		<dc:creator>Paulo "JCranky" Siqueira</dc:creator>
				<category><![CDATA[scala]]></category>
		<category><![CDATA[adaptworks]]></category>
		<category><![CDATA[anagram]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[dojo]]></category>
		<category><![CDATA[ListBuffer]]></category>
		<category><![CDATA[permutations]]></category>
		<category><![CDATA[puzzle]]></category>

		<guid isPermaLink="false">http://jcranky.com/?p=886</guid>
		<description><![CDATA[A few weeks ago, we had a coding dojo in Adaptworks. There, we decided to use Scala as the coding language, and the problem to be solved was the Anagram puzzle. Unfortunately we didn&#8217;t manage to solve the problem during the coding session, but I&#8217;m sure everyone learned a bit more about Scala, which is [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jcranky.com&amp;blog=4406641&amp;post=886&amp;subd=jcranky&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>A few weeks ago, we had a coding dojo in <a title="Adaptworks" href="http://www.adaptworks.com.br/" target="_blank">Adaptworks</a>. There, we decided to use Scala as the coding language, and the problem to be solved was the Anagram puzzle. Unfortunately we didn&#8217;t manage to solve the problem during the coding session, but I&#8217;m sure everyone learned a bit more about Scala, which is always the main goal =)</p>
<p>Now, of course I wanted to have this puzzle solved. So I finally used some spare time and coded a solution. My solution is not the best possible one by any means, but it works and was really fun to work on.</p>
<p>Before showing the solution, a brief explanation of the problem, translated from <a title="Anagrams" href="http://dojopuzzles.com/problemas/exibe/anagramas/" target="_blank">the description in portuguese</a>:</p>
<pre>Write a program that generates all possible anagrams of a given string.

For instance, the possible anagrams of "biro" are:

biro bior brio broi boir bori
ibro ibor irbo irob iobr iorb
rbio rboi ribo riob roib robi
obir obri oibr oirb orbi orib</pre>
<p>I&#8217;m still trying to find better and more readable solutions, but for now the current one is as follows:</p>
<p><pre class="brush: scala;">
class Anagram {
  def anagrams(word: String): Set[String] = anagram(word).toSet

  def anagram(word: String): List[String] = {
    if (word.length == 1) {
      List(word)

    } else {
      var anagrams = ListBuffer[String]()
      0 to word.length-1 foreach { i =&gt;
        anagrams ++= (anagram(without(i, word)) map (word.charAt(i) + _))
      }

      anagrams.toList
    }
  }

  def without(index: Int, word: String): String = new StringBuilder(word).deleteCharAt(index).toString
}
</pre></p>
<p>In summary, what I tried to do was combining the &#8216;current&#8217; char with all combinations of the rest of the input string. I hope this makes sense while reading the code. The code works &#8211; I have some unit tests that we generated during the coding dojo plus a couple more tests I added afterwards. Yet, I&#8217;m not happy with the anagram ListBuffer&#8230; I guess this could be solved in a more elegant way. Ideas?</p>
<p>An extra curiosity. While looking into the Scala Docs API to find possible ways to solve the puzzle, I discovered a very handy function that can be called on strings (and other kinds of collections as well): <em>permutations</em>. It basically solves this puzzle in a single method call&#8230;</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jcranky.wordpress.com/886/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jcranky.wordpress.com/886/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jcranky.wordpress.com/886/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jcranky.wordpress.com/886/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jcranky.wordpress.com/886/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jcranky.wordpress.com/886/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jcranky.wordpress.com/886/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jcranky.wordpress.com/886/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jcranky.wordpress.com/886/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jcranky.wordpress.com/886/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jcranky.wordpress.com/886/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jcranky.wordpress.com/886/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jcranky.wordpress.com/886/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jcranky.wordpress.com/886/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jcranky.com&amp;blog=4406641&amp;post=886&amp;subd=jcranky&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jcranky.com/2011/08/02/the-anagram-puzzle-in-scala/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3ebbb64cafa4d784a6f09a1886eaa5e7?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">paulosiqueira</media:title>
		</media:content>
	</item>
		<item>
		<title>The Developers Conference 2011</title>
		<link>http://jcranky.com/2011/07/14/the-developers-conference-2011/</link>
		<comments>http://jcranky.com/2011/07/14/the-developers-conference-2011/#comments</comments>
		<pubDate>Thu, 14 Jul 2011 16:18:47 +0000</pubDate>
		<dc:creator>Paulo "JCranky" Siqueira</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[agile]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[scala]]></category>
		<category><![CDATA[tdc]]></category>
		<category><![CDATA[tdc 2011]]></category>
		<category><![CDATA[thedevelopersconference]]></category>

		<guid isPermaLink="false">http://jcranky.com/?p=877</guid>
		<description><![CDATA[From July 6 to 10 happened The Developers Conference 2011 down here in São Paulo. The other editions were really good and, better, this one topped all of them. It was a really fun and productive event. I had the chance to meet friends and to get to know new people. If you were not [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jcranky.com&amp;blog=4406641&amp;post=877&amp;subd=jcranky&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>From July 6 to 10 happened <a title="The Developers Conference 2011" href="http://www.thedevelopersconference.com.br/tdc/2011/index.html#geral" target="_blank">The Developers Conference 2011</a> down here in São Paulo. The other editions were really good and, better, this one topped all of them. It was a really fun and productive event. I had the chance to meet friends and to get to know new people. If you were not there, #protip for you: BE THERE NEXT YEAR. Really.</p>
<p>Before talking more about the conference, I have to thank <a title="Globalcode" href="http://globalcode.com.br" target="_blank">Globalcode</a> (in special Vinicius and Yara). They did a great job =)</p>
<p>Since last year, the premise of the conference is to have people from all kinds of backgrounds, from a lot of different communities, like Java, Ruby, Python, Agile, Javascript, .NET and lots more. Each day featured five different tracks, with different topics, to fit all those communities and tools. The result is that we had a lot of different people and lots of opportunities to talk to people you usually would not encounter. This was a really enriching experience.</p>
<p>In a personal level, this conference was special to me: I made two &#8216;speaking&#8217; participations. In one of them I talked about Scala, an introductory talk (you can find the sample code used in the talk in <a title="Scala TDC2011 Source Repo" href="https://github.com/jcranky/scala-tdc2011" target="_blank">my github</a>). The other one was a panel consisting of people from different languages on the jvm &#8211; lots of fun =)</p>
<p>In the event, I had one personal fail moment, that should remain as a tip for everyone out there. In a given moment, when talking to someone, I was asked for my business card&#8230; and didn&#8217;t have any on me. So the obvious tip: always have business cards with you. Always.</p>
<p>Now, if you follow my posts about events you may have noticed that this one is a little different. It contains no pictures. This is due to a second fail, that happened on this last Monday. I lost (or it got stolen&#8230;) my mobile. And with it, all the pictures from the entire event. Sorry =(</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jcranky.wordpress.com/877/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jcranky.wordpress.com/877/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jcranky.wordpress.com/877/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jcranky.wordpress.com/877/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jcranky.wordpress.com/877/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jcranky.wordpress.com/877/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jcranky.wordpress.com/877/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jcranky.wordpress.com/877/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jcranky.wordpress.com/877/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jcranky.wordpress.com/877/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jcranky.wordpress.com/877/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jcranky.wordpress.com/877/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jcranky.wordpress.com/877/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jcranky.wordpress.com/877/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jcranky.com&amp;blog=4406641&amp;post=877&amp;subd=jcranky&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jcranky.com/2011/07/14/the-developers-conference-2011/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3ebbb64cafa4d784a6f09a1886eaa5e7?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">paulosiqueira</media:title>
		</media:content>
	</item>
		<item>
		<title>Scala Problem Number Five</title>
		<link>http://jcranky.com/2011/06/25/scala-problem-number-five/</link>
		<comments>http://jcranky.com/2011/06/25/scala-problem-number-five/#comments</comments>
		<pubDate>Sat, 25 Jun 2011 16:39:22 +0000</pubDate>
		<dc:creator>Paulo "JCranky" Siqueira</dc:creator>
				<category><![CDATA[scala]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[list reverse]]></category>
		<category><![CDATA[lists]]></category>
		<category><![CDATA[pattern matching]]></category>
		<category><![CDATA[s-99]]></category>
		<category><![CDATA[scala 99 problems]]></category>

		<guid isPermaLink="false">http://jcranky.com/?p=815</guid>
		<description><![CDATA[From the Scala 99 Problems, the problem number five goes like this: P05 (*) Reverse a list. Example: scala&#62; reverse(List(1, 1, 2, 3, 5, 8)) res0: List[Int] = List(8, 5, 3, 2, 1, 1) Starting with the obvious solution, using the API: Yes, the List API has a reverse function. Pretty handy =) How about [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jcranky.com&amp;blog=4406641&amp;post=815&amp;subd=jcranky&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>From the <a title="Scala 99 Problems" href="http://aperiodic.net/phil/scala/s-99/" target="_blank">Scala 99 Problems</a>, the problem number five goes like this:</p>
<dl>
<dt><strong>P05 (*) Reverse a list.</strong></dt>
<dd>Example:</p>
<pre>scala&gt; reverse(List(1, 1, 2, 3, 5, 8))
res0: List[Int] = List(8, 5, 3, 2, 1, 1)</pre>
</dd>
</dl>
<p>Starting with the obvious solution, using the API:</p>
<p><pre class="brush: scala;">
val list = List(1, 2, 3, 4)
list.reverse
</pre></p>
<p>Yes, the List API has a <em>reverse</em> function. Pretty handy =)</p>
<p>How about being more creative now? Like this:</p>
<p><pre class="brush: scala;">
def myReverse[A](list: List[A]): List[A] = list match {
  case head :: tail =&gt; myReverse(tail) :+ head
  case Nil =&gt; Nil
}
</pre></p>
<p>With pattern matching and recursion, the solution is pretty simple. We basically decompose the list, and rebuild it in the opposite order. The code is simple, but it can take a while to understand if you are not used to recursion. The pattern matching part shows how nicely we can decompose Lists in scala =)</p>
<p>Lets try a different solution now:</p>
<p><pre class="brush: scala;">
def myReverse[A](list: List[A]): List[A] = {
  var reversedList = List[A]()
  list.foreach(item =&gt; reversedList = item +: reversedList)
  reversedList
}
</pre></p>
<p>Here we take a more &#8216;traditional&#8217; approach &#8211; its closer to what we would do in Java, but still more compact and readable. First we create a buffer list, and add each element of the original list to the <em>beginning</em> of the buffer. Finally, we return the reversed list.</p>
<p>You can find the original solution to the problem <a title="Scala Problem Number Five - Solution" href="http://aperiodic.net/phil/scala/s-99/p05.scala" target="_blank">here</a>. As always, I learned something more after reading the original solution &#8211; I always write my solution before reading that. What I found is that it is also possible to solve the problem in a pure functional way, with foldLeft (copied from the original solution):</p>
<p><pre class="brush: scala;">
def reverseFunctional[A](ls: List[A]): List[A] =
    ls.foldLeft(List[A]()) { (r, h) =&gt; h :: r }
</pre></p>
<p>This solution is probably the most difficult to get at first, but the best after you get used to this kind of &#8216;extreme&#8217; functional stuff. Nice =)</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jcranky.wordpress.com/815/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jcranky.wordpress.com/815/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jcranky.wordpress.com/815/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jcranky.wordpress.com/815/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jcranky.wordpress.com/815/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jcranky.wordpress.com/815/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jcranky.wordpress.com/815/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jcranky.wordpress.com/815/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jcranky.wordpress.com/815/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jcranky.wordpress.com/815/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jcranky.wordpress.com/815/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jcranky.wordpress.com/815/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jcranky.wordpress.com/815/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jcranky.wordpress.com/815/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jcranky.com&amp;blog=4406641&amp;post=815&amp;subd=jcranky&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jcranky.com/2011/06/25/scala-problem-number-five/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3ebbb64cafa4d784a6f09a1886eaa5e7?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">paulosiqueira</media:title>
		</media:content>
	</item>
		<item>
		<title>About nulls and System Properties in Scala</title>
		<link>http://jcranky.com/2011/06/04/about-nulls-and-system-properties-in-scala/</link>
		<comments>http://jcranky.com/2011/06/04/about-nulls-and-system-properties-in-scala/#comments</comments>
		<pubDate>Sat, 04 Jun 2011 23:43:14 +0000</pubDate>
		<dc:creator>Paulo "JCranky" Siqueira</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[scala]]></category>
		<category><![CDATA[map]]></category>
		<category><![CDATA[none]]></category>
		<category><![CDATA[option]]></category>
		<category><![CDATA[some]]></category>
		<category><![CDATA[sys.props]]></category>
		<category><![CDATA[system properties]]></category>

		<guid isPermaLink="false">http://jcranky.com/?p=854</guid>
		<description><![CDATA[In Scala, it is usually a good idea to avoid nulls in your code. Instead of that, it is recommended that you use the Option class. This is how it works: if the value you are working with is null, it is represented by None, otherwise, it is represented by Some(value). Both None and Some [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jcranky.com&amp;blog=4406641&amp;post=854&amp;subd=jcranky&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In <a title="Scala" href="http://www.scala-lang.org/" target="_blank">Scala</a>, it is usually a good idea to avoid nulls in your code. Instead of that, it is recommended that you use the <a title="Scala Option Class" href="http://www.scala-lang.org/api/current/index.html#scala.Option" target="_blank"><em>Option</em></a> class. This is how it works: if the value you are working with is null, it is represented by <a title="None class" href="http://www.scala-lang.org/api/current/index.html#scala.None$" target="_blank"><em>None</em></a>, otherwise, it is represented by <a title="Some class" href="http://www.scala-lang.org/api/current/index.html#scala.Some" target="_blank"><em>Some</em></a>(value). Both <em>None</em> and <em>Some</em> are subclasses of <em>Option</em>.</p>
<p>This allows you to write code that is much more expressive than having &#8216;<em>if != null</em>&#8216; all over the place. Also, it is much less likely that you will forget to verify for nullity. One way of leveraging an <em>Option</em> value is through pattern matching:</p>
<p><pre class="brush: scala;">
val myValue: Option[String] = Some(&quot;Hello&quot;)
myValue match {
  case Some(x) =&gt; println(&quot;my value is &quot; + x)
  case None =&gt; println(&quot;nonthing found&quot;)
}
</pre></p>
<p>The <em>Option</em> class also offers some collection-like functions that allow for great readability as well. From the <em>Scaladoc</em>:</p>
<p><pre class="brush: scala;">
val name: Option[String] = request.getParameter(&quot;name&quot;)
val upper = name map { _.trim } filter { _.length != 0 } map { _.toUpperCase }
println(upper.getOrElse(&quot;&quot;))
</pre></p>
<p>Notice how the code execute transformations and &#8216;if-like&#8217; filtering, in a very clean way. After that, they try to use the value with the <em>getOrElse</em> function, which allows you to pass a default value to be used if the result from the previous line was <em>None</em>. Finally, if you REALLY want to use an if statement, you can call the <em>isDefined</em> function, which returns true if the value is <em>Some</em>, false otherwise.</p>
<p>Now lets talk about system properties. Lets face it: dealing with System Properties in Java is boring. You have to get each property you are interested in and have to verify if it is null or empty. After all the talk above, you probably are starting to guess where I&#8217;m heading, right?</p>
<p>Well, it is better than that. Scala encapsulates the system properties in a way we can leverage all the <em>Option</em> stuff explained above. Those properties are encapsulated in a class named <em><a title="SystemProperties class" href="http://www.scala-lang.org/api/current/index.html#scala.sys.SystemProperties" target="_blank">SystemProperties</a></em> and can be accessed by the object &#8216;<em>sys.props</em>&#8216;. Try typing it into a scala console session to see for yourself.</p>
<p>Since it is a map, you can use any map function you can use with other maps. Also, you can add and get properties from this object, and they will be reflected in the actual System Properties &#8211; which means that your Java code should be able to read the properties as well, even if through more traditional means (i.e. <em>System.getProperty</em>). Printing all system properties is as simple as:</p>
<p><pre class="brush: scala;">
sys.props.foreach(println)
</pre></p>
<p>And you clearly could do something more clever and useful with those properties.</p>
<p>There are two functions available in the <em>SystemProperties</em> class that I want to cover here: <em>get</em> and <em>getOrElse</em>. The first one returns a property with a given name, in an Option. With this, you can now handle your property with all the Option goodness we talked about above. The second function, <em>getOrElse</em>, is basically a direct way of calling the same function you could call in an <em>Option</em>. For example, lets say you read a database configuration from the system properties, and want to fall back to a default configuration if the property is not found:</p>
<p><pre class="brush: scala;">
val dbURL = sys.props.getOrElse(&quot;myapp.database.url&quot;, &quot;jdbc:postgresql://localhost:5432/mydb&quot;)
</pre></p>
<p>A lot better than reading the property and verifying if it exists &#8216;<em>by hand</em>&#8216;, right?</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jcranky.wordpress.com/854/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jcranky.wordpress.com/854/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jcranky.wordpress.com/854/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jcranky.wordpress.com/854/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jcranky.wordpress.com/854/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jcranky.wordpress.com/854/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jcranky.wordpress.com/854/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jcranky.wordpress.com/854/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jcranky.wordpress.com/854/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jcranky.wordpress.com/854/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jcranky.wordpress.com/854/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jcranky.wordpress.com/854/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jcranky.wordpress.com/854/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jcranky.wordpress.com/854/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jcranky.com&amp;blog=4406641&amp;post=854&amp;subd=jcranky&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jcranky.com/2011/06/04/about-nulls-and-system-properties-in-scala/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3ebbb64cafa4d784a6f09a1886eaa5e7?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">paulosiqueira</media:title>
		</media:content>
	</item>
		<item>
		<title>Migrating a java project to scala</title>
		<link>http://jcranky.com/2011/05/24/migrating-a-java-project-to-scala/</link>
		<comments>http://jcranky.com/2011/05/24/migrating-a-java-project-to-scala/#comments</comments>
		<pubDate>Tue, 24 May 2011 03:23:18 +0000</pubDate>
		<dc:creator>Paulo "JCranky" Siqueira</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[scala]]></category>
		<category><![CDATA[maven]]></category>
		<category><![CDATA[mvn]]></category>
		<category><![CDATA[pom]]></category>
		<category><![CDATA[web objects]]></category>
		<category><![CDATA[wounit]]></category>

		<guid isPermaLink="false">http://jcranky.com/?p=838</guid>
		<description><![CDATA[Hi everybody! This time I wanted to bring something different about scala &#8211; so I decided to migrate a java project to this language. The chosen one is an open source test framework for the Web Objects framework (sorry for the double f* word there) called WOUnit. Web Objects is a web application framework that [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jcranky.com&amp;blog=4406641&amp;post=838&amp;subd=jcranky&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Hi everybody!</p>
<p>This time I wanted to bring something different about scala &#8211; so I decided to migrate a java project to this language. The chosen one is an open source test framework for the Web Objects framework (sorry for the double f* word there) called <a title="WOUnit" href="http://hprange.github.com/wounit/" target="_blank">WOUnit</a>.</p>
<p>Web Objects is a web application framework that is quite old, and is developed by Apple. I&#8217;m not the right person to talk about this framework, so I&#8217;m not going further than that. The point is that this framework has some specificities that could make writing pure JUnit tests more troublesome than it should be, and this is where WOUnit comes in.</p>
<p>This project is reasonably small, so its a good choice for our small experiment. I&#8217;ll talk about what I did bellow, you can take a look in the project in the link above, and in the changes I made <a title="WOUnit - Scala" href="https://github.com/jcranky/wounit" target="_blank">in my fork</a>.</p>
<p>The current changes consisted of the following steps:</p>
<ul>
<li>added the scala plugin and scala library dependency to the project&#8217;s pom.xml file &#8211; more about the <a title="Maven scala plugin" href="http://scala-tools.org/mvnsites/maven-scala-plugin/index.html" target="_blank">maven scala plugin here</a>;</li>
<li>renamed the EOAssert.java file to EOAssert.scala;</li>
<li>rewrote the contents of the file to use scala sintax</li>
</ul>
<p>It was not complicated at all. You can find the current version of the file <a title="EOAssert.scala" href="https://github.com/jcranky/wounit/blob/master/src/main/java/com/wounit/matchers/EOAssert.scala" target="_blank">here</a>. And what have we gained with this?</p>
<p>First, the code is now more readable. Which one is easier to read, this:</p>
<p><pre class="brush: java;">
public static Matcher&lt;EOEnterpriseObject&gt; canBeDeleted() {
    return new CanBeDeletedMatcher&lt;EOEnterpriseObject&gt;();
}
</pre></p>
<p>or this:</p>
<p><pre class="brush: scala;">
def canBeDeleted(): Matcher[EOEnterpriseObject] =
  new CanBeDeletedMatcher[EOEnterpriseObject]
</pre></p>
<p>Nice, isn&#8217;t it? This comes to a point made by Martin Odersky (creator of Scala), where he said that the most important information in a function / method declaration is it&#8217;s name. Makes sense.</p>
<p>Second, you might have noticed that something seems to be missing in the scala version above. The java version has a <em>static</em> method, which doesn&#8217;t exist in scala. Actually, it isn&#8217;t even needed. Our EOAssert in scala is an <em>object</em>, which means scala handles it internally as a singleton. This feels a lot more correct from an object orientation point of view than having <em>static</em> things all over the place. Yet, from Java, you access EOAssert&#8217;s methods exactly as before &#8211; which means I didn&#8217;t have to change ANYTHING in the EOAssert class usages throughout the project, and it kept compiling and the tests passing.</p>
<p>Also, please notice how we are mixing java and scala code seamlessly here. Any doubts you could use scala as part of just about ANY java project without major headaches?</p>
<p>One thing I&#8217;m not sure about how it is working right now are the javadocs. In the migration, I kept then exactly how they were, and I&#8217;m not sure how the scaladoc would be generated for then. Since EOAssert class is intended as a public API, this is an issue that must be taken into account.</p>
<p>In summary, I didn&#8217;t do too much here &#8211; it was just a small experiment on migration a java project to scala. I hope you liked it and perhaps felt that scala is really worth considering serious usage. I&#8217;m not sure if I&#8217;ll continue this migration, but if I do, I&#8217;ll have more posts about it.</p>
<p>EDIT:</p>
<p>about the scaladocs point, this link might help a little bit: <a title="http://lampsvn.epfl.ch/trac/scala-old/wiki/Scaladoc/AuthorDocs" href="http://lampsvn.epfl.ch/trac/scala-old/wiki/Scaladoc/AuthorDocs" target="_blank">http://lampsvn.epfl.ch/trac/scala-old/wiki/Scaladoc/AuthorDocs</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jcranky.wordpress.com/838/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jcranky.wordpress.com/838/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jcranky.wordpress.com/838/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jcranky.wordpress.com/838/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jcranky.wordpress.com/838/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jcranky.wordpress.com/838/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jcranky.wordpress.com/838/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jcranky.wordpress.com/838/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jcranky.wordpress.com/838/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jcranky.wordpress.com/838/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jcranky.wordpress.com/838/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jcranky.wordpress.com/838/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jcranky.wordpress.com/838/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jcranky.wordpress.com/838/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jcranky.com&amp;blog=4406641&amp;post=838&amp;subd=jcranky&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jcranky.com/2011/05/24/migrating-a-java-project-to-scala/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3ebbb64cafa4d784a6f09a1886eaa5e7?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">paulosiqueira</media:title>
		</media:content>
	</item>
	</channel>
</rss>
