<?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; programming languages</title>
	<atom:link href="http://www.monomorphic.org/wordpress/tag/programming-languages/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.monomorphic.org/wordpress</link>
	<description>Nystrom re-presents</description>
	<lastBuildDate>Tue, 27 Jul 2010 02:12:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Doing generality right</title>
		<link>http://www.monomorphic.org/wordpress/doing-generality-right/</link>
		<comments>http://www.monomorphic.org/wordpress/doing-generality-right/#comments</comments>
		<pubDate>Fri, 02 Apr 2010 02:00:02 +0000</pubDate>
		<dc:creator>johan</dc:creator>
				<category><![CDATA[Philosophy]]></category>
		<category><![CDATA[Software development]]></category>
		<category><![CDATA[apple]]></category>
		<category><![CDATA[applications]]></category>
		<category><![CDATA[HCI]]></category>
		<category><![CDATA[platforms]]></category>
		<category><![CDATA[programming languages]]></category>
		<category><![CDATA[reuse]]></category>
		<category><![CDATA[smalltalk]]></category>
		<category><![CDATA[software engineering]]></category>

		<guid isPermaLink="false">http://www.monomorphic.org/wordpress/?p=558</guid>
		<description><![CDATA[Many software developers, while making a tool to solve a specific problem, heed the siren call of generality. By making a few specific changes, they can turn the tool into a general framework for solving a larger class of problems. And then, with a few more changes, an even larger class of problems, and so [...]]]></description>
			<content:encoded><![CDATA[<p>Many software developers, while making a tool to solve a specific problem, heed the siren call of generality. By making a few specific changes, they can turn the tool into a general framework for solving a larger class of problems. And then, with a few more changes, an even larger class of problems, and so on. This often turns into a trap, and there is a risk that the end of the line is an over-generalised tool that isn&#8217;t very good at solving any problem, because the specificity that was present in the first place was part of why it was powerful. In this way, constraints can equal freedom.</p>
<p>Sometimes, though, the generalizers get it right. These are often moments of exceptional and lasting innovation. One example of such a system is the fabulously influential (but today, not that widely used) programming language <a href="http://www.smalltalk.org">Smalltalk</a>. Invented by the former jazz guitarist and subsequent Turing award winner <a href="http://en.wikipedia.org/wiki/Alan_Kay">Alan Kay</a>, Smalltalk was released as one of the first true object-oriented programming languages in 1980. It is probably still ahead of its time. It runs on a virtual machine, it has reflection, everything is an object, and the separation between applications is blurred in favour of a big object box. On running <a href="http://www.squeak.org/">Squeak</a>, a popular Smalltalk implementation, with its default system image today, users discover that all the objects on the screen, including the IDE to develop and debug objects, appear to follow the same rules. No objects seem to have special privileges.</p>
<p>Another such system is an application that used to be shipped on Mac computers in the distant past, <a href="http://en.wikipedia.org/wiki/HyperCard">Hypercard</a>. Hypercard enabled ordinary users to create highly customized software using the idea of filing cards in a drawer as the underlying model, blurring the line between end users and developers through its accessibility. I haven&#8217;t had the privilege to use it myself, but it seems like this was as powerful as it was because it served up a homogenous and familiar model, where everything was a card, and yet the cards had considerable scope for modification and special features. Even though, in some ways, this system appears to be a database, the cards didn&#8217;t need to have the same format, for instance. (Are we seeing this particular idea being recycled in a more enterprisey form in <a href="http://couchdb.apache.org/">CouchDB</a>?)</p>
<p>There are more examples of successful highly general design: the Unix file system, TCP/IP sockets and so on. They all have in common that they are easy to think about as a mental model, since a universal set of rules apply to all objects, they scale well in different directions when used for different purposes, and they give the user a satisfying sense of empowerment, <a href="http://www.monomorphic.org/wordpress/making-playtime-useful-with-color-filling-games/">blurring the line between work and play</a> to draw on the user&#8217;s natural creativity. Successful general systems are the ones that can be easily applied in quite varied situations without tearing in the seams.</p>
<p>While not widely used by industrial programmers today, Smalltalk was incredibly influential. In 1981 Objective-C was created by Brad Cox and Tom Love, directly inspired by what the Smalltalk designers had done. Objective-C was subsequently used as the language of choice for NeXTStep, and later for Apple&#8217;s MacOS X when Apple bought NeXT. Today it&#8217;s seeing a big surge in popularity thanks to devices like the iPhone, on which it is also used. In 1995 Java was introduced, owing a great deal of its design to Objective-C, but also introducing features such as a universal virtual machine and garbage collection, which Objective-C didn&#8217;t have at the time. In some sense, both Objective-C and Java are blends of the C-family languages and Smalltalk. Tongue in cheek, we might say that it seems evolution in industrial programming these days consists of finding blends that contain less of the C model and more of smalltalk or functional programming.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.monomorphic.org/wordpress/doing-generality-right/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Overloading words in research and programming</title>
		<link>http://www.monomorphic.org/wordpress/overloading-words-in-research-and-programming/</link>
		<comments>http://www.monomorphic.org/wordpress/overloading-words-in-research-and-programming/#comments</comments>
		<pubDate>Thu, 11 Mar 2010 06:16:22 +0000</pubDate>
		<dc:creator>johan</dc:creator>
				<category><![CDATA[Philosophy]]></category>
		<category><![CDATA[Software development]]></category>
		<category><![CDATA[human condition]]></category>
		<category><![CDATA[Natural language]]></category>
		<category><![CDATA[ontologies]]></category>
		<category><![CDATA[programming languages]]></category>
		<category><![CDATA[research]]></category>
		<category><![CDATA[software engineering]]></category>

		<guid isPermaLink="false">http://www.monomorphic.org/wordpress/?p=540</guid>
		<description><![CDATA[In research and academia, one of the fundamental activities is the invention and subsequent examination of new concepts. For concepts, we need names. One way of making a name is stringing words together until the meaning is sufficiently specific. E.g. &#8220;morphism averse co-dependent functor substitutions in virtual machine transmigration systems&#8221;. Thus the abstruse academic research [...]]]></description>
			<content:encoded><![CDATA[<p>In research and academia, one of the fundamental activities is the invention and subsequent examination of new concepts. For concepts, we need names.</p>
<p>One way of making a name is stringing words together until the meaning is sufficiently specific. E.g. &#8220;morphism averse co-dependent functor substitutions in virtual machine transmigration systems&#8221;. Thus the abstruse academic research paper title is born.</p>
<p>Sciences sometimes give new meanings to existing words. This could be called overloading, following the example of object-oriented programming. E.g. a &#8220;group&#8221; in mathematics is something different from the everyday use of the term. A &#8220;buffer&#8221; in chemistry is something different from a software or hardware buffer, even though a fragment of similarity is there. And so on. This overloading of words gives newcomers to the field a handle on what is meant, but full understanding is still impossible without understanding the actual definitions being employed.</p>
<p>Sometimes new terms can be created using inventors&#8217; names and everyday words. E.g. a &#8220;Lie group&#8221; or the &#8220;Maxwell equations&#8221;, or &#8220;Curry-Howard correspondence&#8221;. This is potentially useful, but perhaps not something you can do freely with your own research without seeming like you&#8217;re trying to inflate your ego excessively. (Even though researchers love inflating their egos, nobody wants to admit it.)</p>
<p>There&#8217;s a similar problem in software development. When we invent names of functions, classes and variables, the lack of words becomes very clear. Intuitively, what is an &#8220;adapter registry&#8221;? An &#8220;observer list&#8221;? Or an &#8220;observer list mediation adapter?&#8221; My feeling is that we often end up compounding abstract words because we have no better choice. And here lies a clue to some of the apparent impermeability of difficult source code. We need better ways of making names. We&#8217;re inventing ideas faster than our language can stretch.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.monomorphic.org/wordpress/overloading-words-in-research-and-programming/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A wikipedia of algorithms</title>
		<link>http://www.monomorphic.org/wordpress/a-wikipedia-of-algorithms/</link>
		<comments>http://www.monomorphic.org/wordpress/a-wikipedia-of-algorithms/#comments</comments>
		<pubDate>Mon, 07 Dec 2009 01:21:52 +0000</pubDate>
		<dc:creator>johan</dc:creator>
				<category><![CDATA[Software development]]></category>
		<category><![CDATA[programming languages]]></category>
		<category><![CDATA[reuse]]></category>
		<category><![CDATA[social media]]></category>
		<category><![CDATA[software engineering]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://www.monomorphic.org/wordpress/?p=429</guid>
		<description><![CDATA[Here&#8217;s something I&#8217;ve wanted to see for some time, but probably don&#8217;t have time to work on myself. It would be nice if there was a wikipedia-like web site for code and algorithms. Just the common ones to start with, but perhaps more specialised ones over time. Of course the algorithms should be available in [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s something I&#8217;ve wanted to see for some time, but probably don&#8217;t have time to work on myself.</p>
<p>It would be nice if there was a wikipedia-like web site for code and algorithms. Just the common ones to start with, but perhaps more specialised ones over time. Of course the algorithms should be available in lots of different languages. This would in fact be one of the main points, so that people could compare good style and see how things should be done for different languages. In addition, there should be an in-browser editor, just like on Wikipedia (but perhaps with syntax highlighting) so people can make changes easily.</p>
<p>Furthermore, there should be unit tests for every algorithm, and these should be user-editable in the same way as the main code. In an ideal world, the web site would automatically run the unit tests every time there&#8217;s a change to some algorithm and check in a new version of the code to a versioned repository. People could then trust with reasonable confidence that the code is valid and safe. However, if the system were to be as open as Wikipedia is, such a system wouldn&#8217;t work, since users could write unit tests with malicious code. So I suspect volunteers would have to download, inspect, and run the unit tests regularly, and perhaps there would be a meta-moderation system of some kind, allowing senior members to promote changes to the official repository. In the meantime, everybody should be allowed to see and edit changes on the wiki immediately, but they would be marked as &#8220;untested&#8221; or &#8220;unsafe&#8221;.</p>
<p>User interface would be very important since this kind of site needs to be fun and easy to use regularly.</p>
<p>Has this kind of project already been carried out by someone? I can find some things by googling. <a href="http://www.thecodewiki.com/">The Code Wiki</a> appears to once have been a wikipedia of code, but it seems defunct, C# only, and now they&#8217;re selling a book with the contents of the site! <a href="http://www.algorithm-code.com/">Algorithm Wiki</a> has many algorithms in different languages, but the user interface is awkward and littered with obstructive advertising, the code is hard to browse, and it doesn&#8217;t make for a usable quick reference. They seem to have gotten off to a good start though. Any others?</p>
<p>Edit: <a href="http://rosettacode.org/wiki/Main_Page">Rosetta Code</a> seems to be the most mature and useful such site out there today.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.monomorphic.org/wordpress/a-wikipedia-of-algorithms/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>0</slash:comments>
		</item>
		<item>
		<title>Programming languages are about people</title>
		<link>http://www.monomorphic.org/wordpress/programming-languages-are-about-people/</link>
		<comments>http://www.monomorphic.org/wordpress/programming-languages-are-about-people/#comments</comments>
		<pubDate>Mon, 28 Sep 2009 14:40:43 +0000</pubDate>
		<dc:creator>johan</dc:creator>
				<category><![CDATA[Software development]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[programmer psychology]]></category>
		<category><![CDATA[programming languages]]></category>

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

<p>On line 2, there&#8217;s a label <tt>outer:</tt> which identifies a location in the code. Normally this feature is used with keywords like the <a href="http://www.u.arizona.edu/~rubinson/copyright_violations/Go_To_Considered_Harmful.html">hotly debated</a> <tt>goto</tt> in C. Java has <tt>goto</tt> as a keyword, but doesn&#8217;t support the feature. However, you can still use the labels with statements like <tt>continue</tt> above (line 12), which in this case starts a new iteration of the outer loop rather than the inner one.</p>
<p>I can&#8217;t remember ever having had to use this feature of any C-like language before (perhaps once) so it was intriguing when it popped up. It&#8217;s possible that a neater implementation of this would put the inner loop in a <tt>matchesSignature</tt> method in the <tt>IMethod</tt> interface instead.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.monomorphic.org/wordpress/an-unusual-java-construct/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Languages and automata, part 1</title>
		<link>http://www.monomorphic.org/wordpress/languages-and-automata-part-1/</link>
		<comments>http://www.monomorphic.org/wordpress/languages-and-automata-part-1/#comments</comments>
		<pubDate>Mon, 06 Jul 2009 02:50:52 +0000</pubDate>
		<dc:creator>johan</dc:creator>
				<category><![CDATA[Computer science]]></category>
		<category><![CDATA[Philosophy]]></category>
		<category><![CDATA[metaphors]]></category>
		<category><![CDATA[nature]]></category>
		<category><![CDATA[programming languages]]></category>
		<category><![CDATA[society]]></category>
		<category><![CDATA[state]]></category>

		<guid isPermaLink="false">http://www.monomorphic.org/wordpress/?p=150</guid>
		<description><![CDATA[Computing is very new as a science. Blaise Pascal devised a mechanical calculator in 1645, but Charles Babbage&#8217;s analytical engine, widely considered the first programmable computer, was not conceived of until the mid-19th century. However, it was never constructed (unlike Babbage&#8217;s simpler &#8220;difference engine&#8221;), and even at this time there was almost no theory to [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.monomorphic.org/wordpress/wp-content/uploads/2009/07/yoyogi.jpg"><img class="alignleft size-medium wp-image-153" style="margin: 10px;" title="Yoyogi, Tokyo" src="http://www.monomorphic.org/wordpress/wp-content/uploads/2009/07/yoyogi-225x300.jpg" alt="Yoyogi, Tokyo" width="225" height="300" /></a>Computing is very new as a science. Blaise Pascal devised a mechanical calculator in 1645, but Charles Babbage&#8217;s analytical engine, widely considered the first programmable computer, was not conceived of until the mid-19th century. However, it was never constructed (unlike Babbage&#8217;s simpler &#8220;difference engine&#8221;), and even at this time there was almost no theory to go with the invention. Today, the fundamental abstractions of computing and programming are Turing machines and Lambda calculus, described in the 1930&#8242;s. So essentially, the theory has had less than a century to mature, and is being viewed by many as a branch of mathematics.</p>
<p>The newness of computing means that we don&#8217;t know that much about its role or its applicability outside of devices built specifically for computing, nor do we know if today&#8217;s fundamental computing abstractions are the best ones.</p>
<p>Languages and automata are two of the most fundamental ideas in computing. In contrast to human languages, which are informal and rather unsystematic, in computing we often speak of formal languages. Something like the following is an example of a formal grammar:</p>
<ul>
<li><em>Sequence-list:</em> <em>Sequence [ Sequence-list ]</em></li>
<li><em>Sequence: </em>Wake up <em>Action-list </em>Have lunch <em>Action-list </em>Go to sleep</li>
<li><em>Action-list: Action [ Action-list ]</em></li>
<li><em>Action: </em>Work | Answer the phone | Attend meeting | Relax</li>
</ul>
<p>Using this grammar we can model the life of an office worker. We can generate an infinite list of potentially infinitely long &#8220;sentences&#8221;. The following are examples of valid sentences in the grammar:</p>
<ul>
<li>Wake up, Work, Have lunch, Attend meeting, Go to sleep</li>
<li>Wake up, Work, Have lunch, Work, Go to sleep, Wake up, Work, Have lunch, Work, Go to sleep</li>
<li>Wake up, Answer the phone, Answer the phone, Answer the phone, Have lunch, Work, Go to sleep</li>
</ul>
<p>A grammar such as this has a 1-1 correspondence with what is known as a deterministic finite automaton (DFA) &#8211; a very simple building block of software and hardware models. A formal grammar like the above is in a sense just a more natural way of thinking about a DFA.</p>
<p>What is the applicability of formal languages outside computing hardware and software?</p>
<p><a href="http://www.monomorphic.org/wordpress/wp-content/uploads/2009/07/ferns.jpg"><img class="aligncenter size-medium wp-image-162" title="Ferns. Kyoto, Japan" src="http://www.monomorphic.org/wordpress/wp-content/uploads/2009/07/ferns-300x225.jpg" alt="Ferns. Kyoto, Japan" width="300" height="225" /></a></p>
<p>For one thing, we see them in nature, not least in ferns, which on a miniature level appear to have used the same rules as on the macro level. We see them in trees and flowers. In fact, the formal language paradigm appears to be a very good fit for many natural phenomena. One reason for this might be that formal languages allow rich structures to be constructed from a very small description.</p>
<p>One idea I find fascinating is trying to apply these models to human society: people and institutions. Can we describe the interactions in society as automata and formal languages, and if so, what can we learn about them?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.monomorphic.org/wordpress/languages-and-automata-part-1/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Exploring Scala</title>
		<link>http://www.monomorphic.org/wordpress/exploring-scala/</link>
		<comments>http://www.monomorphic.org/wordpress/exploring-scala/#comments</comments>
		<pubDate>Thu, 07 May 2009 19:10:06 +0000</pubDate>
		<dc:creator>johan</dc:creator>
				<category><![CDATA[Software development]]></category>
		<category><![CDATA[programming languages]]></category>
		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://www.monomorphic.org/wordpress/?p=15</guid>
		<description><![CDATA[I&#8217;ve started experimenting with the programming language Scala. I&#8217;ve been wanting to get back into functional programming for some time, but I&#8217;ve found it impractical for the time being to dive right into something like ML, Haskell or Scheme. Scala has gained notoriety since Twitter announced that they&#8217;ve rewritten their engine in it. Some of [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve started experimenting with the programming language <a href="http://www.scala-lang.org">Scala</a>. I&#8217;ve been wanting to get back into functional programming for some time, but I&#8217;ve found it impractical for the time being to dive right into something like ML, Haskell or Scheme. Scala has gained notoriety since Twitter announced that they&#8217;ve rewritten their engine in it. Some of its benefits are:</p>
<ul>
<li>Mixes multiple paradigms, including imperative, functional and actor programming</li>
<li>Runs on the Java VM &#8211; interop with Java libraries and frameworks is trivial</li>
<li>Lightweight syntax</li>
</ul>
<p>There is much to like in this. I&#8217;m hoping that it will turn out to be useful as a rapid prototyping language for trying things out, especially since so many third party tools are available in Java world.</p>
<p>For now I am using exercises fromÂ <a href="http://www.projecteuler.net">Project Euler</a>Â as a means to experiment with and learn it. This works surprisingly well.</p>
<p>Also see this list of <a href="http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html">the most popular programming languages</a>Â - Scala is now number 27, ahead of Prolog, Erlang, Haskell and ML.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.monomorphic.org/wordpress/exploring-scala/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
