<?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>Ron on software &#187; Scala</title>
	<atom:link href="http://rpstechnologies.net/ron/blog/category/scala/feed/" rel="self" type="application/rss+xml" />
	<link>http://rpstechnologies.net/ron/blog</link>
	<description>Thoughts on software development</description>
	<lastBuildDate>Thu, 23 Jun 2011 23:02:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Using Scala for something I&#8217;d normally using a &#8220;scripting&#8221; language for</title>
		<link>http://rpstechnologies.net/ron/blog/2010/01/using-scala-for-something-id-normally-using-a-scripting-language-for/</link>
		<comments>http://rpstechnologies.net/ron/blog/2010/01/using-scala-for-something-id-normally-using-a-scripting-language-for/#comments</comments>
		<pubDate>Wed, 13 Jan 2010 05:21:13 +0000</pubDate>
		<dc:creator>ron</dc:creator>
				<category><![CDATA[Scala]]></category>

		<guid isPermaLink="false">http://rpstechnologies.net/ron/blog/2010/01/using-scala-for-something-id-normally-using-a-scripting-language-for/</guid>
		<description><![CDATA[From time to time, some repetitive task comes up that I can do quicker by writing a script to do it than to do it manually. Especially if it&#8217;s something I may be needing to do again in the future. &#8230; <a href="http://rpstechnologies.net/ron/blog/2010/01/using-scala-for-something-id-normally-using-a-scripting-language-for/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>From time to time, some repetitive task comes up that I can do quicker by writing a script to do it than to do it manually. Especially if it&#8217;s something I may be needing to do again in the future.</p>
<p>Usually I&#8217;d turn to a &#8220;scripting&#8221; language like Python, Groovy, or back in the day, Perl for this type of thing.</p>
<p>Today such a need came up, and I decided to try tackling it with Scala since it has many of the features that make the above dynamic languages good for this:</p>
<ul>
<li>first-class support for map and list data structures</li>
<li>an interactive shell</li>
<li>minimal overhead to write a program, compile, and run it</li>
<li>support for functional programming</li>
<li>good regular expression support</li>
</ul>
<p>The problem:<br />
A small performance test program ran a large number of tests, and measured the elapsed time for each execution. The program output a line like &#8220;request completed in 10451 msecs&#8221; for each test. I needed to parse the output, collect the elapsed time measurements, and get some basic statistics on them; simple average, minimum, and maximum.</p>
<p>I used a Scala 2.8 snapshot, and fleshed out the code using the Scala interactive shell. First, define a value with the raw output to be processed:</p>
<pre class="brush: bash; gutter: false; title: ; notranslate">
scala&gt; val rawData = &quot;&quot;&quot;request completed in 10288 msecs
     | request completed in 10321 msecs
     | request completed in 10347 msecs
     | request completed in 10451 msecs
     | request completed in 10953 msecs
     | request completed in 11122 msecs
... hundreds of lines ...
     | request completed in 11672 msecs&quot;&quot;&quot;
</pre>
<p>The above uses Scala&#8217;s support for <a href="http://langref.org/scala/strings/declaring-strings/multiline">multi-line string literals</a>.</p>
<p>The next thing I needed to do was parse the above output, using a regular expression to extract just the milliseconds. There&#8217;s several ways to create a regular expression in Scala. This is the one I like:</p>
<pre class="brush: bash; gutter: false; title: ; notranslate">val ReqCompletedRE = &quot;&quot;&quot;\s*request completed in (\d+) msecs&quot;&quot;&quot;r
</pre>
<p>There&#8217;s a bit of magic in how the above string literal actually ends up becoming a regular expression.  There&#8217;s an <a href="http://www.scala-lang.org/node/130">implicit conversion</a> in the Scala <a href="http://www.scala-lang.org/docu/files/api/scala/Predef$object.html">Predef object</a> which turns a Java String into a RichString. RichString provides a &#8216;r&#8217; method that returns a regular expression object. The members of the Predef object are automatically imported into every Scala module, so the Scala compiler will attempt to apply any conversions it finds in Predef when trying to resolve the &#8216;r&#8217; method. So the above expression is creating a RichString from a String via an implicit conversion, then calling the &#8216;r&#8217; method on it, which returns the regular expression.</p>
<p>To apply the regular expression to a line of the output and to extract the milliseconds, we can use an expression like:</p>
<pre class="brush: bash; gutter: false; title: ; notranslate">scala&gt; val ReqCompletedRE(msecs) = &quot; request completed in 10451 msecs&quot;
msecs: String = 10451
</pre>
<p>msecs gets bound to the first group in the regular expression (the part that matches (\d+)).  This takes place via the <a href="http://www.scala-lang.org/node/112">Scala extractors</a> feature &#8211; the scala <a href="http://www.scala-lang.org/docu/files/api/scala/util/matching/Regex.html">regular expression class</a> defines an extractor which extracts the grouping results.</p>
<p>The next step is to iterate over the lines of the output, extract the milliseconds, and turn the results into a list.</p>
<pre class="brush: bash; gutter: false; title: ; notranslate">scala&gt; val msecsVals = rawData.lines.map { line =&gt; val ReqCompletedRE(msecs) = line; Integer.parseInt(msecs);} toList
res11: List[Int] = List(10288, 10321, 10347, 10451, 10953, 11122, ..., 11672)
</pre>
<p>The above code is using the <a href="http://www.scala-lang.org/docu/files/api/scala/runtime/RichString.html#lines">RichString lines</a>, <a href="http://www.scala-lang.org/docu/files/api/scala/Iterator.html#map%28%28A%29%3D%3EB%29">Iterator.map method</a>, along with Scala closures.</p>
<p>Finally, to get the simple statistics:</p>
<pre class="brush: bash; gutter: false; title: ; notranslate">scala&gt; (msecsVals.sum / msecsVals.length, msecsVals.min, msecsVals.max)
res21: (Int, Int, Int) = (10736,10288,11672)
</pre>
<p>Putting the whole script together:</p>
<pre class="brush: bash; gutter: false; title: ; notranslate">val ReqCompletedRE = &quot;&quot;&quot;\s*request completed in (\d+) msecs&quot;&quot;&quot;r
val msecsVals = rawData.lines.map { line =&amp;amp;amp;gt; val ReqCompletedRE(msecs) = line; Integer.parseInt(msecs);} toList
(msecsVals.sum / msecsVals.length, msecsVals.min, msecsVals.max)
</pre>
]]></content:encoded>
			<wfw:commentRss>http://rpstechnologies.net/ron/blog/2010/01/using-scala-for-something-id-normally-using-a-scripting-language-for/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Scala, Maven and IntelliJ</title>
		<link>http://rpstechnologies.net/ron/blog/2009/10/maven-scala-and-intellij/</link>
		<comments>http://rpstechnologies.net/ron/blog/2009/10/maven-scala-and-intellij/#comments</comments>
		<pubDate>Fri, 09 Oct 2009 03:44:46 +0000</pubDate>
		<dc:creator>ron</dc:creator>
				<category><![CDATA[Scala]]></category>
		<category><![CDATA[maven]]></category>

		<guid isPermaLink="false">http://rpstechnologies.net/ron/blog/2009/10/maven-scala-and-intellij/</guid>
		<description><![CDATA[I&#8217;m evaluating Scala for a project I&#8217;ve recently started.  The project requires processing a large number of data structures.  Between Scala&#8217;s good support for parallelism and functional programming, it seems like a good fit.  I&#8217;ve heard Scala brings many of &#8230; <a href="http://rpstechnologies.net/ron/blog/2009/10/maven-scala-and-intellij/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m evaluating Scala for a project I&#8217;ve recently started.  The project requires processing a large number of data structures.  Between Scala&#8217;s good support for parallelism and functional programming, it seems like a good fit.  I&#8217;ve heard Scala brings many of the development efficiencies and expressiveness of dynamic languages, while being statically typed.  It does this through a number of features, including smart type inferencing, great functional programming support, traits, and a bunch of other tricks I&#8217;m in the process of learning.</p>
<p>We&#8217;ve seen several dynamically typed languages gain widespread adoption in the last eight years or so&#8230; Python, Ruby, Groovy, and you can go back to Javascript, Perl, PHP, etc..  What&#8217;s the last statically typed language that&#8217;s gained a wide following?  C#?  I&#8217;ve pretty much come to assume that any new language that was highly expressive, concise, and good for DSLs, would be a dynamic language.  Dynamic languages do have their drawbacks..  performance is usually not up to par with statically typed languages (although it rarely makes a difference), and the lack of type information at compile type make it very difficult for IDEs to provide features such as autocomplete, symbol lookup, and early error detection.  So if languages such as Scala manage to deliver a highly development-efficient language while retaining static typing, that&#8217;s all the better in my eyes.</p>
<p>Here&#8217;s what I did to get a minimal project set up in Maven and IntelliJ IDEA:</p>
<p>Create a shell of a project using a Maven archetype:</p>
<pre class="brush: bash; gutter: false; title: ; notranslate">
mvn org.apache.maven.plugins:maven-archetype-plugin:1.0-alpha-7:create \
-DarchetypeGroupId=org.scala-tools.archetypes \
-DarchetypeArtifactId=scala-archetype-simple \
-DarchetypeVersion=1.1 \
-DremoteRepositories=http://scala-tools.org/repo-releases \
-DgroupId=com.rps -DartifactId=scalatest
</pre>
<p>This created a project with a single Scala class, test, and a Maven POM containing the minimal dependencies and plugins.</p>
<p>The version of Scala generated by the archetype isn&#8217;t the most current.  Just update the version in the POM:</p>
<pre class="brush: xml; gutter: false; title: ; notranslate">&lt;scala.version&gt;2.6.1&lt;/scala.version&gt;</pre>
<p>to 2.7.6, or whatever is the version you want.</p>
<p>I then imported the project into IntelliJ IDEA using the excellent IDEA Maven integration.  I had to add the Scala nature to the project in IDEA myself, but otherwise, everything is working..  Editing, unit tests, debugging, etc.</p>
<p>The IntelliJ IDEA editor support for autocomplete seems to work pretty well, but it doesn&#8217;t seem to do much detection of errors as you&#8217;re editing yet.</p>
]]></content:encoded>
			<wfw:commentRss>http://rpstechnologies.net/ron/blog/2009/10/maven-scala-and-intellij/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

