<?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>LolocoJr &#187; scala</title>
	<atom:link href="http://www.railsguru.com/articles/tag/scala/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.railsguru.com</link>
	<description>Andy Lo-A-Foe&#039;s blog</description>
	<lastBuildDate>Fri, 09 Jul 2010 12:08:16 +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>First stab at Scala scripting.. filters do not work like my Ruby brain thinks they do</title>
		<link>http://www.railsguru.com/articles/2009/07/10/first-stab-at-scala-scripting-filters-do-not-work-like-my-ruby-brain-thinks-they-do/</link>
		<comments>http://www.railsguru.com/articles/2009/07/10/first-stab-at-scala-scripting-filters-do-not-work-like-my-ruby-brain-thinks-they-do/#comments</comments>
		<pubDate>Fri, 10 Jul 2009 15:36:23 +0000</pubDate>
		<dc:creator>andy</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://www.railsguru.com/?p=4551</guid>
		<description><![CDATA[<p>So I decided to pick up the Scala language. Having done Ruby for a couple of years now Scala is one of the few, if not the only language that&#8217;s gotten me excited. Being a first class citizen on the JVM has many advantages and I&#8217;m really liking LiftWeb.</p>
<p>I also suspect I&#8217;ll be doing a [...]]]></description>
			<content:encoded><![CDATA[<p>So I decided to pick up the Scala language. Having done Ruby for a couple of years now Scala is one of the few, if not the only language that&#8217;s gotten me excited. Being a first class citizen on the JVM has many advantages and I&#8217;m really liking <a href="http://liftweb.net">LiftWeb</a>.</p>
<p>I also suspect I&#8217;ll be doing a bit more Java related stuff in the very near future (stay tuned for that one <img src='http://www.railsguru.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  ) so it&#8217;s a good idea to dive  into the J world again, and what better way then with Scala!</p>
<p>Problem definition: there is an issue with some XML files which came with a large batch ingest for one of our clients. In short there are a bunch of track elements in the XML which each element having a on_disc and sequence_number (think audio CDs) . However, the sequence_numbers should always be incremented e.g. if you have 2 tracks on disc 1 and 3 tracks on disc 2 the tracks on disc 2 should be have sequence numbers 3,4 and 5 respectively. The XML files we got have reset the sequence_number to 1 whenever a new disc begins.The fix is thus a simple matter of adding the highest sequence number of the previous disc to the sequence number of the current disc. Easy enough.</p>
<p>I tried to implement this in Scala and one of the helper methods calculates the highest sequence number of a given disc. This is the odd part though</p>
<pre class="brush:scala">
// The XML is first parsed to read all tracks
// These are stored in a tuple list
// Note the order of the tuples!
// Tuple values are (on_volume, sequence_number)

val sequence = List((1,2),(1,1),(2,1),(2,2),(2,3))

// Find the max value given a volume
def maxVal(vol: Int): Int = {
  var max = 1
  for (t <- sequence
    if t._1 == vol;
    if t._2 > max) max = t._2
 max
}

println(maxVal(1))
</pre>
<p>Now my Ruby brain would expect this to print out <b>2</b>, however when you run this code the result will be <b>1</b>! That&#8217;s because the filters are actually creating a new list before feeding that to for clause  so max is always 1 in the test. I did not expect this <img src='http://www.railsguru.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.railsguru.com/articles/2009/07/10/first-stab-at-scala-scripting-filters-do-not-work-like-my-ruby-brain-thinks-they-do/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
