<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" gd:etag="W/&quot;DkQESXk9eCp7ImA9WxBVGEg.&quot;"><id>tag:blogger.com,1999:blog-2399258289155141422</id><updated>2010-02-22T12:58:28.760-03:00</updated><title>Jonhnny Weslley's blog</title><subtitle type="html" /><link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="http://blog.jonhnnyweslley.net/feeds/posts/default" /><link rel="alternate" type="text/html" href="http://blog.jonhnnyweslley.net/" /><author><name>Jonhnny Weslley</name><uri>http://www.blogger.com/profile/12114421477154945891</uri><email>jonhnnyweslley@gmail.com</email></author><generator version="7.00" uri="http://www.blogger.com">Blogger</generator><openSearch:totalResults>5</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/JonhnnyWeslley" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="jonhnnyweslley" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><link rel="license" type="text/html" href="http://creativecommons.org/licenses/by/3.0/" /><logo>http://creativecommons.org/images/public/somerights20.gif</logo><entry gd:etag="W/&quot;C0EGQHg6cCp7ImA9WxRSEUQ.&quot;"><id>tag:blogger.com,1999:blog-2399258289155141422.post-539286453463963980</id><published>2008-09-12T00:02:00.003-03:00</published><updated>2008-09-12T00:53:41.618-03:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2008-09-12T00:53:41.618-03:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="scala tool" /><title>Scala Shell</title><content type="html">&lt;p&gt;&lt;b&gt;Scala Shell&lt;/b&gt; (scalash) is a &lt;a href="http://en.wikipedia.org/wiki/Shell_%28computing%29" title="Shell (computing)"&gt;shell&lt;/a&gt; for programming in &lt;a href="http://www.scala-lang.org/" title="Scala"&gt;Scala&lt;/a&gt;. Scalash is run from the &lt;a href="http://en.wikipedia.org/wiki/Command_line_interface" title="Command line interface"&gt;command line&lt;/a&gt; and allows the programmer to experiment with code in real time. It allows you to enter Scala commands at the prompt and have the interpreter respond immediately.&lt;/p&gt;&lt;p&gt; A quick summary of the features present:  &lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;em&gt;&lt;/em&gt;colourized output &lt;em&gt;(highlighting)&lt;/em&gt;&lt;/li&gt;&lt;li&gt;auto-completion, aka &lt;a href="http://en.wikipedia.org/wiki/Tab_completion"&gt;Tab-completion&lt;/a&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;start script support - when an interactive shell is started, &lt;a href="http://code.google.com/p/scalashell/"&gt;Scala Shell&lt;/a&gt; reads and executes commands from &lt;tt&gt;~/.scalarc&lt;/tt&gt;, if that file exists.&lt;/li&gt;&lt;li&gt;persistent history &lt;tt&gt;(~/.scala_history)&lt;/tt&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://en.wikipedia.org/wiki/Command_history"&gt;command history&lt;/a&gt;&lt;br /&gt;&lt;tt&gt;&lt;/tt&gt;&lt;/li&gt;&lt;li&gt;command load&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;&lt;object width="400" height="295"&gt;&lt;param name="allowfullscreen" value="true"&gt; &lt;param name="allowscriptaccess" value="always"&gt; &lt;param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=1709559&amp;amp;server=vimeo.com&amp;amp;show_title=0&amp;amp;show_byline=0&amp;amp;show_portrait=0&amp;amp;color=ff9933&amp;amp;fullscreen=1"&gt; &lt;embed src="http://vimeo.com/moogaloop.swf?clip_id=1709559&amp;amp;server=vimeo.com&amp;amp;show_title=0&amp;amp;show_byline=0&amp;amp;show_portrait=0&amp;amp;color=ff9933&amp;amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="400" height="295"&gt;&lt;/embed&gt;&lt;/object&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;You can install the current release directly with Scala Bazaar:&lt;br /&gt;&lt;/p&gt;&lt;span style="font-family:Courier New;"&gt;&lt;pre class="prettyprint" id="bash"&gt;sbaz update&lt;br /&gt;sbaz install scalashell-scala&lt;/pre&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2399258289155141422-539286453463963980?l=blog.jonhnnyweslley.net' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.jonhnnyweslley.net/feeds/539286453463963980/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://blog.jonhnnyweslley.net/2008/09/scala-shell.html#comment-form" title="7 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2399258289155141422/posts/default/539286453463963980?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2399258289155141422/posts/default/539286453463963980?v=2" /><link rel="alternate" type="text/html" href="http://blog.jonhnnyweslley.net/2008/09/scala-shell.html" title="Scala Shell" /><author><name>Jonhnny Weslley</name><uri>http://www.blogger.com/profile/12114421477154945891</uri><email>jonhnnyweslley@gmail.com</email><gd:extendedProperty name="OpenSocialUserId" value="12067944590540191865" /></author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">7</thr:total></entry><entry gd:etag="W/&quot;A04NRHc_eyp7ImA9WxRTF08.&quot;"><id>tag:blogger.com,1999:blog-2399258289155141422.post-5052081833947447826</id><published>2008-09-06T16:30:00.005-03:00</published><updated>2008-09-06T16:39:55.943-03:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2008-09-06T16:39:55.943-03:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="scala ubiquity" /><title>Scala API lookup with Ubiquity</title><content type="html">&lt;div id="g_body"&gt;&lt;p&gt;A quickly hacked up &lt;a href="http://www.scala-lang.org/"&gt;Scala&lt;/a&gt; &lt;a href="http://www.scala-lang.org/docu/files/api/index.html"&gt;API&lt;/a&gt; lookup for &lt;a href="https://wiki.mozilla.org/Labs/Ubiquity/"&gt;Ubiquity&lt;/a&gt;, the first prototype of a natural language web service connector created by &lt;a href="http://labs.mozilla.com"&gt;Mozilla Labs&lt;/a&gt;. It basically prepends a google search with the site: parameter pointing to the Scala API. Click &lt;a href="http://jonhnnyweslley.googlepages.com/scalaapilookup"&gt;here&lt;/a&gt; to install it.&lt;br /&gt;&lt;/p&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2399258289155141422-5052081833947447826?l=blog.jonhnnyweslley.net' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.jonhnnyweslley.net/feeds/5052081833947447826/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://blog.jonhnnyweslley.net/2008/09/scala-api-lookup-with-ubiquity.html#comment-form" title="2 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2399258289155141422/posts/default/5052081833947447826?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2399258289155141422/posts/default/5052081833947447826?v=2" /><link rel="alternate" type="text/html" href="http://blog.jonhnnyweslley.net/2008/09/scala-api-lookup-with-ubiquity.html" title="Scala API lookup with Ubiquity" /><author><name>Jonhnny Weslley</name><uri>http://www.blogger.com/profile/12114421477154945891</uri><email>jonhnnyweslley@gmail.com</email><gd:extendedProperty name="OpenSocialUserId" value="12067944590540191865" /></author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">2</thr:total></entry><entry gd:etag="W/&quot;DkANSHo8eSp7ImA9WxdaGE0.&quot;"><id>tag:blogger.com,1999:blog-2399258289155141422.post-255813948019679515</id><published>2008-08-26T22:40:00.019-03:00</published><updated>2008-08-26T23:39:59.471-03:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2008-08-26T23:39:59.471-03:00</app:edited><title>Escape from Zurg</title><content type="html">&lt;div style="text-align: right;"&gt;&lt;abbr title="Samuel James Alexander Halliday"&gt;&lt;/abbr&gt; &lt;a href="http://pixar.wikia.com/wiki/Emperor_Zurg" title=""&gt;&lt;img style="width: 108px; height: 83px;" alt="" src="http://images2.wikia.nocookie.net/pixar/images//thumb/e/e6/Zurg.jpg/282px-Zurg.jpg" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;A long time ago, I got fascinated with the &lt;a href="http://web.engr.oregonstate.edu/%7Eerwig/zurg/"&gt;Zurg riddle&lt;/a&gt;. Aka &lt;a href="http://web.engr.oregonstate.edu/%7Eerwig/papers/Zurg_JFP04.pdf"&gt;Escape from Zurg&lt;/a&gt;, the Zurg riddle is a puzzle (featuring characters from the movie &lt;a href="http://en.wikipedia.org/wiki/Toy_Story"&gt;Toy Story&lt;/a&gt;) which has been used to teach students Logic Programming.  Recently, Sam Halliday from the &lt;a href="http://javablog.co.uk/"&gt;Javablog&lt;/a&gt;&lt;span style="text-decoration: underline;"&gt; &lt;/span&gt;wrote a blog post with a &lt;a href="http://javablog.co.uk/2007/10/13/escape-from-zurg/"&gt;Java implementation&lt;/a&gt;, and Paul Butcher from the &lt;a href="http://texperts.com/"&gt;Texperts&lt;/a&gt; wrote another blog post with a &lt;a href="http://www.texperts.com/2007/09/09/escape-from-zurg/"&gt;Ruby implementation&lt;/a&gt; of &lt;a href="http://web.engr.oregonstate.edu/%7Eerwig/papers/Zurg_JFP04.pdf"&gt;Escape from Zurg&lt;/a&gt;. Others implementations can to be find &lt;a href="http://lambda-the-ultimate.org/node/2434"&gt;here&lt;/a&gt;. However it wasn't long before I got the craving to write a &lt;a href="http://www.scala-lang.org/"&gt;Scala&lt;/a&gt; version. The idea is not to solve this specific problem, but to use it to show some Scala features.&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;Here's the puzzle:&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;blockquote&gt;&lt;a href="http://en.wikipedia.org/wiki/Buzz_Lightyear"&gt;Buzz&lt;/a&gt;, &lt;a href="http://en.wikipedia.org/wiki/Sheriff_Woody#Woody"&gt;Woody&lt;/a&gt;, &lt;a href="http://en.wikipedia.org/wiki/Rex_%28Toy_Story%29#Rex"&gt;Rex&lt;/a&gt;, and &lt;a href="http://en.wikipedia.org/wiki/Hamm_%28Toy_Story%29#Hamm"&gt;Hamm&lt;/a&gt; have to escape from &lt;a href="http://en.wikipedia.org/wiki/Emperor_Zurg"&gt;Zurg&lt;/a&gt;. They merely have to cross one last bridge before they are free. However, the bridge is fragile and can hold at most two of them at the same time. Moreover, to cross the bridge a flashlight is needed to avoid traps and broken parts. The problem is that our friends have only one flashlight with one battery that lasts for only &lt;span style="font-weight: bold;"&gt;60 minutes&lt;/span&gt;. The toys need different times to cross the bridge (in either direction):&lt;/blockquote&gt;&lt;/div&gt;&lt;blockquote&gt;&lt;span style="font-weight: bold;"&gt;Buzz&lt;/span&gt;:      5 minutes&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Woody&lt;/span&gt;: 10 minutes&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Rex&lt;/span&gt;:      20 minutes&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Hamm&lt;/span&gt;: 25 minutes &lt;/blockquote&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;Since there can be only two toys on the bridge at the same time, they cannot cross the bridge all at once. Since they need the flashlight to cross the bridge, whenever two have crossed the bridge, somebody has to go back and bring the flashlight to those toys on the other side that still have to cross the bridge.&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;The problem is: In which order can the four toys cross the bridge in time (that is, in 60 minutes) to be saved from Zurg?&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;span style="font-size:100%;"&gt;Let's start defining the &lt;span style="font-style: italic;"&gt;Toy&lt;/span&gt;s as a case class with &lt;span style="font-style: italic;"&gt;name&lt;/span&gt; and &lt;span style="font-style: italic;"&gt;time&lt;/span&gt; fields.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:Courier New;"&gt;&lt;pre class="prettyprint" id="java"&gt;&lt;br /&gt;case class Toy(name: String, time: Int)&lt;br /&gt;&lt;/pre&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-family:Arial;"&gt;Like as the Toys, the sides of the bridge are case classes, in truth, case objects.&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:Courier New;"&gt;&lt;pre class="prettyprint" id="java"&gt;&lt;br /&gt;abstract class Direction&lt;br /&gt;case object Left extends Direction&lt;br /&gt;case object Right extends Direction&lt;br /&gt;&lt;/pre&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:130%;"&gt;&lt;span style="font-weight: bold;"&gt;What are case classes/objects?&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;span style="font-style: italic;"&gt;Case classes&lt;/span&gt; and &lt;span style="font-style: italic;"&gt;case objects&lt;/span&gt; are defined like a normal classes or objects, except that the definition is prefixed with the modifier &lt;span style="font-style: italic;"&gt;case&lt;/span&gt;. The case modifier in front of a class or object definition has the following effects:&lt;br /&gt;&lt;/div&gt;&lt;ol&gt;&lt;li&gt;Case classes implicitly come with a constructor function, with the same name as the class.&lt;/li&gt;&lt;li&gt;Case classes and case objects implicitly come with implementations of methods &lt;span style="font-style: italic;"&gt;toString,&lt;/span&gt; &lt;span style="font-style: italic;"&gt;equals&lt;/span&gt; and &lt;span style="font-style: italic;"&gt;hashCode.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;Case classes implicitly come with nullary accessor methods which retrieve the constructor arguments.&lt;/li&gt;&lt;li&gt;Case classes allow the constructions of patterns which refer to the case class constructor (Pattern Matching).&lt;br /&gt;&lt;/li&gt;&lt;/ol&gt;&lt;span style=";font-family:Arial;font-size:100%;"  &gt;For more details about case classes/objects see &lt;a href="http://alblue.blogspot.com/2007/12/scala-introduction-to-scala-case.html"&gt;here&lt;/a&gt; or in &lt;a href="http://www.scala-lang.org/docu/index.html"&gt;Scala Documentation&lt;/a&gt;.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;span style="font-size:100%;"&gt;Next, we define &lt;span style="font-style: italic;"&gt;Move&lt;/span&gt; which we will use to represent transitions between states. Each move consists of a direction (right or left) and a group of toys. &lt;span style="font-style: italic;"&gt;Move&lt;/span&gt; provides two methods, the &lt;span style="font-style: italic;"&gt;cost&lt;/span&gt; method which returns the time taken to complete the move and the overrided &lt;span style="font-style: italic;"&gt;toString&lt;/span&gt; method which provides a better print version from the move.&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;span style="font-family:Courier New;"&gt;&lt;pre class="prettyprint" id="java"&gt;&lt;br /&gt;class Move(direction: Direction, toys: List[Toy]) {&lt;br /&gt;&lt;br /&gt;def cost = Iterable.max(toys.map{_.time})&lt;br /&gt;&lt;br /&gt;override def toString = "Move: " + direction + " " + toys.map{_.name}.mkString("[", ",", "]")&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;span style="font-size:100%;"&gt;A &lt;code style="font-family: Arial; font-style: italic;"&gt;State&lt;/code&gt; comprises two fields, &lt;span style="font-style: italic;"&gt;direction&lt;/span&gt; which represents the current flashlight position and &lt;span style="font-style: italic;"&gt;group&lt;/span&gt; which represents the toys remaining on the left-hand side of the bridge.&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:Courier New;"&gt;&lt;pre class="prettyprint" id="java"&gt;class State(direction: Direction, group: List[Toy]) {&lt;br /&gt;&lt;br /&gt;  def done = group.isEmpty&lt;br /&gt;&lt;br /&gt;  def next(f: (Move, State) =&gt; Unit) = direction match {&lt;br /&gt;    case Left =&gt; for { tuple &lt;- group.zipWithIndex&lt;br /&gt;                       toy &lt;- group drop (tuple._2 + 1)&lt;br /&gt;                       toys = List(toy, tuple._1) }&lt;br /&gt;                   f(new Move(Right, toys), new State(Right, group diff toys))&lt;br /&gt;&lt;br /&gt;    case Right =&gt; for(toy &lt;- (ToyStory.toys diff group))&lt;br /&gt;                    f(new Move(Left, List(toy)), new State(Left, toy :: group))&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;What are the Scala features in State class?&lt;br /&gt;&lt;/span&gt;&lt;ol&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;First-Class Functions&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;Pattern Matching&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;For-Comprehensions&lt;/span&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&gt; &lt;span style="font-weight: bold; color: rgb(51, 51, 51);"&gt;First-Class Functions&lt;br /&gt;&lt;/span&gt;&lt;div style="text-align: justify;"&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(51, 51, 51);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;In Scala each function is a &lt;/span&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;“first-class value”. &lt;/span&gt;&lt;span style="color: rgb(51, 51, 51);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Like any other value, it may be passed as &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;a parameter or returned as a result. Functions which take other functions as parameters &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;or return them as results are called higher-order functions. The &lt;/span&gt;&lt;span style="font-style: italic; color: rgb(0, 0, 0);"&gt;next&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; method takes a function from the type &lt;/span&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0); font-style: italic;font-family:Courier;" &gt;(Move, State) =&gt; Unit&lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);font-family:Courier;" &gt; &lt;/span&gt;&lt;span style="color: rgb(51, 51, 51);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;as para&lt;/span&gt;&lt;/span&gt;meter named &lt;span style="font-style: italic;"&gt;f&lt;/span&gt;&lt;span style="font-weight: bold;"&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family:Arial;"&gt;&lt;br /&gt;&lt;br /&gt;For more details about functions&lt;/span&gt; &lt;span style="font-family:Arial;"&gt;see &lt;/span&gt;&lt;a style="font-family: Arial;" href="http://alblue.blogspot.com/2007/10/scala-introduction-to-scala-functions.html"&gt;here&lt;/a&gt;&lt;span style="font-family:Arial;"&gt; or in &lt;/span&gt;&lt;a style="font-family: Arial;" href="http://www.scala-lang.org/docu/index.html"&gt;Scala Documentation&lt;/a&gt;&lt;br /&gt;&lt;/div&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-size:100%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(51, 51, 51);"&gt;&lt;span style="color: rgb(51, 51, 51);"&gt;&gt; Pattern Matching&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;span style="font-size:100%;"&gt;Scala has a built-in general pattern matching mechanism. Pattern matching is a generalization of C or Java’s switch statement to class hierarchies. Instead of a switch statement, there is a standard method &lt;span style="font-style: italic;"&gt;match&lt;/span&gt;, which is defined in Scala’s root class Any, and therefore is available for all objects. The match method takes as argument a number of cases. Scala's pattern matching statement is most useful for matching on algebraic types expressed via &lt;a href="http://www.scala-lang.org/intro/caseclasses.html"&gt;case classes&lt;/a&gt;. Scala also allows the definition of patterns independently of case classes, using &lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-style: italic;font-size:100%;" &gt;&lt;code&gt;unapply&lt;/code&gt;&lt;/span&gt; methods in &lt;a href="http://www.scala-lang.org/intro/extractors.html"&gt;extractor objects&lt;/a&gt;. The &lt;span style="font-style: italic;"&gt;next&lt;/span&gt; method starts with the statement: &lt;span style="font-style: italic; color: rgb(0, 0, 0);font-family:Courier;" &gt;direction match..&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);font-family:Courier;" &gt;&lt;span style="color: rgb(153, 153, 153);"&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(0, 0, 0);font-family:Courier;" &gt;, &lt;/span&gt;a pattern matching expression with two options: &lt;span style="font-style: italic; color: rgb(0, 0, 0);font-family:Courier;" &gt;Left&lt;/span&gt;&lt;span style="font-weight: bold; font-style: italic; color: rgb(153, 153, 153);font-family:Courier;" &gt; &lt;/span&gt;&lt;span style="color: rgb(153, 153, 153);font-family:Times New Roman;" &gt;&lt;span style="color: rgb(0, 0, 0);"&gt;and&lt;/span&gt;&lt;/span&gt;&lt;span style="font-weight: bold; font-style: italic; color: rgb(153, 153, 153);font-family:Courier;" &gt; &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0); font-style: italic;font-family:Courier;" &gt;Right&lt;/span&gt;&lt;span style="font-weight: bold; font-style: italic; color: rgb(153, 153, 153);font-family:Courier;" &gt;&lt;span style="color: rgb(0, 0, 0);"&gt;.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;span style="font-size:100%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=";font-family:Times New Roman;font-size:100%;"  &gt;For more details about pattern matching and extractor objects &lt;/span&gt;&lt;span style=";font-family:Times New Roman;font-size:100%;"  &gt; see &lt;a href="http://langexplr.blogspot.com/2007/05/pattern-matching-on-java-objects-with.html"&gt;here&lt;/a&gt; or in &lt;a href="http://www.scala-lang.org/docu/index.html"&gt;Scala Documentation&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(51, 51, 51);"&gt;&lt;span style="color: rgb(51, 51, 51);"&gt;&gt; For-Comprehensions&lt;/span&gt;&lt;/span&gt;&lt;span style=";font-family:Times New Roman;font-size:100%;"  &gt;&lt;br /&gt;&lt;/span&gt;Scala offers special syntax to express combinations of certain&lt;span style="font-family:monospace;"&gt; &lt;/span&gt;higher-order functions more naturally. For comprehensions are a generalization of list comprehensions found in&lt;span style="font-family:monospace;"&gt; &lt;/span&gt;languages like Haskell and Python. They are mapped to combinations involving methods &lt;span style="font-style: italic;"&gt;foreach&lt;/span&gt; and &lt;span style="font-style: italic;"&gt;filter&lt;/span&gt;. For instance, the for loop &lt;span style="font-style: italic;"&gt;for (path &lt;- problem)&lt;/span&gt; ... in &lt;span style="font-style: italic;"&gt;ToyStory&lt;/span&gt; object is mapped to &lt;span style="font-style: italic;"&gt;problem foreach (path =&gt; ...)&lt;/span&gt; defined in &lt;span style="font-style: italic;"&gt;SearchProblem&lt;/span&gt; class.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:Courier New;"&gt;&lt;pre class="prettyprint" id="java"&gt;class SearchProblem(initial: State) {&lt;br /&gt;&lt;br /&gt;def foreach(f: List[Move] =&gt; Unit) {&lt;br /&gt; def solve(path: List[Move], state: State) {&lt;br /&gt;   if (state.done) {&lt;br /&gt;     f(path)&lt;br /&gt;   } else {&lt;br /&gt;     state next { (move, state) =&gt; solve(move :: path, state) }&lt;br /&gt;   }&lt;br /&gt; }&lt;br /&gt; solve(Nil, initial)&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;object ToyStory extends Application {&lt;br /&gt;&lt;br /&gt;  val toys = Toy("Buzz", 5) :: Toy("Woody", 10) :: Toy("Rex", 20) :: Toy("Hamm", 25) :: Nil&lt;br /&gt;&lt;br /&gt;  val problem = new SearchProblem(new State(Left, toys)) &lt;br /&gt;  for (path &lt;- problem)&lt;br /&gt;    if ((0 /: path) {(cost, move) =&gt; cost + move.cost} &lt;= 60)&lt;br /&gt;      println("Solution: " + path)&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;&lt;/span&gt;&lt;br /&gt;The complete source code can be downloaded &lt;a href="http://docs.google.com/Doc?id=dfzs39d9_4kz3cwh" title="Ruby Zurg solution source code"&gt;here&lt;/a&gt;.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2399258289155141422-255813948019679515?l=blog.jonhnnyweslley.net' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.jonhnnyweslley.net/feeds/255813948019679515/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://blog.jonhnnyweslley.net/2008/08/escape-from-zurg.html#comment-form" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2399258289155141422/posts/default/255813948019679515?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2399258289155141422/posts/default/255813948019679515?v=2" /><link rel="alternate" type="text/html" href="http://blog.jonhnnyweslley.net/2008/08/escape-from-zurg.html" title="Escape from Zurg" /><author><name>Jonhnny Weslley</name><uri>http://www.blogger.com/profile/12114421477154945891</uri><email>jonhnnyweslley@gmail.com</email><gd:extendedProperty name="OpenSocialUserId" value="12067944590540191865" /></author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">1</thr:total></entry><entry gd:etag="W/&quot;AkQAR345fip7ImA9WxdaE0o.&quot;"><id>tag:blogger.com,1999:blog-2399258289155141422.post-3704949989248718184</id><published>2008-08-22T00:44:00.006-03:00</published><updated>2008-08-22T01:12:26.026-03:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2008-08-22T01:12:26.026-03:00</app:edited><title>Guide to Scala Bazaar auto completion using BASH</title><content type="html">The &lt;a href="http://www.scala-lang.org/node/93"&gt;Scala Bazaar&lt;/a&gt; system, "sbaz" in short, is a system used by &lt;a href="http://www.scala-lang.org/"&gt;Scala&lt;/a&gt; enthusiasts to share computer files with each other. In particular, it makes it easy to share libraries and applications. In this post, I'll show you how easy it to use one of the nicest facilities of the modern shell, the built in "completion" support, to become more easy to use sbaz in command line.&lt;br /&gt;&lt;br /&gt;First you must go to the following site to install the BASH programmable auto completion setup if your distro doesn't have it by default. I don't think many do so you'll need to go to the &lt;a href="http://www.caliban.org/bash/index.shtml#completion" class="externalLink"&gt;Programmable Completion Website&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;Once you've setup your system for auto completion you need to take the following:&lt;br /&gt;&lt;span style="font-family:Courier New;"&gt;&lt;pre class="prettyprint" id="bash"&gt;&lt;br /&gt;#!/bin/bash&lt;br /&gt;&lt;br /&gt;_sbaz_complete()&lt;br /&gt;{&lt;br /&gt;local cur commands&lt;br /&gt;&lt;br /&gt;COMPREPLY=()&lt;br /&gt;cur=${COMP_WORDS[COMP_CWORD]}&lt;br /&gt;commands='available compact help install installed keycreate keyforget keyknown keyremember keyremoteknown keyrevoke pack remove retract setuniverse setup share show showuniverse update packages upgrade'&lt;br /&gt;cur=`echo $cur | sed 's/\\\\//g'`&lt;br /&gt;COMPREPLY=($(compgen -W "${commands}" ${cur} | sed 's/\\\\//g') )&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;complete -F _sbaz_complete -o filenames sbaz&lt;br /&gt;&lt;/pre&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;And place it in /etc/bash_completion.d/sbaz. Once you've done that the next time you start up your BASH shell you will have sbaz auto completion! &lt;br /&gt;&lt;br /&gt;Download snippet code &lt;a href="http://snippets.dzone.com/posts/show/5968"&gt;here&lt;/a&gt;.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2399258289155141422-3704949989248718184?l=blog.jonhnnyweslley.net' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.jonhnnyweslley.net/feeds/3704949989248718184/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://blog.jonhnnyweslley.net/2008/08/guide-to-scala-bazaar-auto-completion.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2399258289155141422/posts/default/3704949989248718184?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2399258289155141422/posts/default/3704949989248718184?v=2" /><link rel="alternate" type="text/html" href="http://blog.jonhnnyweslley.net/2008/08/guide-to-scala-bazaar-auto-completion.html" title="Guide to Scala Bazaar auto completion using BASH" /><author><name>Jonhnny Weslley</name><uri>http://www.blogger.com/profile/12114421477154945891</uri><email>jonhnnyweslley@gmail.com</email><gd:extendedProperty name="OpenSocialUserId" value="12067944590540191865" /></author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></entry><entry gd:etag="W/&quot;DUIBQH48fyp7ImA9WxdTGEw.&quot;"><id>tag:blogger.com,1999:blog-2399258289155141422.post-6054485996627823526</id><published>2008-05-14T22:10:00.001-03:00</published><updated>2008-05-14T23:12:31.077-03:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2008-05-14T23:12:31.077-03:00</app:edited><title>SHadoop</title><content type="html">&lt;span style="font-size:small;"&gt;&lt;span&gt;&lt;strong&gt;What is Scala and Hadoop?&lt;/strong&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;a id="r_su" title="Scala" href="http://www.scala-lang.org/"&gt;Scala&lt;/a&gt; is a modern multi-paradigm programming language designed to express common programming patterns in a concise, and type-safe way. It smoothly integrates features of object-oriented and functional languages including mixins, algebraic datatypes with pattern matching, genericity, and more.&lt;br /&gt;&lt;br /&gt;&lt;a id="n.l4" title="Hadoop" href="http://hadoop.apache.org/core/"&gt;Hadoop&lt;/a&gt; is a free Java software platform that supports running applications to process vast amounts of data. It has been developed under the Apache &lt;a id="qrwd" title="Lucene" href="http://lucene.apache.org/"&gt;Lucene&lt;/a&gt; Project and was originally developed to support distribution for &lt;a id="qbav" title="Nutch" href="http://lucene.apache.org/nutch/"&gt;Nutch&lt;/a&gt;, which is an effort to build an open source search engine for the search and index component. Hadoop consists of an open source implementation of Google’s published computing infrastructure, specifically &lt;a id="j4lx" title="MapReduce" href="http://labs.google.com/papers/mapreduce.html"&gt;MapReduce&lt;/a&gt; and the &lt;a id="k8p5" title="Google File System" href="http://labs.google.com/papers/gfs.html"&gt;Google File System&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;span&gt;&lt;strong&gt;&lt;span style="font-size:small;"&gt;&lt;br /&gt;Scala + Hadoop!?&lt;/span&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/span&gt;&lt;span&gt;&lt;br /&gt;The &lt;a id="vs67" title="Hadoop Map-Reduce" href="http://wiki.apache.org/hadoop/HadoopMapReduce"&gt;Hadoop Map-Reduce&lt;/a&gt; framework is map based&lt;/span&gt;, whose keys and values are serializable objects which implements a simple serialization protocol. This serialization protocol is defined by the &lt;a id="mwpe" title="Writable" href="http://hadoop.apache.org/core/docs/current/api/org/apache/hadoop/io/Writable.html"&gt;Writable&lt;/a&gt; interface. In addition, Hadoop provides Writable's implementations for each basic types(Ints, Long, Float, String, ...). This implementations wrap a value of the basic type in an Writable object. Let us take this &lt;a id="s86x" title="example" href="http://hadoop.apache.org/core/docs/r0.15.3/mapred_tutorial.html#Example%3A+WordCount+v1.0"&gt;example&lt;/a&gt;, and think about possible utilization of int values in Hadoop:&lt;br /&gt;&lt;span style="font-family:Courier New;"&gt;&lt;pre class="prettyprint" id="java"&gt;private final static IntWritable one = new IntWritable(1);&lt;/pre&gt;&lt;/span&gt;Sounds like primitive wrappers before Java 5 boxing!&lt;br /&gt;&lt;br /&gt;&lt;span&gt;&lt;strong&gt;&lt;span style="font-size:small;"&gt;&lt;br /&gt;Why Scala?&lt;/span&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;Read &lt;a id="xwh2" title="this" href="http://www.infoq.com/news/2008/01/why-scala"&gt;this&lt;/a&gt; and &lt;a id="kiik" title="this" href="http://www.drmaciver.com/2008/01/why-not-scala/"&gt;this&lt;/a&gt;. Moreover, Scala offers a bag of others features:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Implicit conversion methods&lt;/li&gt;&lt;/ul&gt;Implicit methods that are often used for converting types, essentially give you statically guaranteed and provided dynamic scoping. Implicit conversions between types (You know the way a number get implicitly converted to another number type in Java, e.g. int to long, short to int, ...? Scala does that too, but it’s all programmer definable. They’re just library functions in &lt;span&gt;&lt;em&gt;&lt;a id="tort" title="scala.Predef" href="http://www.scala-lang.org/docu/files/api/scala/Predef$object.html"&gt;scala.Predef&lt;/a&gt; &lt;/em&gt;&lt;/span&gt;).&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Type inference&lt;/li&gt;&lt;/ul&gt;The Scala compiler can oftentimes infer the type of an object so there is no need to explicitly specify the type. It is, for instance, often not necessary in Scala to specify the type of a variable, since the compiler can deduce the type from the initialization expression of the variable. Also return types of methods can often be omitted since they corresponds to the type of the body, which gets inferred by the compiler.&lt;br /&gt;&lt;br /&gt;In short, Scala provides a clear, concise and stylized syntax.&lt;br /&gt;&lt;br /&gt;&lt;span&gt;&lt;strong&gt;&lt;span style="font-size:small;"&gt;&lt;br /&gt;SHadoop = Scala + Hadoop&lt;/span&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;What we would like is something like this:&lt;br /&gt;&lt;span style="font-family:Courier New;"&gt;&lt;pre class="prettyprint" id="java"&gt;val one = 1&lt;br /&gt;&lt;/pre&gt;&lt;/span&gt;Or like this:&lt;br /&gt;&lt;span style="font-family:Courier New;"&gt;&lt;pre class="prettyprint" id="java"&gt;&lt;br /&gt;def map(key: LongWritable, value: Text, output: OutputCollector[Text, IntWritable], reporter: Reporter) =&lt;br /&gt;(value split " ") foreach (output collect (_, one))&lt;br /&gt;&lt;/pre&gt;&lt;/span&gt;The interesting point is that with Scala, this is quite simple to implement. SHadoop is the proof!!! SHadoop consists in only one &lt;a id="pgf4" title="source file" href="http://code.google.com/p/jweslley/source/browse/trunk/scala/shadoop/src/main/scala/shadoop/SHadoop.scala"&gt;source file&lt;/a&gt; containing a Scala object with some implicit methods that are often used for converting primitives Java types (including String) to writable instances. Furthermore, the SHadoop object provides implicit methods that are often used for converting writable java iterators to primitives type scala iterators - scala iterators provides a lot of useful methods, like foreach, map, filter and &lt;a id="aee4" title="others" href="http://www.scala-lang.org/docu/files/api/scala/Iterator.html"&gt;others&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;span&gt;&lt;strong&gt;&lt;span style="font-size:small;"&gt;&lt;br /&gt;Usage&lt;/span&gt;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span&gt;The &lt;a id="z.il" title="Hadoop Map-Reduce Tutorial" href="http://hadoop.apache.org/core/docs/r0.15.3/mapred_tutorial.html"&gt;Hadoop Map-Reduce Tutorial&lt;/a&gt; shows a very simple &lt;/span&gt;&lt;span&gt;Map-Reduce        application that counts the number of occurences of each word in a given input set.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;&lt;span&gt;&lt;strong&gt;&lt;em&gt;Source Code - &lt;/em&gt;&lt;/strong&gt;&lt;a id="atte" title="WordCount.scala" href="http://code.google.com/p/jweslley/source/browse/trunk/scala/shadoop/src/main/scala/shadoop/WordCount.scala"&gt;&lt;strong&gt;&lt;em&gt;WordCount.scala&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre class="prettyprint" id="java"&gt;package shadoop&lt;br /&gt;&lt;br /&gt;import SHadoop._&lt;br /&gt;import java.util.Iterator&lt;br /&gt;import org.apache.hadoop.fs._&lt;br /&gt;import org.apache.hadoop.io._&lt;br /&gt;import org.apache.hadoop.mapred._&lt;br /&gt;&lt;br /&gt;object WordCount {&lt;br /&gt;&lt;br /&gt;class Map extends MapReduceBase with Mapper[LongWritable, Text, Text, IntWritable] {&lt;br /&gt;&lt;br /&gt;val one = 1&lt;br /&gt;&lt;br /&gt;def map(key: LongWritable, value: Text, output: OutputCollector[Text, IntWritable], reporter: Reporter) =&lt;br /&gt;    (value split " ") foreach (output collect (_, one))&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;class Reduce extends MapReduceBase with Reducer[Text, IntWritable, Text, IntWritable] {&lt;br /&gt;  def reduce(key: Text, values: Iterator[IntWritable],&lt;br /&gt;             output: OutputCollector[Text, IntWritable], reporter: Reporter) = {&lt;br /&gt;      val sum = values reduceLeft ((a: Int, b: Int) =&amp;gt; a + b)&lt;br /&gt;      output collect (key, sum)&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;def main(args: Array[String]) = {&lt;br /&gt;  val conf = new JobConf(classOf[Map])&lt;br /&gt;  conf setJobName "wordCount"&lt;br /&gt;&lt;br /&gt;  conf setOutputKeyClass classOf[Text]&lt;br /&gt;  conf setOutputValueClass classOf[IntWritable]&lt;br /&gt;&lt;br /&gt;  conf setMapperClass classOf[Map]&lt;br /&gt;  conf setCombinerClass classOf[Reduce]&lt;br /&gt;  conf setReducerClass classOf[Reduce]&lt;br /&gt;&lt;br /&gt;  conf setInputFormat classOf[TextInputFormat]&lt;br /&gt;  conf setOutputFormat classOf[TextOutputFormat[_ &amp;lt;: WritableComparable, _ &amp;lt;: Writable]]&lt;br /&gt;&lt;br /&gt;  conf setInputPath(args(0))&lt;br /&gt;  conf setOutputPath(args(1))&lt;br /&gt;&lt;br /&gt;  JobClient runJob conf&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span&gt;&lt;strong&gt;&lt;span style="font-size:small;"&gt;&lt;br /&gt;Source code explained: Java x Scala&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/strong&gt;&lt;/span&gt;&lt;strong&gt;1. The &lt;em&gt;one&lt;/em&gt; field from the &lt;/strong&gt;&lt;strong&gt;Map &lt;/strong&gt;&lt;strong&gt;class&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Java&lt;br /&gt;&lt;/strong&gt;&lt;pre class="prettyprint" id="java"&gt;private final static IntWritable one = new IntWritable(1);&lt;/pre&gt;&lt;strong&gt;Scala&lt;/strong&gt;&lt;br /&gt;&lt;pre class="prettyprint" id="java"&gt;val one = 1&lt;/pre&gt;Scala to specify the type of the field by type inference. Very cool!!!&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span&gt;&lt;strong&gt;2. The &lt;/strong&gt;&lt;/span&gt;&lt;span&gt;&lt;strong&gt;&lt;em&gt;map&lt;/em&gt;&lt;/strong&gt;&lt;/span&gt;&lt;span&gt;&lt;strong&gt; method &lt;/strong&gt;&lt;/span&gt;&lt;strong&gt;from the &lt;/strong&gt;&lt;strong&gt;Map &lt;/strong&gt;&lt;strong&gt;class&lt;br /&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;Java&lt;/strong&gt;&lt;br /&gt;&lt;pre class="prettyprint" id="java"&gt;public void map(LongWritable key, Text value,&lt;br /&gt;              OutputCollector&amp;lt;Text, IntWritable&amp;gt; output, Reporter reporter) throws IOException {&lt;br /&gt;    String line = value.toString();&lt;br /&gt;    StringTokenizer tokenizer = new StringTokenizer(line);&lt;br /&gt;    while (tokenizer.hasMoreTokens()) {&lt;br /&gt;        word.set(tokenizer.nextToken());&lt;br /&gt;        output.collect(word, one);&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;strong&gt;Scala&lt;/strong&gt;&lt;br /&gt;&lt;pre class="prettyprint" id="java"&gt;def map(key: LongWritable, value: Text,&lt;br /&gt;      output: OutputCollector[Text, IntWritable], reporter: Reporter) =&lt;br /&gt;    (value split " ") foreach (output collect (_, one))&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;span&gt;Wow!!! Scala implicitly converts &lt;span&gt;&lt;em&gt;value&lt;/em&gt;&lt;/span&gt;&lt;/span&gt; to &lt;span&gt;&lt;em&gt;String&lt;/em&gt;&lt;/span&gt; and applies String's &lt;span&gt;&lt;em&gt;split&lt;/em&gt;&lt;/span&gt; method that returns a String array. This array iterate over each String adding it as key(&lt;span&gt;implicitly converted to Text)&lt;/span&gt; from &lt;span&gt;&lt;em&gt;output&lt;/em&gt;&lt;/span&gt; object and whose value is &lt;span&gt;&lt;em&gt;one&lt;/em&gt;&lt;/span&gt;. Note: Scala doesn't require semicolons at the end of each instruction, they are optionals.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span&gt;&lt;strong&gt;3. The &lt;/strong&gt;&lt;/span&gt;&lt;span&gt;&lt;strong&gt;&lt;em&gt;reduce&lt;/em&gt;&lt;/strong&gt;&lt;/span&gt;&lt;span&gt;&lt;strong&gt; method from the Reduce class&lt;br /&gt;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;strong&gt;Java&lt;/strong&gt;&lt;br /&gt;&lt;pre class="prettyprint" id="java"&gt;public void reduce(Text key, Iterator&amp;lt;IntWritable&amp;gt; values,&lt;br /&gt;                 OutputCollector&amp;lt;Text, IntWritable&amp;gt; output, Reporter reporter) throws IOException {&lt;br /&gt;    int sum = 0;&lt;br /&gt;    while (values.hasNext()) {&lt;br /&gt;        sum += values.next().get();&lt;br /&gt;    }&lt;br /&gt;    output.collect(key, new IntWritable(sum));&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;strong&gt;&lt;br /&gt;Scala&lt;/strong&gt;&lt;br /&gt;&lt;pre class="prettyprint" id="java"&gt;def reduce(key: Text, values: Iterator[IntWritable],&lt;br /&gt;         output: OutputCollector[Text, IntWritable], reporter: Reporter) = {&lt;br /&gt;    val sum = values reduceLeft ((a: Int, b: Int) =&amp;gt; a + b)&lt;br /&gt;    output collect (key, sum)&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;span&gt;Again, &lt;/span&gt;&lt;span&gt;wow!!! On first line Scala calculates the sum of the values using the &lt;span&gt;&lt;em&gt;reduceLeft &lt;/em&gt;&lt;/span&gt;method from a Int iterator, &lt;/span&gt;&lt;span&gt;implicitly&lt;/span&gt;&lt;span&gt; converted from &lt;/span&gt;&lt;span&gt;&lt;em&gt;IntWritable&lt;/em&gt;&lt;/span&gt; java iterator. After, the &lt;span&gt;&lt;em&gt;output&lt;/em&gt;&lt;/span&gt; object collects the sum result.&lt;br /&gt;&lt;br /&gt;&lt;span&gt;&lt;strong&gt;&lt;span style="font-size:small;"&gt;&lt;br /&gt;Running&lt;/span&gt;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;Assuming HADOOP_HOME is the root of the installation from Hadoop:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Copy the scala-library.jar to &lt;span&gt;&lt;em&gt;${HADOOP_HOME}/lib&lt;/em&gt;&lt;/span&gt; directory&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;Run the application:&lt;/li&gt;&lt;/ul&gt;&lt;span&gt;&lt;/span&gt; &lt;span&gt;&lt;pre class="prettyprint" id="bash"&gt;$ ${HADOOP_HOME}/bin/hadoop jar shadoop-0.0.1-alpha.jar shadoop.WordCount input/ output/&lt;/pre&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;input/ - a directory containing the text-files as input set&lt;br /&gt;ouput/ - a ouput directory&lt;br /&gt;&lt;br /&gt;Download jar with sources &lt;a id="x6fi" title="here" href="http://jweslley.googlecode.com/files/shadoop-0.0.1-alpha.jar"&gt;here&lt;/a&gt;.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2399258289155141422-6054485996627823526?l=blog.jonhnnyweslley.net' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.jonhnnyweslley.net/feeds/6054485996627823526/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://blog.jonhnnyweslley.net/2008/05/shadoop.html#comment-form" title="4 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2399258289155141422/posts/default/6054485996627823526?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2399258289155141422/posts/default/6054485996627823526?v=2" /><link rel="alternate" type="text/html" href="http://blog.jonhnnyweslley.net/2008/05/shadoop.html" title="SHadoop" /><author><name>Jonhnny Weslley</name><uri>http://www.blogger.com/profile/12114421477154945891</uri><email>jonhnnyweslley@gmail.com</email><gd:extendedProperty name="OpenSocialUserId" value="12067944590540191865" /></author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">4</thr:total></entry></feed>
