<?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>Colin Howe</title>
	<atom:link href="http://www.colinhowe.co.uk/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.colinhowe.co.uk</link>
	<description>My blog</description>
	<lastBuildDate>Mon, 12 Jul 2010 21:48:41 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>A tip for when TDD gets hard</title>
		<link>http://www.colinhowe.co.uk/2010/07/12/a-tip-for-when-tdd-gets-hard/</link>
		<comments>http://www.colinhowe.co.uk/2010/07/12/a-tip-for-when-tdd-gets-hard/#comments</comments>
		<pubDate>Mon, 12 Jul 2010 21:48:41 +0000</pubDate>
		<dc:creator>Colin</dc:creator>
				<category><![CDATA[Coding / Tech]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://www.colinhowe.co.uk/?p=284</guid>
		<description><![CDATA[
One of the biggest reasons I see for TDD not being used enough is that it makes things harder. The typical complaints are:

&#8220;I have to setup/mock too much&#8221;
&#8220;I want to test my complicated internals without exposing them&#8221;
&#8220;I&#8217;m writing a web-app, it&#8217;s hard to get the interactions right&#8221;

All of these should be alarm bells in your [...]]]></description>
			<content:encoded><![CDATA[
<p>One of the biggest reasons I see for TDD not being used enough is that it makes things harder. The typical complaints are:</p>
<ul>
<li>&#8220;I have to setup/mock too much&#8221;</li>
<li>&#8220;I want to test my complicated internals without exposing them&#8221;</li>
<li>&#8220;I&#8217;m writing a web-app, it&#8217;s hard to get the interactions right&#8221;</li>
</ul>
<p>All of these should be alarm bells in your head. The design needs a rethink. These alarm bells are one of the benefits of TDD &#8211; if it&#8217;s getting tough then the design probably needs a rethink.</p>
<h3>Too much setup/mocking</h3>
<p>This likely indicates that your classes are too coupled. Consider the following:</p>
<ul>
<li>Is there any functionality that is in the wrong place? Should it be moved to a different/new class?</li>
<li>Should you create a simple class with little logic that provides the glue for your application?</li>
<li>Have you considered <a href="http://martinfowler.com/bliki/InversionOfControl.html">inversion of control</a>?</li>
</ul>
<h3>Testing Complicated Internals</h3>
<p>Sometimes you start with something simple and then it gets more complicated. As it gets more complicated the internals get more complex and you start wanting to test the internals to make sure your state is correct. Don&#8217;t.</p>
<p>Instead, consider whether those internals could be extracted out in to a new class which has the sole purpose of implementing the complex logic you were trying to hide away.</p>
<p>This makes testing easier, improves re-usability and leads to code that is easier to understand.</p>
<h3>Testing Web-apps</h3>
<p>Testing web-apps <em>can</em> require a different set of tools. Often, you want to be using some sort of browser based testing <b>as well as</b> your normal testing. Most of your browser based testing should be simple (little logic) and be something of a formality. </p>
<p>If you have all your business logic tidied away nicely in to your models then your views and their controllers should be simple. Your tests for these should then be simple.</p>
<p>You should be able to directly call methods on your controllers to test any logic present in them. If you can&#8217;t, then it is likely there is logic in the wrong place or you are storing too much state.</p>
<h3>To summarise&#8230;</h3>
<h1>If TDD gets hard &#8211; check your design</h1>

]]></content:encoded>
			<wfw:commentRss>http://www.colinhowe.co.uk/2010/07/12/a-tip-for-when-tdd-gets-hard/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Presentations and History</title>
		<link>http://www.colinhowe.co.uk/2010/07/12/presentations-and-history/</link>
		<comments>http://www.colinhowe.co.uk/2010/07/12/presentations-and-history/#comments</comments>
		<pubDate>Mon, 12 Jul 2010 12:27:55 +0000</pubDate>
		<dc:creator>Colin</dc:creator>
				<category><![CDATA[Presentation tips]]></category>
		<category><![CDATA[history]]></category>
		<category><![CDATA[presenting]]></category>

		<guid isPermaLink="false">http://www.colinhowe.co.uk/?p=286</guid>
		<description><![CDATA[
Four-year-old child: What&#8217;s ice hockey?
Dad: Ice hockey may have started out as a form of hurling that was played on the ice. An account by Thomas Chandler Haliburton suggests that it was played as early as 1810&#8230;
Would you really respond like that to your child?
Or would you show them the game?
I&#8217;ve seen many presentations that [...]]]></description>
			<content:encoded><![CDATA[
<p><b>Four-year-old child</b>: What&#8217;s ice hockey?<br />
<b>Dad</b>: Ice hockey may have started out as a form of hurling that was played on the ice. An account by Thomas Chandler Haliburton suggests that it was played as early as 1810&#8230;</p>
<p>Would you really respond like that to your child?</p>
<p>Or would you show them the game?</p>
<p>I&#8217;ve seen many presentations that start with a history section. <em>Every single one</em> could have skipped the history and been a <em>better presentation</em> for it.</p>
<p>We don&#8217;t want to know the history. Get to the interesting part. Hook us with that and we may want to know the history later.</p>
<p><em>Show us the game.</em></p>

]]></content:encoded>
			<wfw:commentRss>http://www.colinhowe.co.uk/2010/07/12/presentations-and-history/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Rewriting Java in Scala &amp; Making Code Lovely 6 &#8211; Pattern Matching</title>
		<link>http://www.colinhowe.co.uk/2010/06/24/rewriting-java-in-scala-making-code-lovely-6-pattern-matching/</link>
		<comments>http://www.colinhowe.co.uk/2010/06/24/rewriting-java-in-scala-making-code-lovely-6-pattern-matching/#comments</comments>
		<pubDate>Thu, 24 Jun 2010 09:58:35 +0000</pubDate>
		<dc:creator>Colin</dc:creator>
				<category><![CDATA[Coding / Tech]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[lovely]]></category>
		<category><![CDATA[pattern matching]]></category>
		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://www.colinhowe.co.uk/?p=270</guid>
		<description><![CDATA[
Pattern Matching &#8211; The Problem
I&#8217;m going to start off with a code snippet:

if &#40;x instanceof SomeObject&#41; &#123;
  &#40;&#40;SomeObject&#41;x&#41;.someMethod&#40;&#41;;
&#125; else if &#40;x instanceof SomeOtherObject&#41; &#123;
  &#40;&#40;SomeOtherObject&#41;x&#41;.someOtherMethod&#40;&#41;;
&#125;

Eugh. This is ugly. Not only that, it&#8217;s something we all see quite often when writing Java. No matter how well we&#8217;ve designed our APIs to avoid needing this [...]]]></description>
			<content:encoded><![CDATA[
<h2>Pattern Matching &#8211; The Problem</h2>
<p>I&#8217;m going to start off with a code snippet:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>x <span style="color: #000000; font-weight: bold;">instanceof</span> SomeObject<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>SomeObject<span style="color: #009900;">&#41;</span>x<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">someMethod</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>x <span style="color: #000000; font-weight: bold;">instanceof</span> SomeOtherObject<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>SomeOtherObject<span style="color: #009900;">&#41;</span>x<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">someOtherMethod</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Eugh. This is ugly. Not only that, it&#8217;s something we all see quite often when writing Java. No matter how well we&#8217;ve designed our APIs to avoid needing this sort of thing, it always creeps in somewhere.</p>
<p>Scala gives us a (huge) feature called Pattern Matching that allows us to tidy this up:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">x <span style="color: #003399; font-weight: bold;">match</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003399; font-weight: bold;">case</span> y <span style="color: #000080;">:</span> SomeObject <span style="color: #000080;">=&gt;</span> y.<span style="color: #000000;">someMethod</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #003399; font-weight: bold;">case</span> y <span style="color: #000080;">:</span> SomeOtherObject <span style="color: #000080;">=&gt;</span> y.<span style="color: #000000;">someOtherMethod</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This is very similar to Java&#8217;s switch. The first case clause can be thought of as &#8220;if x is of type SomeObject then create a new variable, y, and set it to x&#8221;. Unlike some languages, Scala does not have fall through, if the first clause is matched then no other clauses are checked.</p>
<p>I prefer this to Java&#8217;s style of doing things because the cast/instance checking is done only once in your code. This is much more readable. Readability is only the tip of the iceberg. Pattern matching is <em>very</em> powerful and I&#8217;m now going to cover a few more uses of pattern matching.</p>
<h3>Matching Constants</h3>
<p>We can match against constants:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">x <span style="color: #003399; font-weight: bold;">match</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003399; font-weight: bold;">case</span> <span style="color: #cc66cc;">1</span> <span style="color: #000080;">=&gt;</span> println<span style="color: #009900;">&#40;</span><span style="color: #6666FF;">&quot;ONE!&quot;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #003399; font-weight: bold;">case</span> <span style="color: #cc66cc;">2</span> <span style="color: #000080;">=&gt;</span> println<span style="color: #009900;">&#40;</span><span style="color: #6666FF;">&quot;Two...&quot;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #003399; font-weight: bold;">case</span> <span style="color: #000080;">_</span> <span style="color: #000080;">=&gt;</span> println<span style="color: #009900;">&#40;</span><span style="color: #6666FF;">&quot;I can't count higher :(&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The first two cases here should be simple to follow. The third one is the same as Java&#8217;s default when using switch.</p>
<p>We can also return values out of a pattern match:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">y <span style="color: #000080;">=</span> x <span style="color: #003399; font-weight: bold;">match</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003399; font-weight: bold;">case</span> <span style="color: #6666FF;">&quot;one&quot;</span> <span style="color: #000080;">=&gt;</span> <span style="color: #cc66cc;">1</span>
  <span style="color: #003399; font-weight: bold;">case</span> <span style="color: #6666FF;">&quot;two&quot;</span> <span style="color: #000080;">=&gt;</span> <span style="color: #cc66cc;">2</span>
  <span style="color: #003399; font-weight: bold;">case</span> <span style="color: #000080;">_</span> <span style="color: #000080;">=&gt;</span> <span style="color: #cc66cc;">3</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Oh, we can match against strings too&#8230; or, if we really want, we can match both in the same match:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">x <span style="color: #003399; font-weight: bold;">match</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003399; font-weight: bold;">case</span> <span style="color: #cc66cc;">1</span> <span style="color: #000080;">=&gt;</span> <span style="color: #cc66cc;">1</span>
  <span style="color: #003399; font-weight: bold;">case</span> <span style="color: #6666FF;">&quot;one&quot;</span> <span style="color: #000080;">=&gt;</span> <span style="color: #cc66cc;">1</span>
  <span style="color: #003399; font-weight: bold;">case</span> <span style="color: #000080;">_</span> <span style="color: #000080;">=&gt;</span> <span style="color: #cc66cc;">2</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>Matching Regular Expressions</h3>
<p>Scala also allows us to match against regular expressions.</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #003399; font-weight: bold;">val</span> timeformat <span style="color: #000080;">=</span> <span style="color: #6666FF;">&quot;&quot;</span><span style="color: #6666FF;">&quot;(<span style="color: #0000ff; font-weight: bold;">\d</span><span style="color: #0000ff; font-weight: bold;">\d</span>):(<span style="color: #0000ff; font-weight: bold;">\d</span><span style="color: #0000ff; font-weight: bold;">\d</span>)&quot;</span><span style="color: #6666FF;">&quot;&quot;</span>.<span style="color: #000000;">r</span>
<span style="color: #003399; font-weight: bold;">val</span> time <span style="color: #000080;">=</span> <span style="color: #6666FF;">&quot;19:21&quot;</span>
time <span style="color: #003399; font-weight: bold;">match</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003399; font-weight: bold;">case</span> timeformat<span style="color: #009900;">&#40;</span>h, m<span style="color: #009900;">&#41;</span> <span style="color: #000080;">=&gt;</span> println<span style="color: #009900;">&#40;</span><span style="color: #6666FF;">&quot;It is &quot;</span> + m + <span style="color: #6666FF;">&quot; minutes past hour &quot;</span> + h<span style="color: #009900;">&#41;</span>
  <span style="color: #003399; font-weight: bold;">case</span> <span style="color: #000080;">_</span> <span style="color: #000080;">=&gt;</span> println<span style="color: #009900;">&#40;</span><span style="color: #6666FF;">&quot;You gave me junk :(&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Read that again.</p>
<p>In the above example, we create a regular expression that matches times that look like &#8220;15:49&#8243;, i.e. the standard western way of doing 24 hour times. We then pattern match on the string &#8220;19:21&#8243;. The first clause will:</p>
<ul>
<li>Check if the given string matches the regular expression</li>
<li>If a match is found, the two groups in the regular expression are substituted into variables h and m respectively</li>
</ul>
<p>You can also ignore parts of the match using placeholder syntax (the underscore) if you want:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #003399; font-weight: bold;">val</span> timeformat <span style="color: #000080;">=</span> <span style="color: #6666FF;">&quot;&quot;</span><span style="color: #6666FF;">&quot;(<span style="color: #0000ff; font-weight: bold;">\d</span><span style="color: #0000ff; font-weight: bold;">\d</span>):(<span style="color: #0000ff; font-weight: bold;">\d</span><span style="color: #0000ff; font-weight: bold;">\d</span>)&quot;</span><span style="color: #6666FF;">&quot;&quot;</span>.<span style="color: #000000;">r</span>
<span style="color: #003399; font-weight: bold;">val</span> time <span style="color: #000080;">=</span> <span style="color: #6666FF;">&quot;4:21&quot;</span>
time <span style="color: #003399; font-weight: bold;">match</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003399; font-weight: bold;">case</span> timeformat<span style="color: #009900;">&#40;</span>h, <span style="color: #000080;">_</span><span style="color: #009900;">&#41;</span> <span style="color: #000080;">=&gt;</span> println<span style="color: #009900;">&#40;</span><span style="color: #6666FF;">&quot;It is &quot;</span> + h + <span style="color: #6666FF;">&quot; o'clock&quot;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #003399; font-weight: bold;">case</span> <span style="color: #000080;">_</span> <span style="color: #000080;">=&gt;</span> println<span style="color: #009900;">&#40;</span><span style="color: #6666FF;">&quot;You gave me junk :(&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Short and readable. Nice.</p>
<h3>Matching Types</h3>
<p>Matching types can make equals method remarkably tidy. Suppose we&#8217;re writing the equals method for ThisClass which has a single field called someField:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #003399; font-weight: bold;">def</span> equals<span style="color: #009900;">&#40;</span>other <span style="color: #000080;">:</span> Any<span style="color: #009900;">&#41;</span> <span style="color: #000080;">=</span>
  other <span style="color: #003399; font-weight: bold;">match</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003399; font-weight: bold;">case</span> other <span style="color: #000080;">:</span> ThisClass <span style="color: #000080;">=&gt;</span> other.<span style="color: #000000;">someField</span> <span style="color: #000080;">==</span> <span style="color: #003399; font-weight: bold;">this</span>.<span style="color: #000000;">someField</span>
    <span style="color: #003399; font-weight: bold;">case</span> <span style="color: #000080;">_</span> <span style="color: #000080;">=&gt;</span> <span style="color: #003399; font-weight: bold;">false</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Easy. The first clause says that if other is of type ThisClass then cast it to ThisClass and put it in a new variable called other (we can re-use the same name here without a conflict). We can then use other to compare the fields. The second clause is the result if no match was found in the first clause (i.e. if other is not an instance of ThisClass).</p>
<h3>Matching Anything</h3>
<p>In Scala you can use something called an extractor object to match against anything you could ever want. I&#8217;m not going to go into this in this post as it&#8217;s a big topic that deserves its own posting.</p>
<h2>Summary</h2>
<p>Many people compare pattern matching to &#8220;switch on steroids&#8221; or similar. This isn&#8217;t an exaggeration. It&#8217;s an extremely powerful tool and can lead to very succinct code (especially when doing things with regular expressions).</p>

]]></content:encoded>
			<wfw:commentRss>http://www.colinhowe.co.uk/2010/06/24/rewriting-java-in-scala-making-code-lovely-6-pattern-matching/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Rewriting Java in Scala &amp; Making Code Lovely 5 – Structural Typing</title>
		<link>http://www.colinhowe.co.uk/2010/06/03/rewriting-java-in-scala-making-code-lovely-5-%e2%80%93-structural-typing/</link>
		<comments>http://www.colinhowe.co.uk/2010/06/03/rewriting-java-in-scala-making-code-lovely-5-%e2%80%93-structural-typing/#comments</comments>
		<pubDate>Thu, 03 Jun 2010 20:22:14 +0000</pubDate>
		<dc:creator>Colin</dc:creator>
				<category><![CDATA[Coding / Tech]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[front end development]]></category>
		<category><![CDATA[lovely]]></category>
		<category><![CDATA[scala]]></category>
		<category><![CDATA[typing]]></category>

		<guid isPermaLink="false">http://www.colinhowe.co.uk/?p=255</guid>
		<description><![CDATA[
What is structural typing?
Structural typing is a way of saying that we don&#8217;t care about the type of a variable but we do care about the structure of the type of a variable.
Here&#8217;s a simple example to explain this further:

def printArea&#40;shape : &#123; def area : Double&#125;&#41; &#123;
    println&#40;shape + &#34; has [...]]]></description>
			<content:encoded><![CDATA[
<h2>What is structural typing?</h2>
<p>Structural typing is a way of saying that we don&#8217;t care about the <em>type</em> of a variable but we do care about the <em>structure</em> of the type of a variable.</p>
<p>Here&#8217;s a simple example to explain this further:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #003399; font-weight: bold;">def</span> printArea<span style="color: #009900;">&#40;</span>shape <span style="color: #000080;">:</span> <span style="color: #009900;">&#123;</span> <span style="color: #003399; font-weight: bold;">def</span> area <span style="color: #000080;">:</span> Double<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    println<span style="color: #009900;">&#40;</span>shape + <span style="color: #6666FF;">&quot; has area &quot;</span> + shape.<span style="color: #000000;">area</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The interesting part of the above function is the type of the argument shape; shape is a structurally typed variable. We can pass any object we want to the printArea function so long as it has a function called area that returns a Double:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #003399; font-weight: bold;">case</span> <span style="color: #003399; font-weight: bold;">class</span> square<span style="color: #009900;">&#40;</span><span style="color: #003399; font-weight: bold;">val</span> sideLength <span style="color: #000080;">:</span> Double<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003399; font-weight: bold;">def</span> area <span style="color: #000080;">=</span> sideLength <span style="color: #000080;">*</span> sideLength
<span style="color: #009900;">&#125;</span>
&nbsp;
printArea<span style="color: #009900;">&#40;</span>square<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">5.0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #008000; font-style: italic;">// square(5.0) has area 25.0</span>
&nbsp;
<span style="color: #003399; font-weight: bold;">case</span> <span style="color: #003399; font-weight: bold;">class</span> circle<span style="color: #009900;">&#40;</span><span style="color: #003399; font-weight: bold;">val</span> radius <span style="color: #000080;">:</span> Double<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003399; font-weight: bold;">def</span> area <span style="color: #000080;">=</span> scala.<span style="color: #000000;">math</span>.<span style="color: #000000;">Pi</span> <span style="color: #000080;">*</span> radius <span style="color: #000080;">*</span> radius
<span style="color: #009900;">&#125;</span>
&nbsp;
printArea<span style="color: #009900;">&#40;</span>circle<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">10.0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #008000; font-style: italic;">// circle(10.0) has area 314.1592653589793</span></pre></div></div>

<p>We can go as far as you want with specifying structural types:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #003399; font-weight: bold;">def</span> pointlessFunction<span style="color: #009900;">&#40;</span>thing <span style="color: #000080;">:</span> <span style="color: #009900;">&#123;</span> 
    <span style="color: #003399; font-weight: bold;">def</span> areaWithMultiplier<span style="color: #009900;">&#40;</span>multiplier <span style="color: #000080;">:</span> Double<span style="color: #009900;">&#41;</span> <span style="color: #000080;">:</span> Double
    <span style="color: #003399; font-weight: bold;">def</span> length <span style="color: #000080;">:</span> Double<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #008000; font-style: italic;">// ...</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The above function takes any type that has two functions:</p>
<ul>
<li>areaWithMultiplier that has one argument of type Double and returns a Double</li>
<li>length that returns a double</li>
</ul>
<p>But, the above example is ugly. Type aliases to the rescue:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #003399; font-weight: bold;">type</span> t <span style="color: #000080;">=</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003399; font-weight: bold;">def</span> areaWithMultiplier<span style="color: #009900;">&#40;</span>multiplier <span style="color: #000080;">:</span> Double<span style="color: #009900;">&#41;</span> <span style="color: #000080;">:</span> Double
  <span style="color: #003399; font-weight: bold;">def</span> length <span style="color: #000080;">:</span> Double
<span style="color: #009900;">&#125;</span>
<span style="color: #003399; font-weight: bold;">def</span> pointlessFunction<span style="color: #009900;">&#40;</span>thing <span style="color: #000080;">:</span> t<span style="color: #009900;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #008000; font-style: italic;">// ...</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Here we&#8217;ve created a type alias <em>t</em> that aliases the complex structural type. We then use that type in the definition of <em>pointlessFunction</em> in the same way we&#8217;d use any other type.</p>
<h2>What&#8217;s the use?</h2>
<h3>Very Reusable Functions</h3>
<p>One of the great uses of structural types is that they allow us to create very reusable functions. For example, we could write a function that sorts a list of shapes according to their size without requiring that the shapes implement a common interface:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #003399; font-weight: bold;">type</span> hasArea <span style="color: #000080;">=</span> <span style="color: #009900;">&#123;</span> <span style="color: #003399; font-weight: bold;">def</span> area <span style="color: #000080;">:</span> Double <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003399; font-weight: bold;">def</span> sortShapes<span style="color: #009900;">&#40;</span>list <span style="color: #000080;">:</span> List<span style="color: #009900;">&#91;</span>hasArea<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #000080;">:</span> List<span style="color: #009900;">&#91;</span>hasArea<span style="color: #009900;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #009900;">&#123;</span>
  list.<span style="color: #000000;">sortWith</span><span style="color: #009900;">&#40;</span><span style="color: #000080;">_</span>.<span style="color: #000000;">area</span> <span style="color: #000080;">&lt;</span> <span style="color: #000080;">_</span>.<span style="color: #000000;">area</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
sortShapes<span style="color: #009900;">&#40;</span>
  List<span style="color: #009900;">&#91;</span>hasArea<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#40;</span>square<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">5.0</span><span style="color: #009900;">&#41;</span>, square<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">6.0</span><span style="color: #009900;">&#41;</span>, circle<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2.0</span><span style="color: #009900;">&#41;</span>, circle<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3.0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span>
<span style="color: #008000; font-style: italic;">// Result is: List(circle(2.0), square(5.0), circle(3.0), square(6.0))</span></pre></div></div>

<p>We could then pass anything we wanted to this function: shapes, countries, building plans, anything with an area function.</p>
<p>This becomes incredibly useful when implementing algorithms. For example, an algorithm that determines how to fit different shapes into a box could be used on any type that implemented the required method.</p>
<h3>Legacy Classes</h3>
<p>Structural types are also a great way to make legacy types easier to use.</p>
<p>We are given two classes that represent different types of customers: BusinessCustomer, IndividualCustomer. Both of these classes have a method getPhoneNumber. They don&#8217;t share a base class. We want to use a library that allows us to call these people from our computer. The typical Java approach might be something like:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> dial<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> customer<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003399;">String</span> phoneNumber<span style="color: #339933;">;</span>  
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>customer <span style="color: #000000; font-weight: bold;">instanceof</span> BusinessCustomer<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    phoneNumber <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>BusinessCustomer<span style="color: #009900;">&#41;</span>customer<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getPhoneNumber</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>customer <span style="color: #000000; font-weight: bold;">instanceof</span> IndividualCustomer<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    phoneNumber <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>IndividualCustomer<span style="color: #009900;">&#41;</span>customer<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getPhoneNumber</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #003399;">IllegalArgumentException</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Argument isn't a customter &quot;</span> <span style="color: #339933;">+</span> customer<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  Dialer.<span style="color: #006633;">dial</span><span style="color: #009900;">&#40;</span>phoneNumber<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Ugh.</p>
<p>Apart from this being a lot of code for something simple, you won&#8217;t be told of errors at compilation time!</p>
<p>Enter structural typing:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #003399; font-weight: bold;">def</span> dial<span style="color: #009900;">&#40;</span>customer <span style="color: #000080;">:</span> <span style="color: #009900;">&#123;</span> <span style="color: #003399; font-weight: bold;">def</span> getPhoneNumber <span style="color: #000080;">:</span> String <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #009900;">&#123;</span>
  Dialer.<span style="color: #000000;">dial</span><span style="color: #009900;">&#40;</span>customer.<span style="color: #000000;">getPhoneNumber</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Much better. Compile-time safe and far easier to follow.</p>
<h2>Duck-Typing</h2>
<p>Structural typing is effectively <a href="http://en.wikipedia.org/wiki/Duck_typing">duck typing</a>. Forget littering all your classes with random interfaces IsCloneable, IsSortable, IsComparable, IsJedi&#8230; use structural typing and we can say &#8220;so long as this argument has a certain property then I&#8217;ll use it&#8221;. </p>
<p>This gives us some of the benefits of dynamic typing (being able to use whatever we want wherever we want) without losing the benefits of a compiler to make sure we&#8217;ve not done something silly.</p>
<h2>Under the Covers</h2>
<p>Under the covers Scala generates byte-code similar to the following Java:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> printArea<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> shape<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">double</span> area <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Double</span><span style="color: #009900;">&#41;</span>shape.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getMethod</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;area&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">invoke</span><span style="color: #009900;">&#40;</span>shape<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Shape &quot;</span> <span style="color: #339933;">+</span> shape <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; has area &quot;</span> <span style="color: #339933;">+</span> area<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>That&#8217;s reflection at work. The up-side of this is that we can use our functions from Java code &#8211; but without any compile-time type safety. All structural types get compiled down to Object and Scala does the work at run-time to determine if the argument fits the function.</p>
<h2>Summary</h2>
<p>Structural typing allows us to use arguments according to what properties they have, not what type they are. This can be a very powerful tool and lead to very neat code. It also gives us the benefit of greater compile-time checking when compared against using instanceof in lots of places.</p>
<p>If you want to see a great real-world example of structural typing then take a look at <a href="http://debasishg.blogspot.com/2008/06/scala-to-java-smaller-inheritance.html">Java to Scala &#8211; Smaller Inheritance hierarchies with Structural Typing</a>.</p>

]]></content:encoded>
			<wfw:commentRss>http://www.colinhowe.co.uk/2010/06/03/rewriting-java-in-scala-making-code-lovely-5-%e2%80%93-structural-typing/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Rewriting Java in Scala &amp; Making Code Lovely 4 – Example: Integration</title>
		<link>http://www.colinhowe.co.uk/2010/05/27/rewriting-java-in-scala-making-code-lovely-4-%e2%80%93-example-integration/</link>
		<comments>http://www.colinhowe.co.uk/2010/05/27/rewriting-java-in-scala-making-code-lovely-4-%e2%80%93-example-integration/#comments</comments>
		<pubDate>Thu, 27 May 2010 12:50:50 +0000</pubDate>
		<dc:creator>Colin</dc:creator>
				<category><![CDATA[Coding / Tech]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[integration]]></category>
		<category><![CDATA[numerical approximation]]></category>
		<category><![CDATA[rectangle method]]></category>
		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://www.colinhowe.co.uk/?p=231</guid>
		<description><![CDATA[
It&#8217;s time for a full example that shows off what we know so far.
For the example, we&#8217;re going to implement something that does basic integration. This program will approximate the value of an integral using the rectangle method &#8211; a good explanation of this can be found here.
Background
Skip this if you already know what integration [...]]]></description>
			<content:encoded><![CDATA[
<p>It&#8217;s time for a full example that shows off what we know so far.</p>
<p>For the example, we&#8217;re going to implement something that does basic <a href="http://simple.wikipedia.org/wiki/Integral">integration</a>. This program will approximate the value of an integral using the rectangle method &#8211; a good explanation of this can be found <a href="http://www.intmath.com/Integration/3_Area-under-curve.php">here</a>.</p>
<h2>Background</h2>
<p>Skip this if you already know what integration and the rectangle method are.</p>
<p>Put simply, integration is finding the area under the graph for a particular function. The article <a href="http://simple.wikipedia.org/wiki/Integral">here</a> has more details on this.</p>
<p>The rectangle method is a way of approximating this. The graph for the function is divided into sections. Each section then has a rectangle put in it. The height of the rectangle is the same as the value of the graph at the middle of the section:<br />
<center><br />
<img src="http://colinhowe-colinhowe.s3.amazonaws.com/graph-1.png" alt="Thick rectangles under a graph" /><br />
</center><br />
This isn&#8217;t perfect. All the places where the rectangles are too small or too big are errors in the approximation. These errors can be reduced by dividing the graph in to more sections:<br />
<center><br />
<img src="http://colinhowe-colinhowe.s3.amazonaws.com/graph-2.png" alt="Thin rectangles under a graph" /><br />
</center></p>
<p>For the purposes of this posting, that&#8217;s all there is to it.</p>
<h2>The Problem</h2>
<blockquote><p>
Make a class that when given a function and a range will approximate the area under the graph. The class can be given the number of rectangles to use.
</p></blockquote>
<h3>Java Version</h3>
<p>We&#8217;ll start off with a Java version of this and then rewrite it to Scala.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> RectangleMethod <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">double</span> integrate<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">double</span> start, <span style="color: #000066; font-weight: bold;">double</span> end, <span style="color: #000066; font-weight: bold;">int</span> steps<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">double</span> stepSize <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>end <span style="color: #339933;">-</span> start<span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> steps<span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">double</span> value <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #008000; font-style: italic;">// running total for the area</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> steps<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #008000; font-style: italic;">// Evaluate the function at the mid-point of the current section</span>
      <span style="color: #008000; font-style: italic;">// The code below could be shorter - but it has been kept purposefully simple</span>
      <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">double</span> startX <span style="color: #339933;">=</span> start <span style="color: #339933;">+</span> stepSize <span style="color: #339933;">*</span> i<span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">double</span> endX <span style="color: #339933;">=</span> start <span style="color: #339933;">+</span> stepSize <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">double</span> midX <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>startX <span style="color: #339933;">+</span> endX<span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">double</span> y <span style="color: #339933;">=</span> function<span style="color: #009900;">&#40;</span>midX<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #008000; font-style: italic;">// Add on the area of the rectangle for the current section: width * height</span>
      value <span style="color: #339933;">+=</span> stepSize <span style="color: #339933;">*</span> y<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">return</span> value<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">double</span> function<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">double</span> x<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">20</span> <span style="color: #339933;">-</span> <span style="color: #003399;">Math</span>.<span style="color: #006633;">pow</span><span style="color: #009900;">&#40;</span>x <span style="color: #339933;">-</span> <span style="color: #cc66cc;">4</span>, <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The Java example above contains a hard-coded function: 20 &#8211; (x &#8211; 4) * (x &#8211; 4).</p>
<p>That&#8217;s not quite the solution to the problem. We must be able to provide a function to this method so that we can do integrations for any function. We can do this by making the integrate method take an interface and then the caller must implement this interface for any function it wants to integrate:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> RectangleMethod <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">double</span> integrate<span style="color: #009900;">&#40;</span>Function function, <span style="color: #000066; font-weight: bold;">double</span> start, <span style="color: #000066; font-weight: bold;">double</span> end, <span style="color: #000066; font-weight: bold;">int</span> steps<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">double</span> stepSize <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>end <span style="color: #339933;">-</span> start<span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> steps<span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">double</span> value <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> steps<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">double</span> startX <span style="color: #339933;">=</span> start <span style="color: #339933;">+</span> stepSize <span style="color: #339933;">*</span> i<span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">double</span> endX <span style="color: #339933;">=</span> start <span style="color: #339933;">+</span> stepSize <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">double</span> midX <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>startX <span style="color: #339933;">+</span> endX<span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">double</span> y <span style="color: #339933;">=</span> function.<span style="color: #006633;">y</span><span style="color: #009900;">&#40;</span>midX<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #008000; font-style: italic;">// Call the function passed in</span>
&nbsp;
      value <span style="color: #339933;">+=</span> stepSize <span style="color: #339933;">*</span> y<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">return</span> value<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #008000; font-style: italic;">// New interface for arbitrary functions</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> Function <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">double</span> y<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">double</span> x<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>I&#8217;ve commented the parts that have changed and removed the old comments.</p>
<p>This now lets us pass in any function we want:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">double</span> value <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> RectangleMethod<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">integrate</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Function<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">double</span> y<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">double</span> x<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">20</span> <span style="color: #339933;">-</span> <span style="color: #003399;">Math</span>.<span style="color: #006633;">pow</span><span style="color: #009900;">&#40;</span>x <span style="color: #339933;">-</span> <span style="color: #cc66cc;">4</span>, <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">200</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This will approximate the integral of 20 &#8211; (x &#8211; 4) * (x &#8211; 4) between 0 and 1 with 200 sections. This requires a lot of work to define a function every time you want to integrate it.</p>
<h3>Scala &#8211; Simple Version</h3>
<p>Now lets take our Scala knowledge so far and convert the above code. We&#8217;ll start with the simple Java version:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #003399; font-weight: bold;">class</span> RectangleMethod <span style="color: #009900;">&#123;</span>
  <span style="color: #003399; font-weight: bold;">def</span> integrate<span style="color: #009900;">&#40;</span>start <span style="color: #000080;">:</span> Double, end <span style="color: #000080;">:</span> Double, steps <span style="color: #000080;">:</span> Int<span style="color: #009900;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003399; font-weight: bold;">val</span> stepSize <span style="color: #000080;">=</span> <span style="color: #009900;">&#40;</span>end - start<span style="color: #009900;">&#41;</span> / steps <span style="color: #008000; font-style: italic;">// width of each step</span>
    <span style="color: #003399; font-weight: bold;">var</span> value <span style="color: #000080;">=</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #008000; font-style: italic;">// running total for the area</span>
&nbsp;
    <span style="color: #003399; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #000080;">&lt;</span>- <span style="color: #cc66cc;">0</span> until steps<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #008000; font-style: italic;">// Evaluate the function at the mid-point of the current section</span>
      <span style="color: #008000; font-style: italic;">// The code below could be shorter - but it has been kept purposefully simple</span>
      <span style="color: #003399; font-weight: bold;">val</span> startX <span style="color: #000080;">=</span> start + stepSize <span style="color: #000080;">*</span> i
      <span style="color: #003399; font-weight: bold;">val</span> endX <span style="color: #000080;">=</span> start + stepSize <span style="color: #000080;">*</span> <span style="color: #009900;">&#40;</span>i + <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>
      <span style="color: #003399; font-weight: bold;">val</span> midX <span style="color: #000080;">=</span> <span style="color: #009900;">&#40;</span>startX + endX<span style="color: #009900;">&#41;</span> / <span style="color: #cc66cc;">2</span>
      <span style="color: #003399; font-weight: bold;">val</span> y <span style="color: #000080;">=</span> function<span style="color: #009900;">&#40;</span>midX<span style="color: #009900;">&#41;</span>
&nbsp;
      <span style="color: #008000; font-style: italic;">// Add on the area of the rectangle for the current section: width * height</span>
      value <span style="color: #000080;">=</span> value + stepSize <span style="color: #000080;">*</span> y
    <span style="color: #009900;">&#125;</span>
&nbsp;
    value <span style="color: #008000; font-style: italic;">// Scala doesn't need the return keyword here</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #003399; font-weight: bold;">def</span> function<span style="color: #009900;">&#40;</span>x <span style="color: #000080;">:</span> Double<span style="color: #009900;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #cc66cc;">20</span> - Math.<span style="color: #000000;">pow</span><span style="color: #009900;">&#40;</span>x - <span style="color: #cc66cc;">4</span>, <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h4>For loops</h4>
<p>I&#8217;ve slipped something new in to the example above, the for loop. In Scala there are many ways to write a for loop, this is one of them. The for loop above will iterate i over the values 0 to steps and exclude steps. E.g.</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #003399; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #000080;">&lt;</span>- <span style="color: #cc66cc;">0</span> until <span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span> print<span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span></pre></div></div>

<p>Will print 0123456789. To make it include the final value change until to &#8220;to&#8221;:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #003399; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #000080;">&lt;</span>- <span style="color: #cc66cc;">0</span> to <span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span> print<span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span></pre></div></div>

<p>Will print 012345678910.</p>
<p>There are more ways to define for loops and I&#8217;ll come back to them in later posts.</p>
<h3>Scala &#8211; Better Version</h3>
<p>Now we use our knowledge of using functions as arguments to pass a function to the integrate method:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #003399; font-weight: bold;">class</span> RectangleMethod <span style="color: #009900;">&#123;</span>
  <span style="color: #003399; font-weight: bold;">def</span> integrate<span style="color: #009900;">&#40;</span>
      function <span style="color: #000080;">:</span> Double <span style="color: #000080;">=&gt;</span> Double, <span style="color: #008000; font-style: italic;">// Add the function argument on</span>
      start <span style="color: #000080;">:</span> Double, end <span style="color: #000080;">:</span> Double, steps <span style="color: #000080;">:</span> Int<span style="color: #009900;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003399; font-weight: bold;">val</span> stepSize <span style="color: #000080;">=</span> <span style="color: #009900;">&#40;</span>end - start<span style="color: #009900;">&#41;</span> / steps
    <span style="color: #003399; font-weight: bold;">var</span> value <span style="color: #000080;">=</span> <span style="color: #cc66cc;">0.0</span>
&nbsp;
    <span style="color: #003399; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #000080;">&lt;</span>- <span style="color: #cc66cc;">0</span> until steps<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #003399; font-weight: bold;">val</span> startX <span style="color: #000080;">=</span> start + stepSize <span style="color: #000080;">*</span> i
      <span style="color: #003399; font-weight: bold;">val</span> endX <span style="color: #000080;">=</span> start + stepSize <span style="color: #000080;">*</span> <span style="color: #009900;">&#40;</span>i + <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>
      <span style="color: #003399; font-weight: bold;">val</span> midX <span style="color: #000080;">=</span> <span style="color: #009900;">&#40;</span>startX + endX<span style="color: #009900;">&#41;</span> / <span style="color: #cc66cc;">2</span>
      <span style="color: #003399; font-weight: bold;">val</span> y <span style="color: #000080;">=</span> function<span style="color: #009900;">&#40;</span>midX<span style="color: #009900;">&#41;</span> <span style="color: #008000; font-style: italic;">// No need to change this!</span>
&nbsp;
      value <span style="color: #000080;">=</span> value + stepSize <span style="color: #000080;">*</span> y
    <span style="color: #009900;">&#125;</span>
&nbsp;
    value
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>We&#8217;ve added a single argument to the integrate function. The argument is a function that takes a Double as a parameter and returns a Double. To use this new method:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #003399; font-weight: bold;">new</span> RectangleMethod<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #000000;">integrate</span><span style="color: #009900;">&#40;</span>
  x <span style="color: #000080;">=&gt;</span> <span style="color: #cc66cc;">20</span> - Math.<span style="color: #000000;">pow</span><span style="color: #009900;">&#40;</span>x - <span style="color: #cc66cc;">4</span>, <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span>, 
  <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">200</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>I hope that you&#8217;ll agree that using this new version is much neater than the Java example. </p>
<h3>Scala &#8211; Tiny Version</h3>
<p>We can make the integrate method really compact and neat.</p>
<p>The first step is to merge the mid-point calculation:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #003399; font-weight: bold;">val</span> startX <span style="color: #000080;">=</span> start + stepSize <span style="color: #000080;">*</span> i
<span style="color: #003399; font-weight: bold;">val</span> endX <span style="color: #000080;">=</span> start + stepSize <span style="color: #000080;">*</span> <span style="color: #009900;">&#40;</span>i + <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>
<span style="color: #003399; font-weight: bold;">val</span> midX <span style="color: #000080;">=</span> <span style="color: #009900;">&#40;</span>startX + endX<span style="color: #009900;">&#41;</span> / <span style="color: #cc66cc;">2</span>
<span style="color: #008000; font-style: italic;">// Becomes (by substitution and simplification)...</span>
<span style="color: #003399; font-weight: bold;">val</span> midX <span style="color: #000080;">=</span> start + stepSize <span style="color: #000080;">*</span> i + stepSize <span style="color: #000080;">*</span> <span style="color: #cc66cc;">0.5</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>Then simply the calculation of y</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #003399; font-weight: bold;">val</span> midX <span style="color: #000080;">=</span> start + stepSize <span style="color: #000080;">*</span> i + stepSize <span style="color: #000080;">*</span> <span style="color: #cc66cc;">0.5</span><span style="color: #009900;">&#41;</span>
<span style="color: #003399; font-weight: bold;">val</span> y <span style="color: #000080;">=</span> function<span style="color: #009900;">&#40;</span>midX<span style="color: #009900;">&#41;</span>
<span style="color: #008000; font-style: italic;">// Becomes...</span>
<span style="color: #003399; font-weight: bold;">val</span> y <span style="color: #000080;">=</span> function<span style="color: #009900;">&#40;</span>start + stepSize <span style="color: #000080;">*</span> i + stepSize <span style="color: #000080;">*</span> <span style="color: #cc66cc;">0.5</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>And finally, move y into the running total calculation:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">value <span style="color: #000080;">=</span> value + stepSize <span style="color: #000080;">*</span> function<span style="color: #009900;">&#40;</span>start + stepSize <span style="color: #000080;">*</span> i + stepSize <span style="color: #000080;">*</span> <span style="color: #cc66cc;">0.5</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>This leaves us with:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #003399; font-weight: bold;">class</span> RectangleMethod <span style="color: #009900;">&#123;</span>
  <span style="color: #003399; font-weight: bold;">def</span> integrate<span style="color: #009900;">&#40;</span>
      function <span style="color: #000080;">:</span> Double <span style="color: #000080;">=&gt;</span> Double, 
      start <span style="color: #000080;">:</span> Double, end <span style="color: #000080;">:</span> Double, steps <span style="color: #000080;">:</span> Int<span style="color: #009900;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003399; font-weight: bold;">val</span> stepSize <span style="color: #000080;">=</span> <span style="color: #009900;">&#40;</span>end - start<span style="color: #009900;">&#41;</span> / steps
    <span style="color: #003399; font-weight: bold;">var</span> value <span style="color: #000080;">=</span> <span style="color: #cc66cc;">0.0</span>
&nbsp;
    <span style="color: #003399; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #000080;">&lt;</span>- <span style="color: #cc66cc;">0</span> until steps<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      value <span style="color: #000080;">=</span> value + stepSize <span style="color: #000080;">*</span> function<span style="color: #009900;">&#40;</span>start + stepSize <span style="color: #000080;">*</span> i + stepSize <span style="color: #000080;">*</span> <span style="color: #cc66cc;">0.5</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    value
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Whenever you see this pattern in Scala you should be thinking about whether it can be turned in to a call to foldLeft. The things to look for are:</p>
<ul>
<li>A collection to fold over &#8211; in this case (0 until steps) is actually a collection (I&#8217;ll cover this more in later posts)</li>
<li>A running total &#8211; in this case value is a running total</li>
</ul>
<p>Now we use foldLeft to make this more compact:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #003399; font-weight: bold;">class</span> RectangleMethod <span style="color: #009900;">&#123;</span>
  <span style="color: #008000; font-style: italic;">// Add the function argument on</span>
  <span style="color: #003399; font-weight: bold;">def</span> integrate<span style="color: #009900;">&#40;</span>
      function <span style="color: #000080;">:</span> Double <span style="color: #000080;">=&gt;</span> Double, 
      start <span style="color: #000080;">:</span> Double, end <span style="color: #000080;">:</span> Double, steps <span style="color: #000080;">:</span> Int<span style="color: #009900;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003399; font-weight: bold;">val</span> stepSize <span style="color: #000080;">=</span> <span style="color: #009900;">&#40;</span>end - start<span style="color: #009900;">&#41;</span> / steps
    <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span> until steps<span style="color: #009900;">&#41;</span>.<span style="color: #000000;">foldLeft</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0.0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>runningTotal, i<span style="color: #009900;">&#41;</span> <span style="color: #000080;">=&gt;</span>
      runningTotal + stepSize <span style="color: #000080;">*</span> function<span style="color: #009900;">&#40;</span>start + stepSize <span style="color: #000080;">*</span> i + stepSize <span style="color: #000080;">*</span> <span style="color: #cc66cc;">0.5</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>And, that&#8217;s it.</p>
<p>There are many other ways to rewrite this, and I would love for you to come up with your own and leave it here as a comment :)</p>

]]></content:encoded>
			<wfw:commentRss>http://www.colinhowe.co.uk/2010/05/27/rewriting-java-in-scala-making-code-lovely-4-%e2%80%93-example-integration/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Rewriting Java in Scala &amp; Making Code Lovely 3 – Variable Declarations</title>
		<link>http://www.colinhowe.co.uk/2010/05/22/rewriting-java-in-scala-making-code-lovely-3-%e2%80%93-variable-declarations/</link>
		<comments>http://www.colinhowe.co.uk/2010/05/22/rewriting-java-in-scala-making-code-lovely-3-%e2%80%93-variable-declarations/#comments</comments>
		<pubDate>Sat, 22 May 2010 15:31:23 +0000</pubDate>
		<dc:creator>Colin</dc:creator>
				<category><![CDATA[Coding / Tech]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://www.colinhowe.co.uk/?p=210</guid>
		<description><![CDATA[
I thought I&#8217;d take a step back and look at how variables are declared in Scala and how it compares with Java.
Local Variables
In Java you declare a local variable like this:

Map&#60;String, Integer&#62; userIds = new HashMap&#60;String, Integer&#62;&#40;&#41;;

In Scala, it is a lot shorter:

var userIds = new HashMap&#91;String, Integer&#93;

The type of the variable is missing. This [...]]]></description>
			<content:encoded><![CDATA[
<p>I thought I&#8217;d take a step back and look at how variables are declared in Scala and how it compares with Java.</p>
<h3>Local Variables</h3>
<p>In Java you declare a local variable like this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Map<span style="color: #339933;">&lt;</span>String, Integer<span style="color: #339933;">&gt;</span> userIds <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HashMap<span style="color: #339933;">&lt;</span>String, Integer<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>In Scala, it is a lot shorter:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #003399; font-weight: bold;">var</span> userIds <span style="color: #000080;">=</span> <span style="color: #003399; font-weight: bold;">new</span> HashMap<span style="color: #009900;">&#91;</span>String, Integer<span style="color: #009900;">&#93;</span></pre></div></div>

<p>The type of the variable is missing. This is because Scala can calculate the type. In this case it will calculate that userIds is of type HashMap[String, Integer]. The Scala site has <a href="http://www.scala-lang.org/node/127">more information</a> on how it calculates the types (this is called type inference).</p>
<p>Another minor detail here is that you use square brackets [ ] instead of angle brackets &lt; &gt; to specify generics.</p>
<h3>Constants</h3>
<p>In the above example we declared userIds with the keyword &#8216;var&#8217;. This makes it a variable &#8211; we can change userIds to point at a different HashMap if we want.</p>
<p>In Java we can say that a variable is constant using the final keyword:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> x <span style="color: #339933;">=</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">;</span>
x <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span> <span style="color: #008000; font-style: italic;">// Compile error: cannot change x</span></pre></div></div>

<p>Scala doesn&#8217;t have the final keyword when declaring variables. Instead, you use the &#8216;val&#8217; keyword:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #003399; font-weight: bold;">val</span> x <span style="color: #000080;">=</span> <span style="color: #cc66cc;">4</span>
x <span style="color: #000080;">=</span> <span style="color: #cc66cc;">5</span> <span style="color: #008000; font-style: italic;">// Compile error: cannot change x</span></pre></div></div>

<p>This has a benefit that isn&#8217;t obvious. </p>
<blockquote><p>Every time you declare a variable you specify whether you can change it or not.</p></blockquote>
<p>There are <a href="http://www.javapractices.com/topic/TopicAction.do?Id=29">many good reasons</a> to make something constant (or, immutable as many people like to say). However, in Java it is easy to forget to make something immutable, it is also a burden as you have to write more every time you want to keep something constant. In Scala, you make an active choice every time you declare a variable. It is also easy for constants to be the default that you use.</p>
<h3>Specifying Types</h3>
<p>Sometimes, you may want to specify the type of a variable. This is easy:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #003399; font-weight: bold;">val</span> x <span style="color: #000080;">:</span> Int <span style="color: #000080;">=</span> <span style="color: #cc66cc;">4</span>
<span style="color: #003399; font-weight: bold;">val</span> name <span style="color: #000080;">:</span> String <span style="color: #000080;">=</span> <span style="color: #6666FF;">&quot;Bob&quot;</span></pre></div></div>

<p>Normally, you won&#8217;t need to do this. Only when you have a specific type requirement or Scala has failed to infer the types &#8211; a rare event!</p>
<h3>Summary</h3>
<p>You&#8217;ve now been introduced to variable declarations. Scala improves this basic area of programming in a number of ways:</p>
<ul>
<li>Reduces repetitiveness by not writing types all the time</li>
<li>Makes the choice of constant/not constant an active choice</li>
<li>Makes it easier to alter types of variables</li>
</ul>
<p>The next part of this series will follow on from this and take a look at declaring fields in classes. </p>

]]></content:encoded>
			<wfw:commentRss>http://www.colinhowe.co.uk/2010/05/22/rewriting-java-in-scala-making-code-lovely-3-%e2%80%93-variable-declarations/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rewriting Java in Scala &amp; Making Code Lovely 2 – Functions as values</title>
		<link>http://www.colinhowe.co.uk/2010/05/15/rewriting-java-in-scala-making-code-lovely-2-%e2%80%93-functions-as-values/</link>
		<comments>http://www.colinhowe.co.uk/2010/05/15/rewriting-java-in-scala-making-code-lovely-2-%e2%80%93-functions-as-values/#comments</comments>
		<pubDate>Sat, 15 May 2010 09:10:39 +0000</pubDate>
		<dc:creator>Colin</dc:creator>
				<category><![CDATA[Coding / Tech]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[function literals]]></category>
		<category><![CDATA[functional programming]]></category>
		<category><![CDATA[functions]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[placeholder syntax]]></category>
		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://www.colinhowe.co.uk/?p=182</guid>
		<description><![CDATA[
Ever wanted to filter a collection in Java and thought &#8220;this sucks&#8221;? Typically, you end up with code like:

List evens = new LinkedList&#40;&#41;;
for &#40;Integer i : list&#41; &#123;
  if &#40;i % 2 == 0&#41; evens.add&#40;i&#41;;
&#125;

Or, if you&#8217;re going for re-use:

Predicate evenNumbers = new Predicate&#40;&#41; &#123;
  @Override
  public boolean evaluate&#40;Object o&#41; &#123;
  [...]]]></description>
			<content:encoded><![CDATA[
<p>Ever wanted to filter a collection in Java and thought &#8220;this sucks&#8221;? Typically, you end up with code like:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">List</span> evens <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">LinkedList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Integer</span> i <span style="color: #339933;">:</span> list<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">%</span> <span style="color: #cc66cc;">2</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> evens.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Or, if you&#8217;re going for re-use:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Predicate evenNumbers <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Predicate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  @Override
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> evaluate<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> o<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003399;">Integer</span> n <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Integer</span><span style="color: #009900;">&#41;</span>o<span style="color: #339933;">;</span> <span style="color: #008000; font-style: italic;">// Assume list contains only integers</span>
    <span style="color: #000000; font-weight: bold;">return</span> n <span style="color: #339933;">%</span> <span style="color: #cc66cc;">2</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
CollectionUtils.<span style="color: #006633;">filter</span><span style="color: #009900;">&#40;</span>list, evenNumbers<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Which is actually longer.</p>
<p>In Scala, functions can be values and this makes the above examples become very short:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #003399; font-weight: bold;">val</span> evens <span style="color: #000080;">=</span> list.<span style="color: #000000;">filter</span><span style="color: #009900;">&#40;</span><span style="color: #000080;">_</span> <span style="color: #000080;">%</span> <span style="color: #cc66cc;">2</span> <span style="color: #000080;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>You&#8217;re not supposed to understand this yet &#8211; it&#8217;s the bait to get you to read further ;-)</p>
<h3>Defining Functions in Scala</h3>
<p>Before we focus on this I&#8217;m going to quickly show you how functions are defined in Scala:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #003399; font-weight: bold;">def</span> getHelloString<span style="color: #009900;">&#40;</span>username <span style="color: #000080;">:</span> String<span style="color: #009900;">&#41;</span> <span style="color: #000080;">:</span> String <span style="color: #000080;">=</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #6666FF;">&quot;Hello &quot;</span> + username
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This defines a function called getHelloString. It takes a single argument of type String called username and returns a String. Unlike Java, the type of an argument goes after the name. Likewise, the return type of a function goes after the argument list. It&#8217;s also worth noting that there isn&#8217;t a return statement here, the value of the last expression in the function is the return value.</p>
<p>That&#8217;s all I&#8217;m going to say on functions for now &#8211; I&#8217;ll come back to them in more detail in a later post.</p>
<h3>Functions as Arguments</h3>
<p>Let&#8217;s take a look at the signature for the method filter:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #003399; font-weight: bold;">def</span> filter<span style="color: #009900;">&#40;</span>p <span style="color: #000080;">:</span> <span style="color: #009900;">&#40;</span>A<span style="color: #009900;">&#41;</span> <span style="color: #000080;">=&gt;</span> Boolean<span style="color: #009900;">&#41;</span> <span style="color: #000080;">:</span> Iterable<span style="color: #009900;">&#91;</span>A<span style="color: #009900;">&#93;</span></pre></div></div>

<p>This method takes an argument p and returns an Iterable[A] (the [A] is how you specify generics in Scala but we can ignore this for now).</p>
<p>The argument p is the interesting bit. The type of p is a function that takes an A (generics again) and returns a Boolean. To help this make sense here are a few more examples:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #003399; font-weight: bold;">def</span> a<span style="color: #009900;">&#40;</span>p <span style="color: #000080;">:</span> String <span style="color: #000080;">=&gt;</span> Boolean<span style="color: #009900;">&#41;</span> <span style="color: #000080;">:</span> Boolean
<span style="color: #008000; font-style: italic;">// The type of p is a function that takes a String and returns a Boolean</span>
&nbsp;
<span style="color: #003399; font-weight: bold;">def</span> b<span style="color: #009900;">&#40;</span>p <span style="color: #000080;">:</span> String <span style="color: #000080;">=&gt;</span> List<span style="color: #009900;">&#91;</span>String<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #000080;">:</span> Boolean
<span style="color: #008000; font-style: italic;">// The type of p is a function that takes a String and returns a list of strings</span>
&nbsp;
<span style="color: #003399; font-weight: bold;">def</span> c<span style="color: #009900;">&#40;</span>p <span style="color: #000080;">:</span> <span style="color: #009900;">&#40;</span>String, Boolean<span style="color: #009900;">&#41;</span> <span style="color: #000080;">=&gt;</span> Int<span style="color: #009900;">&#41;</span> <span style="color: #000080;">:</span> Boolean
<span style="color: #008000; font-style: italic;">// The type of p is a function that takes a String and a boolean and returns a list of strings</span></pre></div></div>

<p>This is very different to Java. In Java you would define an interface and use that as the argument type (just like in the CollectionUtils example above).</p>
<p>Coming back to filter, we now know that it takes a single argument. That argument must be a function which takes a single argument of type A and returns a boolean. What is A? A is the same type as the type of the list we are filtering over. So:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">List<span style="color: #009900;">&#40;</span><span style="color: #6666FF;">&quot;A&quot;</span>, <span style="color: #6666FF;">&quot;B&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #000000;">filter</span><span style="color: #009900;">&#40;</span>...<span style="color: #009900;">&#41;</span>
<span style="color: #008000; font-style: italic;">// requires a function that takes a String and returns a Boolean</span>
&nbsp;
List<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span>.<span style="color: #000000;">filter</span><span style="color: #009900;">&#40;</span>...<span style="color: #009900;">&#41;</span>
<span style="color: #008000; font-style: italic;">// requires a function that takes an Int and returns a Boolean</span></pre></div></div>

<p>With the knowledge we have so far we could comfortably write:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #003399; font-weight: bold;">def</span> isEven<span style="color: #009900;">&#40;</span>n <span style="color: #000080;">:</span> Int<span style="color: #009900;">&#41;</span> <span style="color: #000080;">:</span> Boolean <span style="color: #000080;">=</span> <span style="color: #009900;">&#123;</span>
  n <span style="color: #000080;">%</span> <span style="color: #cc66cc;">2</span> <span style="color: #000080;">==</span> <span style="color: #cc66cc;">0</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
List<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">4</span>, <span style="color: #cc66cc;">5</span>, <span style="color: #cc66cc;">6</span><span style="color: #009900;">&#41;</span>.<span style="color: #000000;">filter</span><span style="color: #009900;">&#40;</span>isEven<span style="color: #009900;">&#41;</span></pre></div></div>

<p>And we&#8217;ll get a list containing only the even numbers.</p>
<h3>Function Literals</h3>
<p>This is all very well and good, but it&#8217;s still a lot to write &#8211; especially if we only ever use the isEven function in one place.</p>
<p>Fortunately, Scala offers a short-hand that allows us to write functions in-line:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">List<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">4</span>, <span style="color: #cc66cc;">5</span>, <span style="color: #cc66cc;">6</span><span style="color: #009900;">&#41;</span>.<span style="color: #000000;">filter</span><span style="color: #009900;">&#40;</span>n <span style="color: #000080;">=&gt;</span> n <span style="color: #000080;">%</span> <span style="color: #cc66cc;">2</span> <span style="color: #000080;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>The way this is written above is very similar to the argument types above. The argument passed to filter here is known as a function literal.</p>
<h3>Placeholder Syntax</h3>
<p>Scala gives us one further refinement to this:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">List<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">4</span>, <span style="color: #cc66cc;">5</span>, <span style="color: #cc66cc;">6</span><span style="color: #009900;">&#41;</span>.<span style="color: #000000;">filter</span><span style="color: #009900;">&#40;</span><span style="color: #000080;">_</span> <span style="color: #000080;">%</span> <span style="color: #cc66cc;">2</span> <span style="color: #000080;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>On first reading this can be cryptic. It&#8217;s called placeholder syntax. The _ is a placeholder for the 1st argument to the function. When Scala sees this it replaces the _ with the 1st argument to the function. Each subsequent _ is a placeholder for the next argument. If we wrote:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">dostuff<span style="color: #009900;">&#40;</span><span style="color: #000080;">_</span> + <span style="color: #000080;">_</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>This would be the same as:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">dostuff<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>a, b<span style="color: #009900;">&#41;</span> <span style="color: #000080;">=&gt;</span> a + b<span style="color: #009900;">&#41;</span></pre></div></div>

<p>It&#8217;s important to note that when writing function literals with the arguments named parentheses must be used to group the arguments. This is to prevent ambiguity:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">dostuff<span style="color: #009900;">&#40;</span>a, b <span style="color: #000080;">=&gt;</span> a + b<span style="color: #009900;">&#41;</span></pre></div></div>

<p>Could be read as:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">dostuff<span style="color: #009900;">&#40;</span>
  a,
  b <span style="color: #000080;">=&gt;</span> a + b<span style="color: #009900;">&#41;</span></pre></div></div>

<p>Which isn&#8217;t what we intended.</p>
<p>I learn best by example, so here are a few more examples of function literals.</p>
<h3>Further Examples</h3>
<h4>Map</h4>
<p>Map is one of the most useful functions in Scala&#8217;s collections:</p>
<p>Map creates a new collection based on the original. Each element in the new collection is the result of applying the function given to each element in the original collection.</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">List<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span>.<span style="color: #000000;">map</span><span style="color: #009900;">&#40;</span><span style="color: #000080;">_</span> <span style="color: #000080;">*</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span>
<span style="color: #008000; font-style: italic;">// Returns: List(2, 4, 6, 8)</span>
&nbsp;
List<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span>.<span style="color: #000000;">map</span><span style="color: #009900;">&#40;</span><span style="color: #000080;">_</span>.<span style="color: #000000;">toString</span><span style="color: #009900;">&#41;</span>
<span style="color: #008000; font-style: italic;">// Returns: List(&quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;4&quot;)</span></pre></div></div>

<h4>findIndexOf</h4>
<p>Finding the index of an element in a list is also very easy:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">List<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span>.<span style="color: #000000;">findIndexOf</span><span style="color: #009900;">&#40;</span><span style="color: #000080;">_</span> <span style="color: #000080;">%</span> <span style="color: #cc66cc;">2</span> <span style="color: #000080;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
<span style="color: #008000; font-style: italic;">// Finds the index of the first even number, or -1 if no such number exists</span></pre></div></div>

<h4>Count</h4>
<p>How about counting the number of even numbers in a list?</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">List<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span>.<span style="color: #000000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000080;">_</span> <span style="color: #000080;">%</span> <span style="color: #cc66cc;">2</span> <span style="color: #000080;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
<span style="color: #008000; font-style: italic;">// Returns 2</span>
&nbsp;
List<span style="color: #009900;">&#40;</span><span style="color: #6666FF;">&quot;Hi&quot;</span>, <span style="color: #6666FF;">&quot;Bye&quot;</span>, <span style="color: #6666FF;">&quot;Hey&quot;</span>, <span style="color: #6666FF;">&quot;Goodbye&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #000000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000080;">_</span>.<span style="color: #000000;">length</span> <span style="color: #000080;">==</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span>
<span style="color: #008000; font-style: italic;">// Returns 1</span></pre></div></div>

<h3>What’s this given us?</h3>
<h4>More concise, more readable</h4>
<p>Goodbye anonymous inner classes. If you&#8217;ve ever written something that fires events you&#8217;ll know how horrible these can be.</p>
<p>Once I got used to passing functions around as arguments I soon realised how often I can re-use the same basic operations. Map and filter are two of the most used constructs in Java &#8211; I love that I can now write them in one line.</p>
<h4>Easier to debug</h4>
<p>The great thing about using functions as arguments is that you can write an algorithm once, and never again. I don&#8217;t want to think about how many times I&#8217;ve written a basic loop in Java and forgotten to deal with the last case, off-by-one errors, etc.</p>
<h4>Easier to test</h4>
<p>Now that I can extract out algorithms more effectively I can test them to hell and back. After that, I only have to test that I&#8217;m calling them correctly &#8211; I don&#8217;t have to retest all the corner cases all over again.</p>
<h3>Summary</h3>
<p>You should now understand:</p>
<ul>
<li>Functions can be passed as arguments to other functions</li>
<li>Functions can be written in-line as literal functions</li>
<li>Literal functions can be extremely short when using placeholder syntax</li>
</ul>
<p>If you don&#8217;t, don&#8217;t worry! If you&#8217;ve not come across functional programming before it can be quite a brain melting experience. My advice is to grab the Scala REPL and put in some of the examples above&#8230; then experiment and have fun!</p>

]]></content:encoded>
			<wfw:commentRss>http://www.colinhowe.co.uk/2010/05/15/rewriting-java-in-scala-making-code-lovely-2-%e2%80%93-functions-as-values/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Rewriting Java in Scala &amp; Making Code Lovely 1 &#8211; Case Classes</title>
		<link>http://www.colinhowe.co.uk/2010/05/09/rewriting-java-in-scala-making-code-lovely-1-case-classes/</link>
		<comments>http://www.colinhowe.co.uk/2010/05/09/rewriting-java-in-scala-making-code-lovely-1-case-classes/#comments</comments>
		<pubDate>Sun, 09 May 2010 21:09:02 +0000</pubDate>
		<dc:creator>Colin</dc:creator>
				<category><![CDATA[Coding / Tech]]></category>
		<category><![CDATA[Scala Explorations]]></category>
		<category><![CDATA[case classes]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://www.colinhowe.co.uk/?p=174</guid>
		<description><![CDATA[
This is the first in a series of posts for people who know Java and are learning Scala. Apart from teaching Scala I hope these posts show how Scala makes your code lovely&#8230;
What does more lovely mean?
Lovely covers several things:

More concise
More readable
Easier to debug
Easier to test
Easier to maintain

I&#8217;m not saying Scala is a silver bullet [...]]]></description>
			<content:encoded><![CDATA[
<p>This is the first in a series of posts for people who know Java and are learning Scala. Apart from teaching Scala I hope these posts show how Scala makes your code lovely&#8230;</p>
<h4>What does <i>more lovely</i> mean?</h4>
<p>Lovely covers several things:</p>
<ul>
<li>More concise</li>
<li>More readable</li>
<li>Easier to debug</li>
<li>Easier to test</li>
<li>Easier to maintain</li>
</ul>
<p>I&#8217;m not saying Scala is a silver bullet &#8211; I&#8217;m saying it gives you more time and support to get the hard parts right.</p>
<p>Anyway, on with the show. First up:</p>
<h3>Case Classes</h3>
<p>Case classes in Scala look something like:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #003399; font-weight: bold;">case</span> <span style="color: #003399; font-weight: bold;">class</span> Employee<span style="color: #009900;">&#40;</span>name <span style="color: #000080;">:</span> String<span style="color: #009900;">&#41;</span></pre></div></div>

<p>They are a short-hand for defining simple classes. They provide you with default implementations of equals, toString and hashCode. They also provide you with some handy tricks when doing pattern matching (which I&#8217;ll talk about in a future post).</p>
<p>The great benefit of case classes is that you can take a Java class such as:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> <span style="color: #003399;">Point</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> x<span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> y<span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Point</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> x, <span style="color: #000066; font-weight: bold;">int</span> y<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">x</span> <span style="color: #339933;">=</span> x<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">y</span> <span style="color: #339933;">=</span> y<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;Point(x: &quot;</span> <span style="color: #339933;">+</span> x <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;, y: &quot;</span> <span style="color: #339933;">+</span> y <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;)&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  @Override
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> equals<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> obj<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #009900;">&#40;</span>obj <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #003399;">Point</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #003399;">Point</span> other <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Point</span><span style="color: #009900;">&#41;</span>obj<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">return</span> other.<span style="color: #006633;">x</span> <span style="color: #339933;">==</span> x <span style="color: #339933;">&amp;&amp;</span> other.<span style="color: #006633;">y</span> <span style="color: #339933;">==</span> y<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  @Override
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> hashCode<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> x <span style="color: #339933;">*</span> <span style="color: #cc66cc;">17</span> <span style="color: #339933;">+</span> y<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>and rewrite as</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #003399; font-weight: bold;">case</span> <span style="color: #003399; font-weight: bold;">class</span> Point<span style="color: #009900;">&#40;</span>x <span style="color: #000080;">:</span> Int, y <span style="color: #000080;">:</span> Int<span style="color: #009900;">&#41;</span></pre></div></div>

<p>That&#8217;s it. </p>
<p>No, really, that&#8217;s it.</p>
<p>You can provide different implementations of toString, equals and hashCode if you really want to. But the majority of the time the default implementations are spot on.</p>
<h3>What&#8217;s this given us?</h3>
<h4>More concise, more readable</h4>
<p>It&#8217;s a one liner. But, it&#8217;s not a Perl one liner ;-)</p>
<h4>Easier to debug</h4>
<p>There isn&#8217;t any functionality here to debug &#8211; but the standard toString makes debugging code that uses this class far easier. The default toString will result in something like:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">Point<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span> 
<span style="color: #008000; font-style: italic;">// The order here is the same as the order in the case class definition</span></pre></div></div>

<p>Isn&#8217;t this nicer than &#8220;Point@0&#215;02345678&#8243; as a default?</p>
<h4>Easier to test</h4>
<p>There&#8217;s nothing to test. It&#8217;s reasonable to assume that Scala gets the default toString and equality methods correct &#8211; so why test them again?</p>
<h4>Easier to maintain</h4>
<p>Future maintainers don&#8217;t have to look at the toString, equals and hashCode methods when they familiarise themselves with the class. As they don&#8217;t exist, they can&#8217;t introduce bugs there either!</p>
<h3>Summary</h3>
<p>Case classes are a great way of defining simple and useful classes with very little code. Goodbye huge files for simple POJOs.</p>

]]></content:encoded>
			<wfw:commentRss>http://www.colinhowe.co.uk/2010/05/09/rewriting-java-in-scala-making-code-lovely-1-case-classes/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Introducing Gleam</title>
		<link>http://www.colinhowe.co.uk/2010/04/26/introducing-gleam/</link>
		<comments>http://www.colinhowe.co.uk/2010/04/26/introducing-gleam/#comments</comments>
		<pubDate>Mon, 26 Apr 2010 21:02:06 +0000</pubDate>
		<dc:creator>Colin</dc:creator>
				<category><![CDATA[Coding / Tech]]></category>
		<category><![CDATA[Gleam]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[front end development]]></category>
		<category><![CDATA[language design]]></category>
		<category><![CDATA[scala]]></category>
		<category><![CDATA[view layer]]></category>

		<guid isPermaLink="false">http://www.colinhowe.co.uk/?p=166</guid>
		<description><![CDATA[
Over the past few months I have been working on a new programming language for the JVM. It&#8217;s called Gleam and its purpose is to make view layer programming for web applications easier.
You can read more on the approach taken and the problems it is trying to solve here and here.
I have created a site [...]]]></description>
			<content:encoded><![CDATA[
<p>Over the past few months I have been working on a new programming language for the JVM. It&#8217;s called Gleam and its purpose is to make view layer programming for web applications easier.</p>
<p>You can read more on the approach taken and the problems it is trying to solve <a href="http://www.colinhowe.co.uk/2010/04/14/html-machine-code-for-web-pages/">here</a> and <a href="http://www.colinhowe.co.uk/2010/04/16/benefits-of-working-in-a-higher-level-for-web-pages/">here</a>.</p>
<p>I have created a site for Gleam (written using Gleam) and it is located at <a href="http://www.gleam-lang.com">http://www.gleam-lang.com</a>.</p>
<p>The source for Gleam is on github at <a href="http://github.com/colinhowe/gleam">http://github.com/gleam</a>. The compiler is written in Scala and is gradually getting completely rewritten now that I&#8217;ve firmed up the design (read: I&#8217;m not proud of the code itself, just what it does).</p>
<p>So, without any further ado, <a href="http://www.gleam-lang.com">have a play</a>!</p>
<h3>Feedback</h3>
<p>I&#8217;d love to hear any feedback or questions on this! Please comment, twitter, e-mail me and I&#8217;ll get back to you :)</p>

]]></content:encoded>
			<wfw:commentRss>http://www.colinhowe.co.uk/2010/04/26/introducing-gleam/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Language Wish-List: Add Information to Exceptions</title>
		<link>http://www.colinhowe.co.uk/2010/04/26/language-wish-list-add-information-to-exceptions/</link>
		<comments>http://www.colinhowe.co.uk/2010/04/26/language-wish-list-add-information-to-exceptions/#comments</comments>
		<pubDate>Mon, 26 Apr 2010 17:17:38 +0000</pubDate>
		<dc:creator>Colin</dc:creator>
				<category><![CDATA[Coding / Tech]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[language design]]></category>
		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://www.colinhowe.co.uk/?p=163</guid>
		<description><![CDATA[
Following a discussion over on David MacIver&#8217;s blog. I have a piece of functionality I&#8217;d like to see in more languages.
The ability to easily add information to exceptions

try &#123;
  // ...
&#125; catch &#40;Exception e&#41; &#123;
  e.addInformation&#40;&#34;serverName&#34;, serverName&#41;;
  throw e;
&#125;

Or, even better, a way to annotate variables to get this for free/cheap:

@PutInExceptions String [...]]]></description>
			<content:encoded><![CDATA[
<p>Following a discussion over on <a href="http://www.drmaciver.com/2010/04/the-best-way-to-handle-exceptions">David MacIver&#8217;s blog</a>. I have a piece of functionality I&#8217;d like to see in more languages.</p>
<h3>The ability to easily add information to exceptions</h3>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #008000; font-style: italic;">// ...</span>
<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  e.<span style="color: #006633;">addInformation</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;serverName&quot;</span>, serverName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">throw</span> e<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Or, even better, a way to annotate variables to get this for free/cheap:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@PutInExceptions <span style="color: #003399;">String</span> serverName <span style="color: #339933;">=</span> config.<span style="color: #006633;">getServerName</span><span style="color: #339933;">;</span></pre></div></div>

<p>The advantage of being able to add this information is that it makes it easier to diagnose exceptions without losing the type of the exception. Often, when we see an exception we get a full stack trace telling us the line that caused it but not enough surrounding information to make replication easy. Consider the following:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> User getUserDetails<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> id<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>cache.<span style="color: #006633;">contains</span><span style="color: #009900;">&#40;</span>User.<span style="color: #000000; font-weight: bold;">class</span>, id<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #008000; font-style: italic;">// Populate the cache</span>
  <span style="color: #009900;">&#125;</span>
  User user <span style="color: #339933;">=</span> cache.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>User.<span style="color: #000000; font-weight: bold;">class</span>, id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  log<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Got user with name [&quot;</span><span style="color: #339933;">+</span>user.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;]&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">return</span> user<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>If this blows up on the log line then all we know is that user is null. We don&#8217;t know the id that was used. This can make debugging harder. </p>
<p>Having the ability to annotate things as &#8220;add this to any exception that occurs&#8221; makes it easy and cheap to get good error handling. If you then want a method to be ultra-fast then you don&#8217;t add the annotations &#8211; simple.</p>

]]></content:encoded>
			<wfw:commentRss>http://www.colinhowe.co.uk/2010/04/26/language-wish-list-add-information-to-exceptions/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
