<?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; scala</title>
	<atom:link href="http://www.monomorphic.org/wordpress/tag/scala/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>What makes a good programming language?</title>
		<link>http://www.monomorphic.org/wordpress/what-makes-a-good-programming-language/</link>
		<comments>http://www.monomorphic.org/wordpress/what-makes-a-good-programming-language/#comments</comments>
		<pubDate>Fri, 02 Sep 2011 09:00:09 +0000</pubDate>
		<dc:creator>johan</dc:creator>
				<category><![CDATA[Computer science]]></category>
		<category><![CDATA[Software development]]></category>
		<category><![CDATA[academic]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[programming languages]]></category>
		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://www.monomorphic.org/wordpress/?p=1012</guid>
		<description><![CDATA[New programming languages are released all the time. History is littered with dead ones. There are also many long time survivors in good shape, as well as geriatric languages on life support. What makes a programming language attractive and competitive? How can we evaluate its quality? There are many different aspects of this problem. Ease [...]]]></description>
			<content:encoded><![CDATA[<p>New programming languages are released all the time. History is littered with dead ones. There are also many long time survivors in good shape, as well as geriatric languages on life support.</p>
<p>What makes a programming language attractive and competitive? How can we evaluate its quality? There are many different aspects of this problem.</p>
<p><strong>Ease of reading and writing</strong>, or: directness of the mapping between the problem in your head and the model you are creating on the computer. This can be highly domain dependent, for instance languages such as LaTeX, Matlab and R are designed with specific problems in mind and cater to users from that domain. Their limits show quickly when you try to stretch them beyond their envisioned purpose. Speaking of general programming languages, I think Python deserves to be mentioned as a language that is extremely readable and writable. It has other shortcomings though &#8211; see below. Prolog is also highly read- and writable if it suits your problem.</p>
<p><strong>Runtime performance. </strong>Arguably this is one of the few reasons to bother with using C++. For the majority of programming projects though, performance is much less of a problem than one might think, especially if one considers how close the performance of many JVM languages get to C++. When programmers think about their overall productivity and effectiveness in developing and maintaining a system, C++ is often not the best choice, obviously.</p>
<p><strong>Scalability to large teams. </strong>The key property here is: does the language do anything to help me, as a developer, work with code that other people wrote? Ease of maintenance may be strongly correlated with usability in large teams. An anti-pattern here is languages that allow for solving the same problem in a huge amount of ways with very variable syntax. For instance, Perl and C++ can lead to notoriously unmaintainable code if used carelessly. Some say that Scala also suffers from this problem. Basically, the language helps here if it prevents me from doing things that other developers might not expect, and that I might forget to document or communicate. This is why Gosling famously called Java a blue collar language; it restricts you enough to make teamwork quite practical. It even restricts the layout of your source file hierarchy. (Now we begin to see that some goals are in conflict with each other).</p>
<p><strong>Scalability to large systems. </strong>This is related to the preceding property, but whereas team scalability seems to be mainly about avoiding the creation of code fragments that surprise people other than their creators, system size scalability seems to be about avoiding the creation of code fragments that surprise other code fragments. Here one needs invariants, good type checking, static constraints of all kinds. Scripting languages like Perl and Python, lacking static typing completely, are some of the worst in this regard, since we cannot even be sure at startup time that methods we try to invoke on objects exist at all (Python).</p>
<p><strong>Scalability over time (maintainability).</strong> If there is both system size scalability and team scalability, then the system is also likely to be able to live for a long time without great troubles.</p>
<p><strong>Developer efficiency and rapid prototyping. </strong>Depending on the nature of the system being developed, this may depend on several different properties listed above.</p>
<p><strong>Availability of quality tools.</strong> Mature runtime environments, such as the JVM, have many more high quality tools and IDEs available than a language than Ruby. Mature languages also have more compilers for more different architectures available.</p>
<p>These points begin to give us an idea of how we can evaluate programming languages. However, I also believe that making a good language and making people use it is largely about luck and factors outside the design itself. Just like there&#8217;s a big step between imagining and specifying an utopian society and making that social order an actuality, there&#8217;s a big step between designing an ideal programming language and achieving widespread adoption for it. We have seen a way forward though: with generalised runtime environments such as the JVM and the CLR, we may develop and deploy languages that take advantage of a lot of existing infrastructure much more easily than before. And what I hope for is in fact that it becomes even easier to deploy new languages, and that new languages are as interoperable as possible (insofar as it doesn&#8217;t constrain their design), so that we could see more competition, more evolution and more risk taking in the PL space.</p>
 <p><a href="http://www.monomorphic.org/wordpress/?flattrss_redirect&amp;id=1012&amp;md5=fff6eeac12b2b852112ea787fe9ec90f" 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/what-makes-a-good-programming-language/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Pointers in programming languages</title>
		<link>http://www.monomorphic.org/wordpress/pointers-in-programming-languages/</link>
		<comments>http://www.monomorphic.org/wordpress/pointers-in-programming-languages/#comments</comments>
		<pubDate>Thu, 25 Aug 2011 15:21:03 +0000</pubDate>
		<dc:creator>johan</dc:creator>
				<category><![CDATA[Computer science]]></category>
		<category><![CDATA[Software development]]></category>
		<category><![CDATA[academic]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[programming languages]]></category>
		<category><![CDATA[research]]></category>
		<category><![CDATA[reuse]]></category>
		<category><![CDATA[scala]]></category>
		<category><![CDATA[state]]></category>

		<guid isPermaLink="false">http://www.monomorphic.org/wordpress/?p=1006</guid>
		<description><![CDATA[It is likely that few features cause as much problems as pointers and references in statement-oriented languages, such as C, C++ and Java. They are powerful, yes, and they allow us to control quite precisely how a program is to represent something. We can use them to conveniently compose objects and data without the redundancy [...]]]></description>
			<content:encoded><![CDATA[<p>It is likely that few features cause as much problems as pointers and references in statement-oriented languages, such as C, C++ and Java. They are powerful, yes, and they allow us to control quite precisely how a program is to represent something. We can use them to conveniently compose objects and data without the redundancy of replicating information massively. In languages like C they are even more powerful than in Java, since just about any part of memory can be viewed as if it were just about anything through the use of pointer arithmetic, which is indeed frightening.</p>
<p>But they also complicate reasoning about programs enormously. Both human reasoning and automated reasoning. Pointers allow any part of the program to have side effects in any other part of the program (if we have a reference to an object that originated there), and they make it very hard to reason about the properties that an object might have at a given point in time (since we generally have no idea who might hold a reference to it &#8211; it is amazing that programmers are forced to track this in their heads, more or less). In my effort to design my own language, multiple pointers to the same objects &#8211; aliases &#8211; have come back from time to time to bite me and block elegant, attractive designs. I believe that this is a<em> very </em>hard problem to design around. Aliased pointers set up communication channels between arbitrary parts of a program.</p>
<p>Nevertheless attempts have been made, in academia and in research labs, to solve this problem. Fraction-based permissions track how many aliases exist and endow each alias with specific permissions to access the object that is referred to. Ownership analysis forces access to certain objects to go through special, &#8220;owning&#8221; objects. Unique or &#8220;unshared&#8221; pointers in some language extensions restrict whether aliases may be created or not. But so far no solution has been extremely attractive and convenient, and none has made it into mainstream languages. (<del>I know that someone</del> Philipp Haller made a <a href="http://lamp.epfl.ch/~phaller/readme_uniqueness.html">uniqueness plugin for the Scala compiler</a>, but it is not in wide use, I believe.)</p>
<p>If we are to attempt further incremental evolution of the C-family languages, aliased pointers are one of the most important issues we can attack in my opinion.</p>
 <p><a href="http://www.monomorphic.org/wordpress/?flattrss_redirect&amp;id=1006&amp;md5=45794a6be4c8cfc42a0be706de7eb18b" 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/pointers-in-programming-languages/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Reviewing the second year of Monomorphic</title>
		<link>http://www.monomorphic.org/wordpress/reviewing-the-second-year-of-monomorphic/</link>
		<comments>http://www.monomorphic.org/wordpress/reviewing-the-second-year-of-monomorphic/#comments</comments>
		<pubDate>Thu, 30 Jun 2011 11:19:26 +0000</pubDate>
		<dc:creator>johan</dc:creator>
				<category><![CDATA[Computer science]]></category>
		<category><![CDATA[Life]]></category>
		<category><![CDATA[Philosophy]]></category>
		<category><![CDATA[meta]]></category>
		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://www.monomorphic.org/wordpress/?p=950</guid>
		<description><![CDATA[In May 2010 I reviewed the state of Monomorphic as a blog. Since it&#8217;s now been almost 13 months since that time, let&#8217;s evaluate what&#8217;s happened in the meantime. Where am I, how did I get here, and where do I go next? The rate of publication has decreased. Prior to the last evaluation, 55 [...]]]></description>
			<content:encoded><![CDATA[<p>In May 2010 <a href="http://www.monomorphic.org/wordpress/meta-notes-1-year-with-monomorphic-blogging/">I reviewed</a> the state of Monomorphic as a blog. Since it&#8217;s now been almost 13 months since that time, let&#8217;s evaluate what&#8217;s happened in the meantime. Where am I, how did I get here, and where do I go next?</p>
<p>The rate of publication has decreased. Prior to the last evaluation, 55 posts had been published &#8211; about one per week. Since then, only 22 new posts have been added. This is partly because I&#8217;ve had more academic tasks to carry out, a condition that is set to intensify gradually from here on, and partly because I tried to change my standards for what I wanted to blog about (in some vague, as of yet unspecified way).</p>
<p><strong>Scala</strong> is still a very popular topic to blog about, and rightly so, but I no longer feel that I should write about it for the sake of doing so. <a href="http://etorreborre.blogspot.com/">Others do</a> <a href="http://www.codecommit.com/blog/">a much better job</a> of writing about Scala than I could do, because they spend <del>all their time</del> more time with that language. Incidentally, I&#8217;m delighted to see that companies are still switching to Scala quite eagerly, and that Martin Odersky and others launched the company <a href="http://www.typesafe.com/">Typesafe</a> to help others with the transition. Learning Scala has honestly been one of the most empowering experiences I&#8217;ve had as a programmer, and I believe that there is a vast space of possibilities that has yet to be explored in the language. <a href="http://kirkwylie.blogspot.com/2011/01/scala-considered-harmful-for-large.html">Maybe it&#8217;s not a language for everybody</a> (I postpone my judgment on this for now), but if it were in the hands of the right teams with the right discipline, the world would be in a better state. Also, the <a href="http://www.scala-ide.org/">Scala IDE</a> for Eclipse has been vastly, vastly improved since 13 months ago, at which time it could barely be used.</p>
<p>I&#8217;ve become more and more interested in <strong>philosophy </strong>over the past 18 months or so, and this started to show up in the blog during this interval, with <a href="http://www.monomorphic.org/wordpress/category/philosophy/">more and more entries</a> tentatively trying to delineate philosophical questions or positions. Initially I was focussing almost only on Nietzsche, but recently I&#8217;ve also been reading a lot of Foucault, as well as some others. I&#8217;ve probably not been very pedagogical in writing down my thoughts on these topics, but I fear I will never be a pedagogical writer unless I go through some initial struggling attempts. The ideas I&#8217;m most interested in currently are causality (I believe that we don&#8217;t understand it at all) and free will (I believe that its existence is highly questionable, but very fruitful to criticise and reason about).</p>
<p><strong>Popularity. </strong>By far my most popular post has been <a href="http://www.monomorphic.org/wordpress/nomuras-jellyfish/">this little note on Nomura&#8217;s Jellyfish</a>. If I put Google adwords on just that post, I would probably make a lot of money without annoying any other readers. For some reason Google directs a lot of people googling jellyfish to this site. As if programming and philosophy are not more interesting things to Google. Other than that, the Scala posts have been very popular, and following them, <em><a href="http://www.monomorphic.org/wordpress/continuous-computing/">Continuous computing</a></em>, <em><a href="http://www.monomorphic.org/wordpress/type-theory/">Type theory</a></em> and <em><a href="http://www.monomorphic.org/wordpress/the-coming-politicisation-of-mathematics-and-computer-science/">Politicization of mathematics&#8230;</a></em> were able to attract some attention.</p>
<p>From now on, until early next year, I have to focus more and more on finishing my Ph.D. studies; it remains to see how this will affect my blogging.</p>
 <p><a href="http://www.monomorphic.org/wordpress/?flattrss_redirect&amp;id=950&amp;md5=13822c37b5c673df125e8d8a599cb00f" 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/reviewing-the-second-year-of-monomorphic/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>Meta notes: 1+ year with Monomorphic blogging</title>
		<link>http://www.monomorphic.org/wordpress/meta-notes-1-year-with-monomorphic-blogging/</link>
		<comments>http://www.monomorphic.org/wordpress/meta-notes-1-year-with-monomorphic-blogging/#comments</comments>
		<pubDate>Fri, 28 May 2010 15:28:30 +0000</pubDate>
		<dc:creator>johan</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[meta]]></category>
		<category><![CDATA[Natural language]]></category>
		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://www.monomorphic.org/wordpress/?p=607</guid>
		<description><![CDATA[After 13 months and 51 posts, my experiments in blogging continue, although they are perhaps better described as polymorphic than monomorphic. Maybe it&#8217;s time for some reflections. On the whole blogging in this format and at this frequency has been a pretty fun and fulfilling process. I get to practice writing free-form, nonscientific texts, and [...]]]></description>
			<content:encoded><![CDATA[<p>After 13 months and 51 posts, my experiments in blogging continue, although they are perhaps better described as polymorphic than monomorphic. Maybe it&#8217;s time for some reflections.</p>
<p>On the whole blogging in this format and at this frequency has been a pretty fun and fulfilling process. I get to practice writing free-form, nonscientific texts, and even if many of them might not be read by so many people, the idea that they might be turns it into a useful exercise.</p>
<p>Recently Flattr buttons were added to this blog, which allows users who use the service to donate money and show appreciation for my texts (some such people indeed exist &#8211; thanks a lot, all two of you!). Initially I had a single button for the entire blog, but now I am trying out a format where I have one button per post.</p>
<p>I&#8217;ve noticed, on this blog and elsewhere, that I can&#8217;t quite decide if I should write with British or American English. I feel culturally uncertain as a writer of this language. But recently I&#8217;ve come to think that I should embrace my European background, so more of the British variety in the future is a likely prospect.</p>
<p>Topics have been varied. The tag and category systems have been used in an attempt to bring some order to the table, but they&#8217;ve become too chaotic to be useful. A restructuring is perhaps in order during the next 13 months.</p>
<p>One of the most popular topics I&#8217;ve written about has been the Scala language. People tend to google Scala a lot, and it&#8217;s actually really uplifting to see the interest in it (since I hold it to be a way forward). If you are a blogger who wants to get a billion page views, write about Scala. I don&#8217;t want to consciously pander to the readers too much, so in itself it is not a reason for me to write about the topic. I will write about Scala when I want to say something about it. (A difficult principle to really practice.)</p>
<p>I&#8217;ve tried out some different WordPress themes occasionally, but so far I haven&#8217;t found anything I like better than this &#8220;Infinimum&#8221; theme. It feels very clean, functional and modern.</p>
<p>That will be enough of the reflections for now.</p>
 <p><a href="http://www.monomorphic.org/wordpress/?flattrss_redirect&amp;id=607&amp;md5=68bdd5e68e03c4b3aa8202bc26f1f22f" 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/meta-notes-1-year-with-monomorphic-blogging/feed/</wfw:commentRss>
		<slash:comments>2</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>Where is Java going?</title>
		<link>http://www.monomorphic.org/wordpress/where-is-java-going/</link>
		<comments>http://www.monomorphic.org/wordpress/where-is-java-going/#comments</comments>
		<pubDate>Sun, 22 Nov 2009 05:51:16 +0000</pubDate>
		<dc:creator>johan</dc:creator>
				<category><![CDATA[Software development]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[platforms]]></category>
		<category><![CDATA[programming languages]]></category>
		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://www.monomorphic.org/wordpress/?p=391</guid>
		<description><![CDATA[Today, Java is one of the most popular programming languages. Introduced in 1995, it rests on a tripod of the language itself, its libraries, and the JVM. In the TIOBE programming language league charts, it has been at the top for as long as the measurements have been made (since 2002), overtaken by C only [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.monomorphic.org/wordpress/wp-content/uploads/2009/11/creative.jpg"><img class="aligncenter size-medium wp-image-401" title="creative" src="http://www.monomorphic.org/wordpress/wp-content/uploads/2009/11/creative-225x300.jpg" alt="creative" width="225" height="300" /></a></p>
<p>Today, Java is one of the most popular programming languages. Introduced in 1995, it rests on a tripod of the language itself, its libraries, and the JVM. In the <a href="http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html">TIOBE programming language league charts</a>, it has been at the top for as long as the measurements have been made (since 2002), overtaken by C only for a brief period due to measurement irregularities.</p>
<p>Yet not all is <a href="http://www.sun.com/">Sun</a>-shine in Java world. Sun Microsystems is about to be taken over by Oracle, pending EU approval. (EU is really dragging its feet in this matter but it seems unlikely they would really reject the merger). Larry Ellison has voiced strong support for Java and for Sun&#8217;s way of developing software, so maybe this is really not a threat by itself. But how far can the language itself go?</p>
<p>The Java language was carefully designed to be relatively easy to understand and work with. James Gosling, its creator, has <a href="http://www.computer.org/portal/web/csdl/doi/10.1109/2.587548">called it a blue collar language</a>, meaning it was designed for industrial, real world use. In a world where C++ was the de facto standard for OO programming, Java was a big step forward in terms of ease of development, with its lack of pointers and strong type system &#8211; to say nothing of its garbage collection. Many classes of common programming errors were removed altogether. However, in the interests of simplicity and clarity, some tradeoffs were made. The language&#8217;s detractors today point to problems such as excessive verbosity, the lack of closures, the limited generics, and the checked exceptions.</p>
<p>For some time there has been a lot of exciting alternative languages available on the JVM. Clojure is a Lisp dialect. Scala, the only non-Java JVM language I have used extensively, mixes the functional and object oriented paradigms. Languages like JPython and JRuby basically exist to allow scripting and interoperability with popular scripting languages on the JVM.</p>
<p>Today it seems as if the JVM and the standardized libraries will be Java&#8217;s most prominent legacy. The language itself will not go away for a long time either &#8211; considering that many companies still maintain or develop in languages like Cobol and Fortran, we will probably be maintaining Java code 30 years from now (what a sad thought!), but newer and more modern JVM languages will probably take turns being number one. The JVM and the libraries guarantee that we will be able to mix them relatively easily anyway, unless they stray too far from the standard with their custom features.</p>
<p>So in hindsight, developing this intermediate layer, this virtual machine &#8211; and disseminating it so widely &#8211;  was a stroke of genius. Will it be that in future programming models we have even more standardized middle layers, and not just one?</p>
<p>Meanwhile, there&#8217;s a lot of debate about the process being used to shape and define Java. For a long time, Sun employed something called the Java Community Process, JCP, which was supposed to ensure openness. Some people <a href="http://www.jroller.com/scolebourne/entry/no_more_java_7">proclaim</a> that the openness has ended. To take one example, very recently, Sun announced that there will be support for closures in Java 7, after first announcing that there would be no support for closures in Java 7. The process by which this decision has been managed has been <a href="http://blogs.tedneward.com/2009/11/19/Closures+Are+Back+Again.aspx">described as</a> not being a community effort. Some aspects of Java are definitely up in the air these days.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.monomorphic.org/wordpress/where-is-java-going/feed/</wfw:commentRss>
		<slash:comments>1</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>
	</channel>
</rss>

