<?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>Ståle&#039;s weblog &#187; TheDailyWTF</title>
	<atom:link href="http://blog.staale.org/tag/thedailywtf/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.staale.org</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Mon, 18 Oct 2010 07:43:50 +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>Another pointless challenge solved in Scala</title>
		<link>http://blog.staale.org/2009/07/another-pointless-challenge-solved-in-scala.html</link>
		<comments>http://blog.staale.org/2009/07/another-pointless-challenge-solved-in-scala.html#comments</comments>
		<pubDate>Mon, 27 Jul 2009 12:06:47 +0000</pubDate>
		<dc:creator>Staale</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Scala]]></category>
		<category><![CDATA[TheDailyWTF]]></category>

		<guid isPermaLink="false">http://blog.staale.org/?p=124</guid>
		<description><![CDATA[I am an avid reader of The Daily WTF, and recently there was a small challenge posted on the site. It deals with something called Russian multiplication. The explanation on the site is much better to understand than something I could reproduce myself, so I urge you to read the full text there. Basically, in [...]]]></description>
			<content:encoded><![CDATA[<p>I am an avid reader of <a title="The Daily WTF" href="http://thedailywtf.com">The Daily WTF</a>, and recently there was a small <a href="http://thedailywtf.com/Articles/Programming-Praxis-Russian-Peasant-Multiplication.aspx">challenge</a> posted on the site. It deals with something called Russian multiplication. The explanation on the site is much better to understand than something I could reproduce myself, so I urge you to read the full text there. Basically, in order to multiply 2 numbers, you set them in 2 columns. You then add a row with half and double of the first and second number in the previous row. Keep doing that until you have reached 1. Here is a simple table example for 47*131:</p>
<table border="0">
<tbody>
<tr>
<td>47</td>
<td>131</td>
</tr>
<tr>
<td>23</td>
<td>262</td>
</tr>
<tr>
<td>11</td>
<td>524</td>
</tr>
<tr>
<td>5</td>
<td>1048</td>
</tr>
<tr>
<td>2</td>
<td>2096</td>
</tr>
<tr>
<td>1</td>
<td>4192</td>
</tr>
</tbody>
</table>
<p>You then add all the numbers in the second column, when the number in the first column is odd, 131 + 262 + 524 + 1048 + 4192 = 6157.</p>
<p>Now to create a small Scala program that can solve this. First, I need a method that just produces the table above. This is rather simple to create:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">def</span> sumParts<span style="color: #F78811;">&#40;</span>a<span style="color: #000080;">:</span>Int, b<span style="color: #000080;">:</span>Int<span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> List<span style="color: #F78811;">&#91;</span><span style="color: #F78811;">&#40;</span>Int,Int<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> 
    <span style="color: #0000ff; font-weight: bold;">if</span> <span style="color: #F78811;">&#40;</span>a<span style="color: #000080;">==</span><span style="color: #F78811;">0</span><span style="color: #F78811;">&#41;</span> List<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> <span style="color: #0000ff; font-weight: bold;">else</span> <span style="color: #F78811;">&#40;</span>a,b<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">::</span> sumParts<span style="color: #F78811;">&#40;</span>a/<span style="color: #F78811;">2</span>, b<span style="color: #000080;">*</span><span style="color: #F78811;">2</span><span style="color: #F78811;">&#41;</span></pre></td></tr></table></div>

<p>Notice the list type here is (Int, Int), which indicates it&#8217;s a list of tuples. If you have used scripting languages, you might be aware of tuples already. Tuples in Scala allows us to define a list like type with fixed size, and fixed type for each value. This is a tuple of 2 Ints, and the actual type off the method is a List of tuples, each with 2 Ints. If we send 47,131 as arguments to this method, we will get List((47,131), (23,262), (11,524), (5,1048), (2,2096), (1,4192)) in return. We can now iterate over this list to create a presentable output:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">val</span> numbers <span style="color: #000080;">=</span> sumParts<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">47</span>, <span style="color: #F78811;">131</span><span style="color: #F78811;">&#41;</span>
<span style="color: #0000ff; font-weight: bold;">for</span> <span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#40;</span>a,b<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">&lt;</span>- numbers<span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
    println<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;% 8d % 8d %5s&quot;</span> format<span style="color: #F78811;">&#40;</span>a, b, <span style="color: #0000ff; font-weight: bold;">if</span> <span style="color: #F78811;">&#40;</span>a<span style="color: #000080;">%</span>2<span style="color: #000080;">==</span><span style="color: #F78811;">1</span><span style="color: #F78811;">&#41;</span> <span style="color: #6666FF;">&quot;*&quot;</span> <span style="color: #0000ff; font-weight: bold;">else</span> <span style="color: #6666FF;">&quot;&quot;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
<span style="color: #F78811;">&#125;</span></pre></td></tr></table></div>

<p>Which produces this output:</p>
<pre>
      47      131     *
      23      262     *
      11      524     *
       5     1048     *
       2     2096
       1     4192     *
</pre>
<p>To format the output nicely, I use the format operator on the string. I have used the &#8220;%&#8221; operator to format stuff extensivly when printing in python, so I was happy to find the same functionality easily available in Scala. It would be nice if they also added a &#8220;%&#8221; overloaded method for RichString in Scala for this. I also use an if to print out a &#8220;*&#8221; next to each number that should be included in the addition. Next we need to filter out the numbers we actually need for the addition, and add them together.</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;">val</span> addNums <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">for</span> <span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#40;</span>a,b<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">&lt;</span>- numbers<span style="color: #000080;">;</span> <span style="color: #0000ff; font-weight: bold;">if</span> <span style="color: #F78811;">&#40;</span>a<span style="color: #000080;">%</span>2<span style="color: #000080;">==</span><span style="color: #F78811;">1</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span> <span style="color: #0000ff; font-weight: bold;">yield</span> b
<span style="color: #0000ff; font-weight: bold;">val</span> sum <span style="color: #000080;">=</span> <span style="color: #F78811;">&#40;</span><span style="color: #F78811;">0</span> /<span style="color: #000080;">:</span> addNums<span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#40;</span><span style="color: #000080;">_</span>+<span style="color: #000080;">_</span><span style="color: #F78811;">&#41;</span>
println<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;Numbers: %s, ansver: %d&quot;</span> format<span style="color: #F78811;">&#40;</span>addNums mkString<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot; + &quot;</span><span style="color: #F78811;">&#41;</span>, sum<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span></pre></td></tr></table></div>

<p>To get addNums, we use a for loop, and Scalas powerfull pattern matching. The for syntax is (item <- iterable). Instead of accessing each entry as a tuple, we use the (a,b) pattern to indicate the shape to Scala. The shape of the List ([(Int, Int)]) matches this, and each value in the tuple will be unpacked into a and b. When then filter on values where a is odd, and yield b. We will then have a list of only those numbers that had a corresponding odd number in our original table. Using mkString we can create a presentable String version off those numbers, and using the fold operator we add the numbers together.</p>
<p>It's also possible to create a smaller method for just calculating the sum from the list of parts returned by sumParts:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">def</span> calc<span style="color: #F78811;">&#40;</span>a<span style="color: #000080;">:</span> Int, b<span style="color: #000080;">:</span> Int<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#40;</span><span style="color: #F78811;">0</span> /<span style="color: #000080;">:</span> <span style="color: #F78811;">&#40;</span>sumParts<span style="color: #F78811;">&#40;</span>a,b<span style="color: #F78811;">&#41;</span> filter<span style="color: #F78811;">&#40;</span><span style="color: #000080;">_</span>.<span style="color: #000080;">_</span>1<span style="color: #000080;">%</span>2<span style="color: #000080;">==</span><span style="color: #F78811;">1</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#40;</span><span style="color: #000080;">_</span>+<span style="color: #000080;">_</span>.<span style="color: #000080;">_</span>2<span style="color: #F78811;">&#41;</span></pre></td></tr></table></div>

<p>Here I fold on the tuples, so I need to use _2 to access the 2nd value off the tuple. </p>
<p>Finally, here is the program in full, accepting 2 numbers on the command line. No error checking and verification is present in this version:</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
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">object</span> RussianMultiplication <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">def</span> sumParts<span style="color: #F78811;">&#40;</span>a<span style="color: #000080;">:</span>Int, b<span style="color: #000080;">:</span>Int<span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> List<span style="color: #F78811;">&#91;</span><span style="color: #F78811;">&#40;</span>Int,Int<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span>
        <span style="color: #0000ff; font-weight: bold;">if</span> <span style="color: #F78811;">&#40;</span>a<span style="color: #000080;">==</span><span style="color: #F78811;">0</span><span style="color: #F78811;">&#41;</span> List<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> <span style="color: #0000ff; font-weight: bold;">else</span> <span style="color: #F78811;">&#40;</span>a,b<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">::</span> sumParts<span style="color: #F78811;">&#40;</span>a/<span style="color: #F78811;">2</span>, b<span style="color: #000080;">*</span><span style="color: #F78811;">2</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>
        <span style="color: #0000ff; font-weight: bold;">val</span> numbers <span style="color: #000080;">=</span> sumParts<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> toInt,args<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">1</span><span style="color: #F78811;">&#41;</span> toInt<span style="color: #F78811;">&#41;</span>
        <span style="color: #0000ff; font-weight: bold;">for</span> <span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#40;</span>a,b<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">&lt;</span>- numbers<span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
            println<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;% 8d % 8d %5s&quot;</span> format<span style="color: #F78811;">&#40;</span>a, b, <span style="color: #0000ff; font-weight: bold;">if</span> <span style="color: #F78811;">&#40;</span>a<span style="color: #000080;">%</span>2<span style="color: #000080;">==</span><span style="color: #F78811;">1</span><span style="color: #F78811;">&#41;</span> <span style="color: #6666FF;">&quot;*&quot;</span> <span style="color: #0000ff; font-weight: bold;">else</span> <span style="color: #6666FF;">&quot;&quot;</span><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;">val</span> addNums <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">for</span> <span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#40;</span>a,b<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">&lt;</span>- numbers<span style="color: #000080;">;</span> <span style="color: #0000ff; font-weight: bold;">if</span> <span style="color: #F78811;">&#40;</span>a<span style="color: #000080;">%</span>2<span style="color: #000080;">==</span><span style="color: #F78811;">1</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span> <span style="color: #0000ff; font-weight: bold;">yield</span> b
        <span style="color: #0000ff; font-weight: bold;">val</span> sum <span style="color: #000080;">=</span> <span style="color: #F78811;">&#40;</span><span style="color: #F78811;">0</span> /<span style="color: #000080;">:</span> addNums<span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#40;</span><span style="color: #000080;">_</span>+<span style="color: #000080;">_</span><span style="color: #F78811;">&#41;</span>
        println<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;Numbers: %s, ansver: %d&quot;</span> format<span style="color: #F78811;">&#40;</span>addNums mkString<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot; + &quot;</span><span style="color: #F78811;">&#41;</span>, sum<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
    <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.staale.org/2009/07/another-pointless-challenge-solved-in-scala.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

