<?xml version="1.0"?><rss version="2.0">
<channel>
  <title>Alan&#039;s Ramblings - Scala category</title>
  <link>http://bleaklow.com:80/categories/tech/scala/</link>
  <description>My opinions may be incorrect, but they are my own</description>
  <language>en</language>
  <copyright>Alan Burlison</copyright>
  <lastBuildDate>Wed, 29 Feb 2012 20:50:00 GMT</lastBuildDate>
  <generator>Pebble (http://pebble.sourceforge.net)</generator>
  <docs>http://backend.userland.com/rss</docs>
  <image>
    <url>http://bleaklow.com/images/misc/logo.gif</url>
    <title>Alan&#039;s Ramblings (Scala category)</title>
    <link>http://bleaklow.com:80/</link>
  </image>
  <item>
    <title>Beautiful Scala</title>
    <link>http://bleaklow.com:80/2011/08/23/beautiful_scala.html</link>
    <description>
          &lt;p&gt;
This is, of course, beautiful:
&lt;/p&gt;
&lt;pre&gt;
def msort[T](less: (T, T) =&amp;gt; Boolean)
      (xs: List[T]): List[T] = {
  
    def merge(xs: List[T], ys: List[T]): List[T] =
      (xs, ys) match {
        case (Nil, _) =&amp;gt; ys
        case (_, Nil) =&amp;gt; xs
        case (x :: xs1, y :: ys1) =&amp;gt;
          if (less(x, y)) x :: merge(xs1, ys)
          else y :: merge(xs, ys1)
      }
  
    val n = xs.length / 2
    if (n == 0) xs
    else {
      val (ys, zs) = xs splitAt n
      merge(msort(less)(ys), msort(less)(zs))
    }
  }

scala&amp;gt; val intSort = msort((x: Int, y: Int) =&amp;gt; x &amp;lt; y) _
intSort: (List[Int]) =&amp;gt; List[Int] = &amp;lt;function1&amp;gt;
scala&amp;gt; intSort(List(3, 5, 1, 6, 2))
res3: List[Int] = List(1, 2, 3, 5, 6)
&lt;/pre&gt;
&lt;p&gt;
This is an example of a recursive &lt;a href=&#034;http://en.wikipedia.org/wiki/Merge_sort&#034;&gt;merge sort&lt;/a&gt; implemented using a technique known as &lt;a href=&#034;http://en.wikipedia.org/wiki/Currying&#034;&gt;currying&lt;/a&gt;, expressed in the language &lt;a href=&#034;http://www.scala-lang.org/&#034;&gt;Scala&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
Some context:  I was looking for a Java-based scripting language to act as &#039;glue&#039; for my &lt;a href=&#034;http://oziapi-java.sourceforge.net/&#034;&gt;Java interface&lt;/a&gt; to &lt;a href=&#034;http://www.oziexplorer.com/&#034;&gt;OziExplorer&lt;/a&gt; which I&#039;m using for a fenceline survey I&#039;m doing for the &lt;a href=&#034;http://www.moorsforthefuture.org.uk/&#034;&gt;Moors For The Future&lt;/a&gt; project.  I initially looked at &lt;a href=&#034;http://groovy.codehaus.org/&#034;&gt;Groovy&lt;/a&gt; and came across an intriguing comment by James Strachan, the creator of Groovy:
&lt;/p&gt;
&lt;blockquote&gt;
I can honestly say if someone had shown me the Programming in Scala book by by Martin Odersky, Lex Spoon &amp; Bill Venners back in 2003 I&#039;d probably have never created Groovy.
&lt;/blockquote&gt;
&lt;p&gt;
That set me off on an exploration of the &lt;a href=&#034;http://www.scala-lang.org/&#034;&gt;Scala website&lt;/a&gt; which has lots of good tutorials and links, including one to an &lt;a href=&#034;http://www.simplyscala.com/&#034;&gt;online interpreter&lt;/a&gt; you can type examples into.  Scala is a really intriguing language - it sits on top of the JVM so you get &#039;for free&#039; Java&#039;s cross-platform support and access to the huge Java ecosystem, but Scala also blends together the features of some of the most influential languages that preceded it:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The &#039;everything is an object&#039; nature of Smalltalk or Ruby&lt;/li&gt;
&lt;li&gt;The imperative nature of languages that have C or Java heritage&lt;/li&gt;
&lt;li&gt;Functional programming as per ML or Haskell&lt;/li&gt;
&lt;li&gt;Strongly typed but with sophisticated type inferencing that makes it look &amp; feel similar to Python or Groovy&lt;/li&gt;
&lt;li&gt;Uniform access to methods and fields, like Eiffel
&lt;li&gt;Actor-based concurrency similar to Erlang&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
Scala also adds new features of its own as well, such as abstract types, traits and extractors.  It&#039;s also very easy to write domain-specific languages in Scala.  James Strachan wrote a good &lt;a href=&#034;http://macstrac.blogspot.com/2009/04/scala-as-long-term-replacement-for.html&#034;&gt;summary&lt;/a&gt; of Scala, from which the above quote is taken.
&lt;/p&gt;
&lt;p&gt;
I also ordered a copy of &lt;a href=&#034;http://www.artima.com/shop/programming_in_scala_2ed&#034;&gt;Programming in Scala, Second Edition&lt;/a&gt; which I&#039;m enjoying reading and can thoroughly recommend.  Unlike many programming books it&#039;s well written with a light but not overly-chatty style.  It explicitly assumes you have programming experience, so it picks up pace pretty quickly.  As I&#039;ve been going through the book, most of my &#034;Why did they do that?&#034; questions have had good answers, which gives the impression that Scala has been very carefully thought through - always a good sign.
&lt;/p&gt;
&lt;p&gt;
Now all I have to do is to think of a substantial project I can use Scala on :-)
&lt;/p&gt;</description>
      <category>Tech</category>
    <category>Scala</category>
    <comments>http://bleaklow.com:80/2011/08/23/beautiful_scala.html#comments</comments>
    <guid isPermaLink="true">http://bleaklow.com:80/2011/08/23/beautiful_scala.html</guid>
    <pubDate>Tue, 23 Aug 2011 08:24:23 GMT</pubDate>
  </item>
  </channel>
</rss>

