<?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>Monomorphic &#187; code</title>
	<atom:link href="http://www.monomorphic.org/wordpress/tag/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.monomorphic.org/wordpress</link>
	<description>Nystrom re-presents</description>
	<lastBuildDate>Tue, 27 Jul 2010 02:12:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Why Scala? (2) Compact syntax applied to probabilistic actions</title>
		<link>http://www.monomorphic.org/wordpress/why-scala-2-compact-syntax-applied-to-probabilistic-actions/</link>
		<comments>http://www.monomorphic.org/wordpress/why-scala-2-compact-syntax-applied-to-probabilistic-actions/#comments</comments>
		<pubDate>Mon, 26 Jul 2010 22:13:36 +0000</pubDate>
		<dc:creator>johan</dc:creator>
				<category><![CDATA[Software development]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://www.monomorphic.org/wordpress/?p=671</guid>
		<description><![CDATA[As a little fun project, I developed some probabilistic cellular automata with Scala and very basic AWT graphics. I continue to become more proficient with Scala, and it feels increasingly natural to use. During this exercise I came up with something that I thought was particularly elegant, and that I am pretty sure would have [...]]]></description>
			<content:encoded><![CDATA[<p>As a little fun project, I developed some probabilistic cellular automata with Scala and very basic AWT graphics. I continue to become more proficient with Scala, and it feels increasingly natural to use. During this exercise I came up with something that I thought was particularly elegant, and that I am pretty sure would have been a lot less readable in Java. I will just reproduce the interesting bits. The basic idea is that I want cells on a 2D grid to take certain redrawing actions according to given probabilities. First, I define this utility function:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">object</span> Util <span style="color: #F78811;">&#123;</span>
&nbsp;
	<span style="color: #008000; font-style: italic;">//sum of floats (probabilities) should be at most 1.0</span>
	<span style="color: #0000ff; font-weight: bold;">def</span> multiAction<span style="color: #F78811;">&#40;</span>acts<span style="color: #000080;">:</span> Seq<span style="color: #F78811;">&#91;</span>Tuple2<span style="color: #F78811;">&#91;</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">=&gt;</span> Unit, Float<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
		<span style="color: #0000ff; font-weight: bold;">val</span> r <span style="color: #000080;">=</span> Math.<span style="color: #000000;">random</span>
		<span style="color: #0000ff; font-weight: bold;">var</span> soFar<span style="color: #000080;">:</span> Float <span style="color: #000080;">=</span> <span style="color: #F78811;">0</span>
		<span style="color: #0000ff; font-weight: bold;">var</span> acted <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">false</span>
		<span style="color: #0000ff; font-weight: bold;">for</span> <span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#40;</span>act,prob<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">&lt;</span>- acts<span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
			soFar +<span style="color: #000080;">=</span> prob
&nbsp;
			<span style="color: #0000ff; font-weight: bold;">if</span> <span style="color: #F78811;">&#40;</span>soFar <span style="color: #000080;">&gt;</span> r <span style="color: #000080;">&amp;&amp;</span> <span style="color: #000080;">!</span>acted<span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
				act<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>	
				acted <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">true</span>
			<span style="color: #F78811;">&#125;</span>
		<span style="color: #F78811;">&#125;</span>
	<span style="color: #F78811;">&#125;</span>
&nbsp;
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>The idea here is that we supply a list of tuples. The first element of each tuple is a function, and the second element is a float value between 0 and 1 representing the probability that each function is evaluated. Only one of the functions supplied is actually evaluated.</p>
<p>This is how I put it to use (excerpt from another class):</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">Util.<span style="color: #000000;">multiAction</span><span style="color: #F78811;">&#40;</span>
  List<span style="color: #F78811;">&#40;</span> 
  <span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=&gt;</span> <span style="color: #F78811;">&#123;</span>
     cellsWrite<span style="color: #F78811;">&#40;</span>x, y-<span style="color: #F78811;">1</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> cellsRead<span style="color: #F78811;">&#40;</span>x, y-<span style="color: #F78811;">1</span><span style="color: #F78811;">&#41;</span> + 0.01f<span style="color: #000080;">;</span>
     cellsWrite<span style="color: #F78811;">&#40;</span>x, y+<span style="color: #F78811;">1</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> cellsRead<span style="color: #F78811;">&#40;</span>x, y+<span style="color: #F78811;">1</span><span style="color: #F78811;">&#41;</span> + 0.01f 
    <span style="color: #F78811;">&#125;</span>, 0.2f<span style="color: #F78811;">&#41;</span>,
  <span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=&gt;</span> <span style="color: #F78811;">&#123;</span> 
     cellsWrite<span style="color: #F78811;">&#40;</span>x+<span style="color: #F78811;">1</span>, y<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> cellsRead<span style="color: #F78811;">&#40;</span>x+<span style="color: #F78811;">1</span>, y<span style="color: #F78811;">&#41;</span> + 0.01f<span style="color: #000080;">;</span>
     cellsWrite<span style="color: #F78811;">&#40;</span>x-<span style="color: #F78811;">1</span>, y<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> cellsRead<span style="color: #F78811;">&#40;</span>x-<span style="color: #F78811;">1</span>, y<span style="color: #F78811;">&#41;</span> + 0.01f 
    <span style="color: #F78811;">&#125;</span>, 0.1f<span style="color: #F78811;">&#41;</span>
 <span style="color: #F78811;">&#41;</span>
<span style="color: #F78811;">&#41;</span></pre></div></div>

<p>Once you take in the braces here it is actually quite simple. We have two anonymous functions taking zero parameters of two statements each. The first one has probability 0.2 and the second probability 0.1, meaning that there&#8217;s a 70% probability that nothing will happen. We can also make an arbitrarily long list of such functions on the fly.</p>
<p>To the best of my knowledge, the only way of reproducing this flexibility in Java would be to create anonymous inner classes and put them inside an array. Certainly that would be quite a bit more verbose than this.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.monomorphic.org/wordpress/why-scala-2-compact-syntax-applied-to-probabilistic-actions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Why Scala? The mixing of imperative and functional style</title>
		<link>http://www.monomorphic.org/wordpress/why-scala-the-mixing-of-imperative-and-functional-style/</link>
		<comments>http://www.monomorphic.org/wordpress/why-scala-the-mixing-of-imperative-and-functional-style/#comments</comments>
		<pubDate>Thu, 11 Feb 2010 04:15:29 +0000</pubDate>
		<dc:creator>johan</dc:creator>
				<category><![CDATA[Software development]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://www.monomorphic.org/wordpress/?p=519</guid>
		<description><![CDATA[Scala is a little wonderland sprinkled with useful things you can mix and match as you like to improve your coding experience while staying on the Java platform. The Option classes, the structural case matching, the compact declarations, lazy evaluation&#8230; the list goes on. But at the heart of it is the decision to mix [...]]]></description>
			<content:encoded><![CDATA[<p>Scala is a little wonderland sprinkled with useful things you can mix and match as you like to improve your coding experience while staying on the Java platform. The Option classes, the structural case matching, the compact declarations, lazy evaluation&#8230; the list goes on. But at the heart of it is the decision to mix freely the functional and imperative programming styles.</p>
<p>How does this work in practice?</p>
<ul>
<li>Statements can have side effects, like in Java</li>
<li>The final statement evaluated in a function is its return value by default</li>
<li>Every statement evaluates to a value, even control flow statements like if&#8230; else, unlike in Java</li>
</ul>
<p>The bottom line is that some problems call for a functional programming style, and others for an imperative one. Scala doesn&#8217;t force you into a mold, it just gives you what you need to express what you&#8217;d like to express. This can lead to very compact code. Here&#8217;s a function that recursively finds all files ending in .java starting in a given directory. The File class here is the standard Java java.io.File!</p>
<p>Remember, the last expression evaluated is the return value.</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"> <span style="color: #0000ff; font-weight: bold;">def</span> findJavaFiles<span style="color: #F78811;">&#40;</span>dir<span style="color: #000080;">:</span> File<span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> List<span style="color: #F78811;">&#91;</span>File<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> files <span style="color: #000080;">=</span> dir.<span style="color: #000000;">listFiles</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> javaFiles <span style="color: #000080;">=</span> files.<span style="color: #000000;">filter</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#123;</span><span style="color: #000080;">_</span>.<span style="color: #000000;">getName</span>.<span style="color: #000000;">endsWith</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;.java&quot;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#125;</span><span style="color: #F78811;">&#41;</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> dirs <span style="color: #000080;">=</span> files.<span style="color: #000000;">filter</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#123;</span><span style="color: #000080;">_</span>.<span style="color: #000000;">isDirectory</span><span style="color: #F78811;">&#125;</span><span style="color: #F78811;">&#41;</span>
    javaFiles.<span style="color: #000000;">toList</span> ++ dirs.<span style="color: #000000;">flatMap</span><span style="color: #F78811;">&#123;</span>findJavaFiles<span style="color: #F78811;">&#40;</span><span style="color: #000080;">_</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#125;</span>
  <span style="color: #F78811;">&#125;</span></pre></div></div>

<p>But we can write it even more compactly at the expense of some clarity:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"> <span style="color: #0000ff; font-weight: bold;">def</span> findJavaFiles<span style="color: #F78811;">&#40;</span>dir<span style="color: #000080;">:</span> File<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> files <span style="color: #000080;">=</span> dir.<span style="color: #000000;">listFiles</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>
    files.<span style="color: #000000;">filter</span><span style="color: #F78811;">&#40;</span><span style="color: #000080;">_</span>.<span style="color: #000000;">getName</span>.<span style="color: #000000;">endsWith</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;.java&quot;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">toList</span> ++
 files.<span style="color: #000000;">filter</span><span style="color: #F78811;">&#40;</span><span style="color: #000080;">_</span>.<span style="color: #000000;">isDirectory</span><span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">flatMap</span><span style="color: #F78811;">&#123;</span>findJavaFiles<span style="color: #F78811;">&#40;</span><span style="color: #000080;">_</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#125;</span>
  <span style="color: #F78811;">&#125;</span></pre></div></div>

<p>Now write this function in Java and see how many lines you end up with.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.monomorphic.org/wordpress/why-scala-the-mixing-of-imperative-and-functional-style/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>An immutable MultiMap for Scala</title>
		<link>http://www.monomorphic.org/wordpress/an-immutable-multimap-for-scala/</link>
		<comments>http://www.monomorphic.org/wordpress/an-immutable-multimap-for-scala/#comments</comments>
		<pubDate>Wed, 09 Dec 2009 06:41:05 +0000</pubDate>
		<dc:creator>johan</dc:creator>
				<category><![CDATA[Software development]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://www.monomorphic.org/wordpress/?p=438</guid>
		<description><![CDATA[The Scala collections library (in version 2.7.7) has a MultiMap trait for mutable collections, but none for immutable ones. I hacked something up to use while waiting for an official version. I&#8217;m finding this to work well, but I don&#8217;t have much experience with collections design, so it&#8217;s likely to have some flaws. Also, this [...]]]></description>
			<content:encoded><![CDATA[<p>The Scala collections library (in version 2.7.7) has a MultiMap trait for mutable collections, but none for immutable ones. I hacked something up to use while waiting for an official version. I&#8217;m finding this to work well, but I don&#8217;t have much experience with collections design, so it&#8217;s likely to have some flaws. Also, this is a class and not a trait, so you can&#8217;t use it with any map you like. And from a concurrency perspective, maybe it&#8217;s sometimes better to use backing collections other than the HashSet and the HashMap.</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">&nbsp;
<span style="color: #0000ff; font-weight: bold;">import</span> scala.<span style="color: #000000;">collection</span>.<span style="color: #000000;">immutable</span>.<span style="color: #000080;">_</span>
&nbsp;
<span style="color: #00ff00; font-style: italic;">/**
A multimap for immutable member sets (the Scala libraries 
only have one for mutable sets). 
*/</span>
<span style="color: #0000ff; font-weight: bold;">class</span> MultiMap<span style="color: #F78811;">&#91;</span>A, B<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">val</span> myMap<span style="color: #000080;">:</span> Map<span style="color: #F78811;">&#91;</span>A, Set<span style="color: #F78811;">&#91;</span>B<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
&nbsp;
	<span style="color: #0000ff; font-weight: bold;">def</span> <span style="color: #0000ff; font-weight: bold;">this</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">this</span><span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">new</span> HashMap<span style="color: #F78811;">&#91;</span>A, Set<span style="color: #F78811;">&#91;</span>B<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span>
&nbsp;
	<span style="color: #0000ff; font-weight: bold;">def</span> +<span style="color: #F78811;">&#40;</span>kv<span style="color: #000080;">:</span> Tuple2<span style="color: #F78811;">&#91;</span>A, B<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> MultiMap<span style="color: #F78811;">&#91;</span>A, B<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
	  <span style="color: #0000ff; font-weight: bold;">val</span> set <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">if</span> <span style="color: #F78811;">&#40;</span>myMap.<span style="color: #000000;">contains</span><span style="color: #F78811;">&#40;</span>kv.<span style="color: #000080;">_</span>1<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
		  myMap<span style="color: #F78811;">&#40;</span>kv.<span style="color: #000080;">_</span>1<span style="color: #F78811;">&#41;</span> + kv.<span style="color: #000080;">_</span>2
	  <span style="color: #F78811;">&#125;</span> <span style="color: #0000ff; font-weight: bold;">else</span> <span style="color: #F78811;">&#123;</span>
		  <span style="color: #0000ff; font-weight: bold;">new</span> HashSet<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> + kv.<span style="color: #000080;">_</span>2	     	   
	  <span style="color: #F78811;">&#125;</span>
&nbsp;
	  <span style="color: #0000ff; font-weight: bold;">new</span> MultiMap<span style="color: #F78811;">&#91;</span>A, B<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span>myMap + <span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#40;</span>kv.<span style="color: #000080;">_</span>1, set<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
	<span style="color: #F78811;">&#125;</span>
&nbsp;
	<span style="color: #0000ff; font-weight: bold;">def</span> -<span style="color: #F78811;">&#40;</span>kv<span style="color: #000080;">:</span> Tuple2<span style="color: #F78811;">&#91;</span>A, B<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> MultiMap<span style="color: #F78811;">&#91;</span>A, B<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
	  <span style="color: #0000ff; font-weight: bold;">if</span> <span style="color: #F78811;">&#40;</span><span style="color: #000080;">!</span>myMap.<span style="color: #000000;">contains</span><span style="color: #F78811;">&#40;</span>kv.<span style="color: #000080;">_</span>1<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
	    <span style="color: #0000ff; font-weight: bold;">throw</span> <span style="color: #0000ff; font-weight: bold;">new</span> Exception<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;No such key&quot;</span><span style="color: #F78811;">&#41;</span>
	  <span style="color: #F78811;">&#125;</span>
	  <span style="color: #0000ff; font-weight: bold;">val</span> set <span style="color: #000080;">=</span> myMap<span style="color: #F78811;">&#40;</span>kv.<span style="color: #000080;">_</span>1<span style="color: #F78811;">&#41;</span> - kv.<span style="color: #000080;">_</span>2
	  <span style="color: #0000ff; font-weight: bold;">if</span> <span style="color: #F78811;">&#40;</span>set.<span style="color: #000000;">isEmpty</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
	    <span style="color: #0000ff; font-weight: bold;">new</span> MultiMap<span style="color: #F78811;">&#91;</span>A, B<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span>myMap - kv.<span style="color: #000080;">_</span>1<span style="color: #F78811;">&#41;</span>
	  <span style="color: #F78811;">&#125;</span> <span style="color: #0000ff; font-weight: bold;">else</span> <span style="color: #F78811;">&#123;</span>
		  <span style="color: #0000ff; font-weight: bold;">new</span> MultiMap<span style="color: #F78811;">&#91;</span>A, B<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span>myMap + <span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#40;</span>kv.<span style="color: #000080;">_</span>1, set<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
	  <span style="color: #F78811;">&#125;</span>
	<span style="color: #F78811;">&#125;</span>
&nbsp;
	<span style="color: #0000ff; font-weight: bold;">def</span> entryExists<span style="color: #F78811;">&#40;</span>kv<span style="color: #000080;">:</span> Tuple2<span style="color: #F78811;">&#91;</span>A, B<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> Boolean <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
	  <span style="color: #0000ff; font-weight: bold;">if</span> <span style="color: #F78811;">&#40;</span><span style="color: #000080;">!</span>myMap.<span style="color: #000000;">contains</span><span style="color: #F78811;">&#40;</span>kv.<span style="color: #000080;">_</span>1<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
	    <span style="color: #0000ff; font-weight: bold;">false</span>
	  <span style="color: #F78811;">&#125;</span> <span style="color: #0000ff; font-weight: bold;">else</span> <span style="color: #F78811;">&#123;</span>
	    myMap<span style="color: #F78811;">&#40;</span>kv.<span style="color: #000080;">_</span>1<span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">contains</span><span style="color: #F78811;">&#40;</span>kv.<span style="color: #000080;">_</span>2<span style="color: #F78811;">&#41;</span>
	  <span style="color: #F78811;">&#125;</span>
	<span style="color: #F78811;">&#125;</span>
&nbsp;
    <span style="color: #0000ff; font-weight: bold;">def</span> keys <span style="color: #000080;">=</span> myMap.<span style="color: #000000;">keys</span>
&nbsp;
     <span style="color: #0000ff; font-weight: bold;">def</span> values<span style="color: #000080;">:</span> Iterator<span style="color: #F78811;">&#91;</span>Set<span style="color: #F78811;">&#91;</span>B<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> myMap.<span style="color: #000000;">values</span>
&nbsp;
    <span style="color: #0000ff; font-weight: bold;">def</span> getOrElse<span style="color: #F78811;">&#40;</span>key<span style="color: #000080;">:</span> A, elval<span style="color: #000080;">:</span> Collection<span style="color: #F78811;">&#91;</span>B<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> Collection<span style="color: #F78811;">&#91;</span>B<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>      
      myMap.<span style="color: #000000;">getOrElse</span><span style="color: #F78811;">&#40;</span>key, elval<span style="color: #F78811;">&#41;</span>
    <span style="color: #F78811;">&#125;</span>
&nbsp;
    <span style="color: #0000ff; font-weight: bold;">def</span> apply<span style="color: #F78811;">&#40;</span>key<span style="color: #000080;">:</span> A<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> myMap<span style="color: #F78811;">&#40;</span>key<span style="color: #F78811;">&#41;</span>
&nbsp;
&nbsp;
&nbsp;
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>Usage:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">&nbsp;
   <span style="color: #0000ff; font-weight: bold;">var</span> theMultiMap <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> MultiMap<span style="color: #F78811;">&#91;</span>String, Int<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>
&nbsp;
   theMultiMap +<span style="color: #000080;">=</span> <span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;george&quot;</span>, <span style="color: #F78811;">1</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
   theMultiMap +<span style="color: #000080;">=</span> <span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;george&quot;</span>, <span style="color: #F78811;">3</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
   theMultiMap +<span style="color: #000080;">=</span> <span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;bob&quot;</span>, <span style="color: #F78811;">2</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
   theMultiMap -<span style="color: #000080;">=</span> <span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;george&quot;</span>, <span style="color: #F78811;">1</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.monomorphic.org/wordpress/an-immutable-multimap-for-scala/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Programming languages are about people</title>
		<link>http://www.monomorphic.org/wordpress/programming-languages-are-about-people/</link>
		<comments>http://www.monomorphic.org/wordpress/programming-languages-are-about-people/#comments</comments>
		<pubDate>Mon, 28 Sep 2009 14:40:43 +0000</pubDate>
		<dc:creator>johan</dc:creator>
				<category><![CDATA[Software development]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[programmer psychology]]></category>
		<category><![CDATA[programming languages]]></category>

		<guid isPermaLink="false">http://www.monomorphic.org/wordpress/?p=337</guid>
		<description><![CDATA[Programming languages are more about people and less about machines. Programming languages are about staying inside the limitations of people&#8217;s minds and their ability to keep track of and work with abstractions. If people had no such limitations, they could code in assembly language all the time. Programming languages and supporting tools and environments are [...]]]></description>
			<content:encoded><![CDATA[<p>Programming languages are more about people and less about machines.</p>
<p>Programming languages are about staying inside the limitations of people&#8217;s minds and their ability to keep track of and work with abstractions. If people had no such limitations, they could code in assembly language all the time.</p>
<p>Programming languages and supporting tools and environments are the interface between people and the raw instruction set of a computer (or a bigger entity, like a network of computers). When we design programming languages, we must take into account not only what the machine environment will do and how it changes, but also how people create the software, how they modify it, how they think about it. Maybe programming languages should even be designed with business processes in mind, in some cases.</p>
<p>But this is also a question of what kind of programmer we want to cultivate. The language shapes the programmer, too.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.monomorphic.org/wordpress/programming-languages-are-about-people/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Scala and actors</title>
		<link>http://www.monomorphic.org/wordpress/scala-and-actors/</link>
		<comments>http://www.monomorphic.org/wordpress/scala-and-actors/#comments</comments>
		<pubDate>Sun, 30 Aug 2009 13:15:03 +0000</pubDate>
		<dc:creator>johan</dc:creator>
				<category><![CDATA[Software development]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[programming languages]]></category>
		<category><![CDATA[scala]]></category>
		<category><![CDATA[software engineering]]></category>

		<guid isPermaLink="false">http://www.monomorphic.org/wordpress/?p=298</guid>
		<description><![CDATA[Programming with actors was a new concept to me until I tried it out in Scala. It&#8217;s appears to be one of Scala&#8217;s most celebrated features, judging by the official blurb. Actors was a daunting word at first but it really ends up being a very simple concept. Actors are a programming model for concurrent [...]]]></description>
			<content:encoded><![CDATA[<p>Programming with <em>actors</em> was a new concept to me until I tried it out in Scala. It&#8217;s appears to be one of Scala&#8217;s most celebrated features, judging by the official blurb. Actors was a daunting word at first but it really ends up being a very simple concept.</p>
<p>Actors are a programming model for concurrent programming. With conventional mutex/monitor based programming in Java, say, programmers hold and release locks (the <code>synchronized</code> keyword) to achieve safe concurrency. Condition variables are used for thread communication (the <code>wait</code> and <code>notify</code> family of functions on java.lang.Object). Communication is synchronous: a typical case would be that you change some condition, invoke <code>notifyAll</code> to wake up threads waiting on that condition, and then they can take over the relevant lock and proceed to do some processing.</p>
<p>An actor is a unit of execution with an asynchronous message queue. Actors can receive messages from other actors or send messages to other actors at any time, however, the messages wait in the receiving actor&#8217;s &#8220;mailbox&#8221; until the actor has time to receive it.</p>
<p>As a simple example, let&#8217;s develop a program that converts text files to upper case using actors. The program will have an &#8220;Input&#8221; actor, an &#8220;Output&#8221; actor, and a number of &#8220;UpperCase&#8221; actors that do the processing. First the Input actor:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">import</span> scala.<span style="color: #000000;">actors</span>.<span style="color: #000080;">_</span>
<span style="color: #0000ff; font-weight: bold;">import</span> java.<span style="color: #000000;">io</span>.<span style="color: #000080;">_</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">class</span> Input<span style="color: #F78811;">&#40;</span>in<span style="color: #000080;">:</span> BufferedReader<span style="color: #F78811;">&#41;</span> <span style="color: #0000ff; font-weight: bold;">extends</span> Actor <span style="color: #F78811;">&#123;</span>
	<span style="color: #0000ff; font-weight: bold;">def</span> act<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
	  <span style="color: #0000ff; font-weight: bold;">while</span><span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">true</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
	    receive <span style="color: #F78811;">&#123;</span>
	      <span style="color: #0000ff; font-weight: bold;">case</span> Next <span style="color: #000080;">=&amp;</span>gt<span style="color: #000080;">;</span> <span style="color: #F78811;">&#123;</span> sender <span style="color: #000080;">!</span> Line<span style="color: #F78811;">&#40;</span>in.<span style="color: #000000;">readLine</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#125;</span>
	    <span style="color: #F78811;">&#125;</span>
	  <span style="color: #F78811;">&#125;</span>
	<span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>It&#8217;s worth noting that the Actor system is implemented completely in the libraries, outside of the core language. Actors are not first class constructs, but sometimes look as if they were. The <code>act</code> method is where actors begin their execution. The receive method causes them to block and wait for a message, which we may pattern match on. The sender variable corresponds to whoever sent the last message received, and the &#8216;!&#8217; operator sends a message. So whenever this actor receives the <code>Next</code> message, it will respond with the next line from a buffered reader.</p>
<p>Then, the UpperCase actor:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">import</span> scala.<span style="color: #000000;">actors</span>.<span style="color: #000080;">_</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">case</span> <span style="color: #0000ff; font-weight: bold;">class</span> Next
<span style="color: #0000ff; font-weight: bold;">case</span> <span style="color: #0000ff; font-weight: bold;">class</span> Line<span style="color: #F78811;">&#40;</span>x<span style="color: #000080;">:</span> String<span style="color: #F78811;">&#41;</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">class</span> UpperCase<span style="color: #F78811;">&#40;</span>input<span style="color: #000080;">:</span> Actor, out<span style="color: #000080;">:</span> Actor<span style="color: #F78811;">&#41;</span> <span style="color: #0000ff; font-weight: bold;">extends</span> Actor <span style="color: #F78811;">&#123;</span>
	<span style="color: #0000ff; font-weight: bold;">def</span> act<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
		<span style="color: #0000ff; font-weight: bold;">while</span><span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">true</span><span style="color: #F78811;">&#41;</span>
		<span style="color: #F78811;">&#123;</span>
			input <span style="color: #000080;">!</span> Next
			receive <span style="color: #F78811;">&#123;</span>
			<span style="color: #0000ff; font-weight: bold;">case</span> Line<span style="color: #F78811;">&#40;</span>x<span style="color: #000080;">:</span>String<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=&amp;</span>gt<span style="color: #000080;">;</span> <span style="color: #F78811;">&#123;</span> out <span style="color: #000080;">!</span> x.<span style="color: #000000;">toUpperCase</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#125;</span>
			<span style="color: #F78811;">&#125;</span>
		<span style="color: #F78811;">&#125;</span>
	<span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>This actor is created with in- and output actors as its constructor parameters. It continually asks the input actor for a new line, converts it to upper case, and sends it to the output actor. Also note the <em>case classes</em> here, which are for pattern matching only. They are a bit like algebraic data types in Haskell.</p>
<p>Finally, the Output actor:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">import</span> scala.<span style="color: #000000;">actors</span>.<span style="color: #000080;">_</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">class</span> Output <span style="color: #0000ff; font-weight: bold;">extends</span> Actor <span style="color: #F78811;">&#123;</span>
	<span style="color: #0000ff; font-weight: bold;">def</span> act<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
		<span style="color: #0000ff; font-weight: bold;">while</span><span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">true</span><span style="color: #F78811;">&#41;</span>
		<span style="color: #F78811;">&#123;</span>
			receive <span style="color: #F78811;">&#123;</span>
			<span style="color: #0000ff; font-weight: bold;">case</span> x<span style="color: #000080;">:</span>String <span style="color: #000080;">=&amp;</span>gt<span style="color: #000080;">;</span> <span style="color: #F78811;">&#123;</span> println<span style="color: #F78811;">&#40;</span>x<span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#125;</span>
			<span style="color: #F78811;">&#125;</span>
		<span style="color: #F78811;">&#125;</span>
	<span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>And then we have to tie it all together:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">import</span> java.<span style="color: #000000;">io</span>.<span style="color: #000080;">_</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">object</span> Demonstration <span style="color: #F78811;">&#123;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">val</span> reader <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> BufferedReader<span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">new</span> InputStreamReader<span style="color: #F78811;">&#40;</span>System.<span style="color: #000000;">in</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> main<span style="color: #F78811;">&#40;</span>args<span style="color: #000080;">:</span> Array<span style="color: #F78811;">&#91;</span>String<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
&nbsp;
    <span style="color: #0000ff; font-weight: bold;">val</span> in <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> Input<span style="color: #F78811;">&#40;</span>reader<span style="color: #F78811;">&#41;</span>
    in.<span style="color: #000000;">start</span>
&nbsp;
    <span style="color: #0000ff; font-weight: bold;">val</span> out <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> Output<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>
    out.<span style="color: #000000;">start</span>
&nbsp;
    1.<span style="color: #000000;">to</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">5</span><span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">foreach</span><span style="color: #F78811;">&#40;</span>x <span style="color: #000080;">=&amp;</span>gt<span style="color: #000080;">;</span> <span style="color: #F78811;">&#123;</span>
      <span style="color: #0000ff; font-weight: bold;">val</span> tr <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> UpperCase<span style="color: #F78811;">&#40;</span>in, out<span style="color: #F78811;">&#41;</span>
      tr.<span style="color: #000000;">start</span>
    <span style="color: #F78811;">&#125;</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>Here I abuse the foreach notation slightly to create 5 parallel text processors. Each actor runs on its own thread (though there are ways to prevent this if one wants very large numbers of actors). Now of course, the lines will probably be output in the wrong order. Another obvious shortcoming is that there is no clean shutdown protocol that terminates all the actors when the input stream is fully read. Solving these problems is outside of the scope of this article.</p>
<p>Some other interesting resources on actors: <a href="http://www.scala-lang.org/node/242">the official tutorial</a>, <a href="http://lamp.epfl.ch/~phaller/actors.html">the papers</a> (slightly more academic but accessible to the <em>monomorphic</em> reader, I imagine). Debasish highlights how actors can be used to get <a href="http://debasishg.blogspot.com/2006/11/threadless-concurrency-on-jvm-aka-scala.html">threadless concurrency</a>, Erlang-style.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.monomorphic.org/wordpress/scala-and-actors/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>First steps with Scala: XML pull parsing</title>
		<link>http://www.monomorphic.org/wordpress/first-steps-with-scala-xml-pull-parsing/</link>
		<comments>http://www.monomorphic.org/wordpress/first-steps-with-scala-xml-pull-parsing/#comments</comments>
		<pubDate>Fri, 14 Aug 2009 05:39:46 +0000</pubDate>
		<dc:creator>johan</dc:creator>
				<category><![CDATA[Software development]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[programming languages]]></category>
		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://www.monomorphic.org/wordpress/?p=264</guid>
		<description><![CDATA[I&#8217;m now going to share some of the results of my recent experiments with theÂ Scala programming language. In May I wrote that I had started looking at it. I&#8217;ve been using it to make some support tools that I needed for research work since. First a disclaimer: It&#8217;s been 4+ years since I did serious [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m now going to share some of the results of my recent experiments with theÂ <a href="http://www.scala-lang.org/">Scala</a> programming language. In May <a href="http://www.monomorphic.org/wordpress/exploring-scala/">I wrote</a> that I had started looking at it. I&#8217;ve been using it to make some support tools that I needed for research work since.</p>
<p>First a disclaimer: It&#8217;s been 4+ years since I did serious work with a functional programming language (Haskell, in first year of university), so my style is imperative-sprinkled-with-functional rather than the opposite. Also, since I haven&#8217;t spent that much time with this language yet, I&#8217;m bound to be making obvious mistakes. That said, I&#8217;m happy to be able to recommend Scala to pretty much anyone at this point. The learning curve is not steep if you know Java, and it allows for a variety of approaches depending on who you are.</p>
<p>For this particular tool, I needed to parse XML files, edit the contents of certain tags, and spit the data back out again. I&#8217;d like to show what I ended up with and point out some of Scala&#8217;s powerful features. Let&#8217;s first look at some interesting parts, and then the entirety.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">object</span> XMLTool <span style="color: #F78811;">&#123;</span>
  <span style="color: #0000ff; font-weight: bold;">val</span> interLink <span style="color: #000080;">=</span> <span style="color: #6666FF;">&quot;&quot;</span><span style="color: #6666FF;">&quot;<span style="color: #0000ff; font-weight: bold;">\[</span><span style="color: #0000ff; font-weight: bold;">\[</span>(.*)<span style="color: #0000ff; font-weight: bold;">\]</span><span style="color: #0000ff; font-weight: bold;">\]</span>&quot;</span><span style="color: #6666FF;">&quot;&quot;</span>.<span style="color: #000000;">r</span>
<span style="color: #F78811;">&#125;</span></pre></td></tr></table></div>

<p>Scala lets you define <i>objects</i> as well as classes. Objects are singletons and can be referred to by name. Otherwise they are like classes; they participate in the type hierarchy.<br />
Scala has three kinds of declarations: val, var and def. Values are evaluates once and cannot be reassigned. Vars are variables which can be reassigned. Defs are definitions and can as such be functions or values. My understanding is that they are lazily evaluated. The type of this val declaration is inferred by the highly powerful type system automatically using Hindley Milner type inference. One of my biggest surprises with Scala is how little type information the programmer has to provide, yet how powerful the static checking is. Incidentally, the .r at the end is a shortcut for turning the string into a regular expression object.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">def</span> main<span style="color: #F78811;">&#40;</span>args <span style="color: #000080;">:</span> Array<span style="color: #F78811;">&#91;</span>String<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">:</span> Unit <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
&nbsp;
    <span style="color: #0000ff; font-weight: bold;">val</span> p <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> XMLEventReader<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">initialize</span><span style="color: #F78811;">&#40;</span>Source.<span style="color: #000000;">fromFile</span><span style="color: #F78811;">&#40;</span>args<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">0</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
    p.<span style="color: #000000;">foreach</span><span style="color: #F78811;">&#40;</span>matchEvent<span style="color: #F78811;">&#41;</span>
&nbsp;
  <span style="color: #F78811;">&#125;</span></pre></td></tr></table></div>

<p>This function is the analogue of Java&#8217;s <tt>public static void main()</tt> and actually compiles to the same bytecode. Unlike in Java, types come after the variable name, separated from it by a colon. We can tell that we&#8217;re dealing with a functional language when we see <tt>foreach</tt> being applied to a function which I&#8217;ll declare next:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">def</span> matchEvent<span style="color: #F78811;">&#40;</span>ev<span style="color: #000080;">:</span> XMLEvent<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    ev <span style="color: #0000ff; font-weight: bold;">match</span> <span style="color: #F78811;">&#123;</span>
      <span style="color: #0000ff; font-weight: bold;">case</span> EvElemStart<span style="color: #F78811;">&#40;</span><span style="color: #000080;">_</span>, <span style="color: #6666FF;">&quot;text&quot;</span>, <span style="color: #000080;">_</span>, <span style="color: #000080;">_</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=&gt;</span> <span style="color: #F78811;">&#123;</span> 
        readingText <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">true</span>
        print<span style="color: #F78811;">&#40;</span>backToXml<span style="color: #F78811;">&#40;</span>ev<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
      <span style="color: #F78811;">&#125;</span>
      <span style="color: #0000ff; font-weight: bold;">case</span> EvElemStart<span style="color: #F78811;">&#40;</span><span style="color: #000080;">_</span>, <span style="color: #000080;">_</span>, <span style="color: #000080;">_</span>, <span style="color: #000080;">_</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=&gt;</span> <span style="color: #F78811;">&#123;</span> print<span style="color: #F78811;">&#40;</span>backToXml<span style="color: #F78811;">&#40;</span>ev<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#125;</span>
      <span style="color: #0000ff; font-weight: bold;">case</span> EvText<span style="color: #F78811;">&#40;</span>text<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=&gt;</span> <span style="color: #F78811;">&#123;</span>
        <span style="color: #0000ff; font-weight: bold;">if</span> <span style="color: #F78811;">&#40;</span>readingText<span style="color: #F78811;">&#41;</span> print<span style="color: #F78811;">&#40;</span>filterText<span style="color: #F78811;">&#40;</span>text<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span> <span style="color: #0000ff; font-weight: bold;">else</span> print<span style="color: #F78811;">&#40;</span>text<span style="color: #F78811;">&#41;</span> 
      <span style="color: #F78811;">&#125;</span> 
      <span style="color: #0000ff; font-weight: bold;">case</span> EvElemEnd<span style="color: #F78811;">&#40;</span><span style="color: #000080;">_</span>, <span style="color: #6666FF;">&quot;text&quot;</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=&gt;</span> <span style="color: #F78811;">&#123;</span>
        readingText <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">false</span>
        print<span style="color: #F78811;">&#40;</span>backToXml<span style="color: #F78811;">&#40;</span>ev<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
      <span style="color: #F78811;">&#125;</span>
      <span style="color: #0000ff; font-weight: bold;">case</span> EvElemEnd<span style="color: #F78811;">&#40;</span><span style="color: #000080;">_</span>, <span style="color: #000080;">_</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=&gt;</span> <span style="color: #F78811;">&#123;</span> print<span style="color: #F78811;">&#40;</span>backToXml<span style="color: #F78811;">&#40;</span>ev<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#125;</span>
      <span style="color: #0000ff; font-weight: bold;">case</span> <span style="color: #000080;">_</span> <span style="color: #000080;">=&gt;</span> <span style="color: #F78811;">&#123;</span><span style="color: #F78811;">&#125;</span>
    <span style="color: #F78811;">&#125;</span>
  <span style="color: #F78811;">&#125;</span></pre></td></tr></table></div>

<p>Here we see <i>pattern matching</i> in action. We can match on lots of things, including types, partially instantiated types, strings and regular expressions. This style of programming is encouraged in FP languages, unlike in imperative ones. By matching on something like <tt>EvElemStart(_, "text", _, _)</tt> I&#8217;m looking for XML tags whose name is &#8220;text&#8221;, and I don&#8217;t care about their namespace or attributes. _ is a wildcard character.</p>
<p>Incidentally, it&#8217;s perfectly fine for me to leave out the return type of this function. Scala will infer that the return type is <tt>Unit</tt> (which vaguely corresponds to void in Java).</p>
<p>Here&#8217;s the whole thing:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">import</span> scala.<span style="color: #000000;">xml</span>.<span style="color: #000080;">_</span>
<span style="color: #0000ff; font-weight: bold;">import</span> scala.<span style="color: #000000;">xml</span>.<span style="color: #000000;">pull</span>.<span style="color: #000080;">_</span>
<span style="color: #0000ff; font-weight: bold;">import</span> scala.<span style="color: #000000;">io</span>.<span style="color: #000000;">Source</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">object</span> XMLTool <span style="color: #F78811;">&#123;</span>
  <span style="color: #0000ff; font-weight: bold;">val</span> interLink <span style="color: #000080;">=</span> <span style="color: #6666FF;">&quot;&quot;</span><span style="color: #6666FF;">&quot;<span style="color: #0000ff; font-weight: bold;">\[</span><span style="color: #0000ff; font-weight: bold;">\[</span>(.*)<span style="color: #0000ff; font-weight: bold;">\]</span><span style="color: #0000ff; font-weight: bold;">\]</span>&quot;</span><span style="color: #6666FF;">&quot;&quot;</span>.<span style="color: #000000;">r</span>
  <span style="color: #0000ff; font-weight: bold;">var</span> readingText <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">false</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> main<span style="color: #F78811;">&#40;</span>args <span style="color: #000080;">:</span> Array<span style="color: #F78811;">&#91;</span>String<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">:</span> Unit <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
&nbsp;
    <span style="color: #0000ff; font-weight: bold;">val</span> p <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> XMLEventReader<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">initialize</span><span style="color: #F78811;">&#40;</span>Source.<span style="color: #000000;">fromFile</span><span style="color: #F78811;">&#40;</span>args<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">0</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
    p.<span style="color: #000000;">foreach</span><span style="color: #F78811;">&#40;</span>matchEvent<span style="color: #F78811;">&#41;</span>
&nbsp;
  <span style="color: #F78811;">&#125;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> matchEvent<span style="color: #F78811;">&#40;</span>ev<span style="color: #000080;">:</span> XMLEvent<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    ev <span style="color: #0000ff; font-weight: bold;">match</span> <span style="color: #F78811;">&#123;</span>
      <span style="color: #0000ff; font-weight: bold;">case</span> EvElemStart<span style="color: #F78811;">&#40;</span><span style="color: #000080;">_</span>, <span style="color: #6666FF;">&quot;text&quot;</span>, <span style="color: #000080;">_</span>, <span style="color: #000080;">_</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=&gt;</span> <span style="color: #F78811;">&#123;</span> 
        readingText <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">true</span>
        print<span style="color: #F78811;">&#40;</span>backToXml<span style="color: #F78811;">&#40;</span>ev<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
      <span style="color: #F78811;">&#125;</span>
      <span style="color: #0000ff; font-weight: bold;">case</span> EvElemStart<span style="color: #F78811;">&#40;</span><span style="color: #000080;">_</span>, <span style="color: #000080;">_</span>, <span style="color: #000080;">_</span>, <span style="color: #000080;">_</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=&gt;</span> <span style="color: #F78811;">&#123;</span> print<span style="color: #F78811;">&#40;</span>backToXml<span style="color: #F78811;">&#40;</span>ev<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#125;</span>
      <span style="color: #0000ff; font-weight: bold;">case</span> EvText<span style="color: #F78811;">&#40;</span>text<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=&gt;</span> <span style="color: #F78811;">&#123;</span>
        <span style="color: #0000ff; font-weight: bold;">if</span> <span style="color: #F78811;">&#40;</span>readingText<span style="color: #F78811;">&#41;</span> print<span style="color: #F78811;">&#40;</span>filterText<span style="color: #F78811;">&#40;</span>text<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span> <span style="color: #0000ff; font-weight: bold;">else</span> print<span style="color: #F78811;">&#40;</span>text<span style="color: #F78811;">&#41;</span> 
      <span style="color: #F78811;">&#125;</span> 
      <span style="color: #0000ff; font-weight: bold;">case</span> EvElemEnd<span style="color: #F78811;">&#40;</span><span style="color: #000080;">_</span>, <span style="color: #6666FF;">&quot;text&quot;</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=&gt;</span> <span style="color: #F78811;">&#123;</span>
        readingText <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">false</span>
        print<span style="color: #F78811;">&#40;</span>backToXml<span style="color: #F78811;">&#40;</span>ev<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
      <span style="color: #F78811;">&#125;</span>
      <span style="color: #0000ff; font-weight: bold;">case</span> EvElemEnd<span style="color: #F78811;">&#40;</span><span style="color: #000080;">_</span>, <span style="color: #000080;">_</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=&gt;</span> <span style="color: #F78811;">&#123;</span> print<span style="color: #F78811;">&#40;</span>backToXml<span style="color: #F78811;">&#40;</span>ev<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#125;</span>
      <span style="color: #0000ff; font-weight: bold;">case</span> <span style="color: #000080;">_</span> <span style="color: #000080;">=&gt;</span> <span style="color: #F78811;">&#123;</span><span style="color: #F78811;">&#125;</span>
    <span style="color: #F78811;">&#125;</span>
  <span style="color: #F78811;">&#125;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> backToXml<span style="color: #F78811;">&#40;</span>ev<span style="color: #000080;">:</span> XMLEvent<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    ev <span style="color: #0000ff; font-weight: bold;">match</span> <span style="color: #F78811;">&#123;</span>
      <span style="color: #0000ff; font-weight: bold;">case</span> EvElemStart<span style="color: #F78811;">&#40;</span>pre, label, attrs, scope<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=&gt;</span> <span style="color: #F78811;">&#123;</span>
        <span style="color: #6666FF;">&quot;&lt;&quot;</span> + label + attrsToString<span style="color: #F78811;">&#40;</span>attrs<span style="color: #F78811;">&#41;</span> + <span style="color: #6666FF;">&quot;&gt;&quot;</span>
      <span style="color: #F78811;">&#125;</span>
      <span style="color: #0000ff; font-weight: bold;">case</span> EvElemEnd<span style="color: #F78811;">&#40;</span>pre, label<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=&gt;</span> <span style="color: #F78811;">&#123;</span>
        <span style="color: #6666FF;">&quot;&lt;/&quot;</span> + label + <span style="color: #6666FF;">&quot;&gt;&quot;</span>
      <span style="color: #F78811;">&#125;</span>
      <span style="color: #0000ff; font-weight: bold;">case</span> <span style="color: #000080;">_</span> <span style="color: #000080;">=&gt;</span> <span style="color: #6666FF;">&quot;&quot;</span>
    <span style="color: #F78811;">&#125;</span>
  <span style="color: #F78811;">&#125;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> attrsToString<span style="color: #F78811;">&#40;</span>attrs<span style="color: #000080;">:</span>MetaData<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    attrs.<span style="color: #000000;">length</span> <span style="color: #0000ff; font-weight: bold;">match</span> <span style="color: #F78811;">&#123;</span>
      <span style="color: #0000ff; font-weight: bold;">case</span> <span style="color: #F78811;">0</span> <span style="color: #000080;">=&gt;</span> <span style="color: #6666FF;">&quot;&quot;</span>
      <span style="color: #0000ff; font-weight: bold;">case</span> <span style="color: #000080;">_</span> <span style="color: #000080;">=&gt;</span> attrs.<span style="color: #000000;">map</span><span style="color: #F78811;">&#40;</span> <span style="color: #F78811;">&#40;</span>m<span style="color: #000080;">:</span>MetaData<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=&gt;</span> <span style="color: #6666FF;">&quot; &quot;</span> + m.<span style="color: #000000;">key</span> + <span style="color: #6666FF;">&quot;='&quot;</span> + m.<span style="color: #000000;">value</span> +<span style="color: #6666FF;">&quot;'&quot;</span> <span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">reduceLeft</span><span style="color: #F78811;">&#40;</span><span style="color: #000080;">_</span>+<span style="color: #000080;">_</span><span style="color: #F78811;">&#41;</span>
    <span style="color: #F78811;">&#125;</span>
  <span style="color: #F78811;">&#125;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> filterText<span style="color: #F78811;">&#40;</span>text<span style="color: #000080;">:</span> String<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> matches <span style="color: #000080;">=</span> interLink.<span style="color: #000000;">findAllIn</span><span style="color: #F78811;">&#40;</span>text<span style="color: #F78811;">&#41;</span>
    <span style="color: #0000ff; font-weight: bold;">if</span> <span style="color: #F78811;">&#40;</span>matches.<span style="color: #000000;">hasNext</span><span style="color: #F78811;">&#41;</span> matches.<span style="color: #000000;">reduceLeft</span><span style="color: #F78811;">&#40;</span><span style="color: #000080;">_</span>+<span style="color: #000080;">_</span><span style="color: #F78811;">&#41;</span> <span style="color: #0000ff; font-weight: bold;">else</span> <span style="color: #6666FF;">&quot;&quot;</span>
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></td></tr></table></div>

<p>So the purpose of this program is to read the XML, remove everything inside &lt;text&gt; tags that doesn&#8217;t match the <tt>interLink</tt> regular expression, and output the XML again. Towards the end, note how pleasant <tt>map</tt> and <tt>reduceLeft</tt> are for string processing &#8211; in Java I can&#8217;t really think of a succinct way of expressing the same notion.</p>
<p>Another couple of disclaimers: someone brought to my attention that there&#8217;s a very compact way of doing XPath queries in Scala, which probably makes my pattern matching on EvElemStart unnecessarily verbose. (<a href="http://metacircular.wordpress.com/2007/02/04/scala-makes-xml-processing-easy/">Here&#8217;s a blog post on the xpath technique</a>) Also, there was no particular reason for me to use pull parsing &#8211; push parsing might have been more natural, but I started down that path and this is what I ended up with. It works.</p>
<p>You can tell that I still have an imperative style from the way I use the readingText state variable to keep track of what the program is doing. A much more functional style program is probably hiding behind this one. Fortunately Scala is very forgiving towards people who mix styles like this.</p>
<p>My experience has been that it&#8217;s quite easy to get started and do useful things with Scala, once you get past the initial ideas (such as the difference between objects and classes, traits, val/def/var, declaration syntax). I would recommend it to anyone doing things with the JVM. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.monomorphic.org/wordpress/first-steps-with-scala-xml-pull-parsing/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>An unusual Java construct</title>
		<link>http://www.monomorphic.org/wordpress/an-unusual-java-construct/</link>
		<comments>http://www.monomorphic.org/wordpress/an-unusual-java-construct/#comments</comments>
		<pubDate>Wed, 29 Jul 2009 09:11:08 +0000</pubDate>
		<dc:creator>johan</dc:creator>
				<category><![CDATA[Software development]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[programming languages]]></category>

		<guid isPermaLink="false">http://www.monomorphic.org/wordpress/?p=215</guid>
		<description><![CDATA[I now break the longstanding tradition of not posting code on this blog. I just wanted to share what I believe to be a somewhat unusual pattern in Java: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 public IMethod findMethod&#40;String name, String&#91;&#93; types&#41; [...]]]></description>
			<content:encoded><![CDATA[<p>I now break the longstanding tradition of not posting code on this blog. I just wanted to share what I believe to be a somewhat unusual pattern in Java:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> IMethod findMethod<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> name, <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> types<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		outer<span style="color: #339933;">:</span>
		<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>IMethod m<span style="color: #339933;">:</span> m_methods<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>m.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>name<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</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>
				<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>IType t<span style="color: #339933;">:</span> m.<span style="color: #006633;">getArgList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getTypes</span><span style="color: #009900;">&#40;</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;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span> t.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>types<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</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;">continue</span> outer<span style="color: #339933;">;</span>
					<span style="color: #009900;">&#125;</span>
					i<span style="color: #339933;">++;</span>
				<span style="color: #009900;">&#125;</span>
				<span style="color: #000000; font-weight: bold;">return</span> m<span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>On line 2, there&#8217;s a label <tt>outer:</tt> which identifies a location in the code. Normally this feature is used with keywords like the <a href="http://www.u.arizona.edu/~rubinson/copyright_violations/Go_To_Considered_Harmful.html">hotly debated</a> <tt>goto</tt> in C. Java has <tt>goto</tt> as a keyword, but doesn&#8217;t support the feature. However, you can still use the labels with statements like <tt>continue</tt> above (line 12), which in this case starts a new iteration of the outer loop rather than the inner one.</p>
<p>I can&#8217;t remember ever having had to use this feature of any C-like language before (perhaps once) so it was intriguing when it popped up. It&#8217;s possible that a neater implementation of this would put the inner loop in a <tt>matchesSignature</tt> method in the <tt>IMethod</tt> interface instead.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.monomorphic.org/wordpress/an-unusual-java-construct/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
