<?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>Conceptual meandering</description>
	<lastBuildDate>Wed, 11 Jan 2012 02:07:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Type theory</title>
		<link>http://www.monomorphic.org/wordpress/type-theory/</link>
		<comments>http://www.monomorphic.org/wordpress/type-theory/#comments</comments>
		<pubDate>Sun, 26 Dec 2010 21:23:32 +0000</pubDate>
		<dc:creator>johan</dc:creator>
				<category><![CDATA[Computer science]]></category>
		<category><![CDATA[Philosophy]]></category>
		<category><![CDATA[academic]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[programming languages]]></category>

		<guid isPermaLink="false">http://www.monomorphic.org/wordpress/?p=774</guid>
		<description><![CDATA[One of the most interesting things I&#8217;ve been studying in the past year has been type theory. I feel that type theory is an area where a lot of separate fields can come together in a good design. In strongly typed languages, language implementation efficiency, syntax and language semantics all leave essential marks in the [...]]]></description>
			<content:encoded><![CDATA[<p>One of the most interesting things I&#8217;ve been studying in the past year has been type theory. I feel that type theory is an area where a lot of separate fields can come together in a good design. In strongly typed languages, language implementation efficiency, syntax and language semantics all leave essential marks in the type system, and conversely, flaws in the type system can impair all of them.</p>
<p>Type theory was invented by <a href="http://en.wikipedia.org/wiki/Bertrand_Russell">Bertrand Russell</a> as a solution to his own <a href="http://en.wikipedia.org/wiki/Russell%27s_paradox">well-known paradox</a>, which cast doubt on the foundations of mathematics upon its discovery.  The problem posed by the paradox is essentially: given a set of all sets that do not contain themselves, does that set contain itself? Another interesting problem arising from self-referentialism. Type theory resolves this issue by classifying the members of sets according to some type, and in such a system a set definition like this one is not permissible.</p>
<p>(The drama surrounding Russell&#8217;s paradox and <a href="http://en.wikipedia.org/wiki/Principia_Mathematica">Principia Mathematica</a> can now also be read in the form of a &#8220;logicomix&#8221; <a href="http://www.amazon.com/Logicomix-Search-Truth-Apostolos-Doxiadis/dp/1596914521">graphic novel</a> by Doxiadis et al!)</p>
<p>Type theory did not end its life as a metamathematical hack that solved a set theoretical problem. <a href="http://cacm.acm.org/magazines/2010/2/69367-type-theory-comes-of-age/fulltext">It has now come of age.</a> Type systems are, of course, at the heart of the development of many modern programming languages. Java, C#, Lisp, ML and Haskell are but a few of the ones that depend completely on a nontrivial type system. (Even though C and C++ have type systems, they are kept in a crippled state by the fact that the programmer is allowed to ignore their commandments at will.)</p>
<p>What is the benefit of a type system in a programming language? In the words of <a href="http://www.cis.upenn.edu/~bcpierce/">Benjamin Pierce</a>,</p>
<blockquote><p>A type system is a syntactic method for automatically checking the  absence of certain erroneous behaviors by classifying program phrases  according to the kinds of values they compute.</p></blockquote>
<p>So, generally speaking, a type system helps distinguish correct programs from incorrect programs. The parser also does part of this job, of course, since many programs that cannot compile will not parse. But there are programs that are syntactically correct but at the same time semantically unsound according to the evaluation rules of the language. Consider the following Java statement:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">T x <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> U<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This is clearly syntactically acceptable, but is it semantically valid? In order for it to be, either U needs to be the same type as T, or a subtype of T. But Java programmers are free to define new subtypes of existing types in source code. In order for the parser to check the correctness of the assignment, it seems that either the grammar would become enormously large, or it would place awkward restrictions on Java syntax. So we leave more refined aspects of correctness to the type checker, which is applied later, post-parsing.</p>
<p>What does a type system look like? As used in programming languages today, a type system consists of a set of <em>typing judgments</em>. The judgments can be stacked together to form the <em>typing derivation</em> of an expression in the language. Every expression normally has a unique typing derivation, and when a type can be derived, we call the expression well typed. As Benjamin Pierce&#8217;s formulation suggests, each typing rule makes some assumptions about the kinds of values that constitute the various parts of an expression, and then says something about the kind of value that will be computed by the expression as a whole.</p>
<p>Type systems can look prohibitive at first sight, since they use quite a few algebraic symbols. But in general, the structure and interrelationships between the symbols is the most important thing to discern from them, and focussing on this aspect makes reading them much easier.</p>
<p>In the following mini-type system, typing judgments will have the form <img src='http://www.monomorphic.org/wordpress/wp-content/latex/4e7/4e703057592a70077f2894e4f4643f16-ffffff-000000-0.png' alt='\Gamma \vdash t: T' title='\Gamma \vdash t: T' class='latex' />, where <img src='http://www.monomorphic.org/wordpress/wp-content/latex/077/07710b5c43702a8bb7b9104eacc6ba71-ffffff-000000-0.png' alt='\Gamma' title='\Gamma' class='latex' /> is a <em>typing context</em>, <img src='http://www.monomorphic.org/wordpress/wp-content/latex/e35/e358efa489f58062f10dd7316b65649e-ffffff-000000-0.png' alt='t' title='t' class='latex' /> is a <em>term</em>, and <img src='http://www.monomorphic.org/wordpress/wp-content/latex/b9e/b9ece18c950afbfa6b0fdbfa4ff731d3-ffffff-000000-0.png' alt='T' title='T' class='latex' /> is a type. Terms are essentially fragments of the source code, and from smaller terms we can construct larger terms using syntactic forms. So for example, if <strong>a</strong>, <strong>b</strong> and <strong>c</strong> are terms, then we might use the if-syntax to construct the larger term</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>a<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>b<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>c<span style="color: #009900;">&#125;</span></pre></div></div>

<p>in a java-like language. The typing context <img src='http://www.monomorphic.org/wordpress/wp-content/latex/077/07710b5c43702a8bb7b9104eacc6ba71-ffffff-000000-0.png' alt='\Gamma' title='\Gamma' class='latex' /> is essentially just a map, mapping already typed terms to the types assigned to them. As we type increasingly large terms, we gradually add more information to the typing context. The symbol <img src='http://www.monomorphic.org/wordpress/wp-content/latex/7f5/7f5788d56bd683ffba59eed8ad6ecbe6-ffffff-000000-0.png' alt='\vdash' title='\vdash' class='latex' /> is just notational convention and has no particular meaning. The judgment <img src='http://www.monomorphic.org/wordpress/wp-content/latex/4e7/4e703057592a70077f2894e4f4643f16-ffffff-000000-0.png' alt='\Gamma \vdash t: T' title='\Gamma \vdash t: T' class='latex' /> is read as &#8220;term t has type T in typing context <img src='http://www.monomorphic.org/wordpress/wp-content/latex/077/07710b5c43702a8bb7b9104eacc6ba71-ffffff-000000-0.png' alt='\Gamma' title='\Gamma' class='latex' />&#8220;.</p>
<p>A fragment of a minimal system might look like the following.</p>
<img src='http://www.monomorphic.org/wordpress/wp-content/latex/d7a/d7a8b6c7986eb44d76281142c2a69410-ffffff-000000-0.png' alt='  \inferrule  {  \Gamma \vdash t_1 : T  \and  \text{vartype}(v) = U  \and  T &lt;: U  }  {  \Gamma \vdash v = t_1 : U  }  {  \text{T-ASGN}  }  \\  \vspace{5mm}  \\   \inferrule  {  \Gamma \vdash t_1 : \text{Int}  \and  \Gamma \vdash t_2 : \text{Int}  }  {  \Gamma \vdash t_1~\textsf{intBinop}~t_2 : \text{Int}  }  {  \text{T-BINOP}  }   \\  \vspace{5mm}  \\   \inferrule  {  \Gamma \vdash t_1 : \text{Bool}  \and  \Gamma \vdash t_2 : T_0  \and  \Gamma \vdash t_3 : T_0  }  {  \Gamma \vdash \text{if}~t_1~\text{then}~t_2~\text{else}~t_3 : T_0  }  {  \text{T-IF}  }  ' title='  \inferrule  {  \Gamma \vdash t_1 : T  \and  \text{vartype}(v) = U  \and  T &lt;: U  }  {  \Gamma \vdash v = t_1 : U  }  {  \text{T-ASGN}  }  \\  \vspace{5mm}  \\   \inferrule  {  \Gamma \vdash t_1 : \text{Int}  \and  \Gamma \vdash t_2 : \text{Int}  }  {  \Gamma \vdash t_1~\textsf{intBinop}~t_2 : \text{Int}  }  {  \text{T-BINOP}  }   \\  \vspace{5mm}  \\   \inferrule  {  \Gamma \vdash t_1 : \text{Bool}  \and  \Gamma \vdash t_2 : T_0  \and  \Gamma \vdash t_3 : T_0  }  {  \Gamma \vdash \text{if}~t_1~\text{then}~t_2~\text{else}~t_3 : T_0  }  {  \text{T-IF}  }  ' class='latex' />
<p>Three typing rules have been introduced here, T-ASGN for assignments, T-IF for if-statements, and T-BINOP for binary operations on integers, like + and -.<br />
Every rule has the same structure: above the line, assumptions that must hold true for the rule to be applied; below the line, the conclusion we may draw if the assumptions are true.</p>
<p>Above, the following example was given.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">T x <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> U<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Using T-ASGN, we can type an assignment statement like this one. The assumption T &lt;: U says that T is a subtype of U. We have also assumed a lookup function <strong>vartype</strong> which gives the declared type of the variable we assign to. In English, we can read the rule T-ASGN as saying: &#8220;Assuming that the type of the right hand side is a subtype of, or the same type as, the declared type of the variable, the assignment will evaluate to a value that has the declared type of the variable&#8221;. Of course not all programming languages implement assignment in this way.</p>
<p>In the T-BINOP rule, we must of course substitute an actual operation on integers for the special word <strong>intBinop</strong> for it to be valid. For addition, the rule can be read as &#8220;Assuming that the left hand side and the right hand side are both integers, then the result is also an integer&#8221;.</p>
<p>Finally, the T-IF rule says &#8220;assuming that the truth condition is a boolean, and that the two alternative paths have the same type, then the if-statement evaluates to the same type as that of the two conditional paths&#8221;.</p>
<p>The shape of the type system follows the evaluation rules for the language quite closely, so that a well-typed term is also always possible to evaluate. We can thus avoid accepting programs that might get stuck without any valid evaluation rule at some point. (Consider what happens in Python if you try to invoke a method on an object, and the object doesn&#8217;t have a method with that name.) We are not limited to tracking just the kinds of values being computed in a type system. We can track various kinds of safety properties, such as memory regions being violated, as well, or we may track multiple properties at once. <a href="http://en.wikipedia.org/wiki/Effect_system">Type and effect systems </a>is one way of tracking both values being computed and resource violations.</p>
<p>A good source of more information on type systems applied to programming languages would be Benjamin Pierce&#8217;s <a href="http://www.amazon.com/Types-Programming-Languages-Benjamin-Pierce/dp/0262162091/ref=cm_cr_pr_product_top">Types and Programming Languages</a>.</p>
 <p><a href="http://www.monomorphic.org/wordpress/?flattrss_redirect&amp;id=774&amp;md5=d3d903e1d4d5f0b5f1bc76ddf9d7f12c" title="Flattr" target="_blank"><img src="http://www.monomorphic.org/wordpress/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.monomorphic.org/wordpress/type-theory/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>
 <p><a href="http://www.monomorphic.org/wordpress/?flattrss_redirect&amp;id=671&amp;md5=06e00967ad2407bd4d0aa599e06b834f" title="Flattr" target="_blank"><img src="http://www.monomorphic.org/wordpress/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></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>

		<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;
    <span style="color: #F78811;">1</span>.<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>

