<?xml version="1.0"?><rss version="2.0">
<channel>
  <title>Alan&#039;s Ramblings</title>
  <link>http://bleaklow.com:80/</link>
  <description>My opinions may be incorrect, but they are my own</description>
  <language>en</language>
  <copyright>Alan Burlison</copyright>
  <lastBuildDate>Wed, 23 Nov 2011 16:42:03 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</title>
    <link>http://bleaklow.com:80/</link>
  </image>
  <item>
    <title>Why I quite like Scala&#039;s type system</title>
    <link>http://bleaklow.com:80/2011/11/23/why_i_quite_like_scalas_type_system.html</link>
    <description>
          &lt;p&gt;
My colleague Gary pointed me at a &lt;a href=&#034;http://blog.joda.org/2011/11/scala-feels-like-ejb-2-and-other.html&#034;&gt;blog post&lt;/a&gt; by Stephen Colebourne that is critical of &lt;a href=&#034;http://www.scala-lang.org/&#034;&gt;Scala&lt;/a&gt;, along with a related &lt;a href=&#034;http://www.reddit.com/comments/mlbna&#034;&gt;Reddit&lt;/a&gt; thread, and asked me what I thought.  Some points are fair enough, for example the Scala community has an elitist Functional Programming cadre who clustered around the &lt;a href=&#034;http://code.google.com/p/scalaz/&#034;&gt;Scalaz&lt;/a&gt; library who tend to be particularly obnoxious.  I&#039;ve already experienced the ire of one of their members, and from the private emails I&#039;ve received I know I&#039;m not the only one to have been the butt-end of his displeasure.  As a generalisation they seem to have come from a &lt;a href=&#034;http://haskell.org&#034;&gt;Haskell&lt;/a&gt; background and it appears that they won&#039;t be satisfied until Scala turns into Haskell.  There&#039;s an obvious solution to that which I&#039;ll refrain from pointing out... ;-)
&lt;/p&gt;
&lt;p&gt;
The blog post linked to above recycles common criticisms of two Scala features without actually considering &lt;i&gt;why&lt;/i&gt; they might have been provided in the first place.  The features in question are &lt;a href=&#034;http://adriaanm.github.com/research/2010/10/06/new-in-scala-2.8-type-constructor-inference.html&#034;&gt;Higher-Kinded Types&lt;/a&gt; (AKA Constructor Polymorphism) and &lt;a href=&#034;http://www.scala-lang.org/node/114&#034;&gt;Implicit Parameters&lt;/a&gt;.  I&#039;d expect a more substantial argument than &#034;It&#039;s too complicated and hard&#034; - that just seems lazy.  Yes, it&#039;s complicated, generic programming nearly always is.  For example, &lt;a href=&#034;http://en.wikipedia.org/wiki/Generics_in_Java&#034;&gt;Java Generics&lt;/a&gt; are commonly accepted as being A Good Thing In General yet I can&#039;t see how any language feature that has a &lt;a href=&#034;http://www.angelikalanger.com/GenericsFAQ/JavaGenericsFAQ.html&#034;&gt;297-page FAQ&lt;/a&gt; is anything other than complicated.
&lt;/p&gt;
&lt;p&gt;
There are many, many blog posts, mailing list discussions and academic papers out there which discuss Scala&#039;s use of Higher-Kinded Types and Implicit Parameters.  Two of the best academic papers (both PDFs) are:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#034;http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.135.575&amp;rep=rep1&amp;type=pdf&#034;&gt;Generics of a Higher Kind&lt;/a&gt; &lt;i&gt;Moors, Piessens &amp;amp; Odersky&lt;/i&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#034;http://drops.dagstuhl.de/volltexte/2009/2338/pdf/09005.OderskyM.2338.pdf&#034;&gt;Fighting Bit Rot with Types (Experience Report: Scala Collections)&lt;/a&gt; &lt;i&gt;Odersky &amp;amp; Moors&lt;/i&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
While they are interesting and well worth a read they aren&#039;t exactly written in a way you&#039;d use to describe the benefits of Scala to your mates in the pub.  However, hidden away in the second paper is a little example that shows &lt;i&gt;why&lt;/i&gt; the combination of Higher-Kinded Types and Implicit Parameters is so powerful, and is such an advance over Java.
&lt;/p&gt;
&lt;p&gt;
Scala&#039;s Implicit Parameters are really just a house-trained version of &lt;a href=&#034;http://msdn.microsoft.com/en-us/library/5s5sk305.aspx&#034;&gt;C++ user-defined type conversion operators&lt;/a&gt;, nothing more.  They are a way of allowing you to specify how one type of thing should be converted into another.
&lt;/p&gt;
&lt;p&gt;
Higher-Kinded Types are a little more difficult to grasp.  Java has types such as List&lt;T&gt; which are &#039;kinded types&#039;, e.g. a &lt;code&gt;List&amp;lt;Integer&amp;gt;&lt;/code&gt; is a &#039;kind of&#039; list.  However in Scala you can go beyond that and define types which are the equivalent of &lt;code&gt;L&amp;lt;T&amp;gt;&lt;/code&gt; where both &lt;code&gt;L&lt;/code&gt; and &lt;code&gt;T&lt;/code&gt; are type parameters.  Why is that useful?  Well, because it allows you to write code that manipulates &lt;strong&gt;all&lt;/strong&gt; sorts of list-like things irrespective of their actual types. Like Java Generics it&#039;s really of most use to the writers of libraries rather than day-to-day use, but in combination with Implicit Parameters it does allow things you just can&#039;t do in Java.
&lt;/p&gt;
&lt;/p&gt;
OK, on to the example.  To show these two language features in action together, we&#039;ll use the Scala &lt;code&gt;BitSet&lt;/code&gt; class which implements a set using bits in integers for compact storage:
&lt;/p&gt;
&lt;pre&gt;
scala&amp;gt; val b = BitSet(2,4,8)
b: scala.collection.immutable.BitSet = BitSet(2, 4, 8)
&lt;/pre&gt;
&lt;p&gt;
Straightforward so far.  Now let&#039;s map that bitset by adding 1 to each element:
&lt;/p&gt;
&lt;pre&gt;
scala&amp;gt; b.map(v =&amp;gt; v + 1)
res0: scala.collection.immutable.BitSet = BitSet(3, 5, 9)
&lt;/pre&gt;
&lt;p&gt;
OK, that&#039;s what we would expect - we get back a new &lt;code&gt;BitSet&lt;/code&gt;.  Now let&#039;s do something that would be impossible in Java&#039;s type system.  Let&#039;s map over the bitset again, but this time the returned values will be something that can&#039;t be stored in bits.  Let&#039;s map the bitset contents to &lt;code&gt;String&lt;/code&gt;:
&lt;/p&gt;
&lt;pre&gt;
scala&amp;gt; b.map(v =&amp;gt; &#034;bit &#034; + v)
res1: scala.collection.immutable.Set[java.lang.String] = Set(bit 2, bit 4, bit 8)
&lt;/pre&gt;
&lt;p&gt;
Now let&#039;s try mapping the contents to floating point numbers:
&lt;/p&gt;
&lt;pre&gt;
scala&amp;gt; b.map(v =&amp;gt; v * 1.5)
res3: scala.collection.immutable.Set[Double] = Set(3.0, 6.0, 12.0)
&lt;/pre&gt;
&lt;p&gt;
So what we get back is not just a set of different values (and types) to the ones we started with, the &lt;i&gt;container&lt;/i&gt; those types are stored in is different as well - &lt;code&gt;BitSet&lt;/code&gt; becomes &lt;code&gt;Set[Double]&lt;/code&gt;.  That transformational magic is &lt;strong&gt;not&lt;/strong&gt; a language-level feature, it&#039;s all done in at library level by making use of both the powerful (and yes, complex) type system and implicit parameters - for a full explanation, see the papers above.  The important takeaway is that you get the appearance and behaviour you might expect of an untyped language such as Python (notice, the types of &lt;code&gt;b&lt;/code&gt; and &lt;code&gt;c&lt;/code&gt; are never declared) and yet Scala is actually strongly typed.
&lt;/p&gt;
&lt;p&gt;
Here&#039;s the coup de grace.  Let&#039;s write something that stores the return of the integer map operation in a variable, then let&#039;s assign the result of the floating point map operation to the same variable.  I&#039;m doing this in the interpreter&#039;s &#039;paste&#039; mode so it&#039;s compiled all in on go:
&lt;/p&gt;
&lt;pre&gt;
scala&amp;gt; :paste
// Entering paste mode (ctrl-D to finish)

val b = BitSet(2,4,8)
var c = b.map(v =&amp;gt; v + 1)
c = b.map(v =&gt; v * 1.5)
^D
// Exiting paste mode, now interpreting.

&amp;ltconsole&amp;gt;:10: error: type mismatch;
 found   : scala.collection.Set[Double]
 required: scala.collection.BitSet
       c = b.map(v =&amp;gt; v * 1.5)
                ^
&lt;/pre&gt;
&lt;p&gt;
Note that&#039;s a &lt;i&gt;compile time&lt;/i&gt; error, &lt;strong&gt;not&lt;/strong&gt; a run-time one, and the parts involved are all &lt;i&gt;library&lt;/i&gt; classes, &lt;strong&gt;not&lt;/strong&gt; fundamental language features. How does it work?  Well, the type of &lt;code&gt;c&lt;/code&gt; is inferred by the compiler so it knows that &lt;code&gt;c&lt;/code&gt; is a &lt;code&gt;BitSet&lt;/code&gt;.  It can also work out at compile-time that the return type of the second map operation will be &lt;code&gt;Set[Double]&lt;/code&gt;, and that&#039;s not type-compatible with &lt;code&gt;BitSet&lt;/code&gt;, so you get a compile-time error.  Yet there&#039;s not an explicit type declaration in sight.  Python, eat your heart out ;-) 
&lt;/p&gt;
&lt;p&gt;
I don&#039;t just think this is cool because I&#039;m some sort of strict-typing bigot, one of my teensy claims to fame is that my name is in the Perl AUTHORS file so feel I&#039;ve earned my dynamically-typed Scout&#039;s badge.  However, given a choice between strong and weak typing I&#039;d prefer strong typing just because it helps protect me from my own stupidity.  Obviously the example above is not the only possible use of these Scala language features, but I do think it&#039;s a good, simple illustration of how they can be used.  And no, this kind of power isn&#039;t easy to wield.  As the second paper listed above says:
&lt;/p&gt;
&lt;blockquote&gt;
Getting this design right was very hard, however. It took us about a year to go from a first sketch to the final implementation. In doing this work, we also encountered some dead ends.
&lt;/blockquote&gt;
&lt;p&gt;
However saying that Scala shouldn&#039;t provide support for powerful concepts just because they are hard to get your head around seems completely wrong-headed.  The difficulty is in the &lt;i&gt;concepts&lt;/i&gt; more than it is in the &lt;i&gt;implementation&lt;/i&gt;.  The blog post that prompted this points to the &lt;a href=&#034;http://fantom.org&#034;&gt;Fantom&lt;/a&gt; language as a superior alternative to Scala, yet the &lt;a href=&#034;http://fantom.org/doc/docIntro/WhyFantom.html&#034;&gt;Why Fantom&lt;/a&gt; page contains the following:
&lt;/p&gt;
&lt;blockquote&gt;
Currently Fantom takes a limited approach to generics. There is no support for user defined generics yet. However, three built-in classes List, Map, and Func can be parameterized using a special syntax. For example a list of Ints in Fantom is declared as Int[] using the familiar array type syntax of Java and C#. This trade-off seems to hit the sweet spot where generics make sense without complicating the overall type system.
&lt;/blockquote&gt;
&lt;p&gt;
So pretending the need for Generics doesn&#039;t exist seems to be &lt;strong&gt;their&lt;/strong&gt; solution.  Boggle.
&lt;/p&gt;
</description>
      <comments>http://bleaklow.com:80/2011/11/23/why_i_quite_like_scalas_type_system.html#comments</comments>
    <guid isPermaLink="true">http://bleaklow.com:80/2011/11/23/why_i_quite_like_scalas_type_system.html</guid>
    <pubDate>Wed, 23 Nov 2011 16:42:03 GMT</pubDate>
  </item>
  <item>
    <title>Stupid BT router...</title>
    <link>http://bleaklow.com:80/2011/11/23/stupid_bt_router.html</link>
    <description>
          &lt;p&gt;
I have the misfortune to own a BT Business 2Wire router, and bleaklow.com sits behind that.  A while back it threw a fit and reconfigured itself and when I beat it into submission I didn&#039;t notice that it had also lost the port mappings for bleaklow.com.  As a result I&#039;ve been off-air for quite some time.  Bloody BT...
&lt;/p&gt;</description>
      <category>Web</category>
    <category>Tech</category>
    <comments>http://bleaklow.com:80/2011/11/23/stupid_bt_router.html#comments</comments>
    <guid isPermaLink="true">http://bleaklow.com:80/2011/11/23/stupid_bt_router.html</guid>
    <pubDate>Wed, 23 Nov 2011 14:12:00 GMT</pubDate>
  </item>
  <item>
    <title>Hints on creating 256-colour themes for vim</title>
    <link>vim 256 colour</link>
    <description>
          &lt;p&gt;
Most existing &lt;a href=&#034;http://www.vim.org/&#034;&gt;vim&lt;/a&gt; colour themes are designed for the graphical version of vim, gvim.  They don&#039;t work properly at all if you are running vim in a terminal, and there are very few themes available for vim in terminal-mode. You are pretty much obliged to make your own if one of the small number of available ones doesn&#039;t suit.  Here&#039;s a list of hints and tips that I discovered in the process of creating one:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If your shell&#039;s setting for &lt;code&gt;$TERM&lt;/code&gt; is &lt;code&gt;xterm&lt;/code&gt; you won&#039;t get colour support.  It needs to be &lt;code&gt;xtermc&lt;/code&gt; or you need to add a &lt;code&gt;set term=xtermc&lt;/code&gt; in your &lt;code&gt;.vimrc&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Even with the terminal type set you may not get 256 colours if you are using gnome-terminal.  To fix that, put &lt;code&gt;set t_Co=256&lt;/code&gt; in your &lt;code&gt;.vimrc&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;In vim, type &lt;code&gt;:help highlight&lt;/code&gt; to get instructions on how to specify colours in vim.&lt;/li&gt;
&lt;li&gt;To make the process of authoring a colour theme easier, install the &lt;a href=&#034;http://www.vim.org/scripts/script.php?script_id=85&#034;&gt;Mkcolorscheme.vim&lt;/a&gt; script in your vim plugin directory, uncommenting the four commands at the top of the file.  Read the instructions in the plugin, they give you hints on how to identify the colours currently being used by syntax elements.&lt;/li&gt;
&lt;li&gt;Open a new subwindow in vim and type &lt;code&gt;:so $VIMRUNTIME/syntax/hitest.vim&lt;/code&gt;.  That will display a table of the current settings, which will be updated as you interactively change colours.&lt;/li&gt;
&lt;li&gt;Open this &lt;a href=&#034;http://upload.wikimedia.org/wikipedia/commons/9/95/Xterm_color_chart.png&#034;&gt;colour chart&lt;/a&gt; in your browser, it gives you the colour numbers you&#039;ll need to set the colours.&lt;/li&gt;
&lt;li&gt;Open up a source file of the type that you want to set the syntax colouring for, put the cursor over an element and use the &lt;code&gt;:GetSyntax&lt;/code&gt; command to identify the element type, followed by the appropriate&lt;code&gt;:highlight&lt;/code&gt; command to set the element colour.&lt;/li&gt;
&lt;li&gt;When you are done, use the &lt;code&gt;:Mkcolorscheme&lt;/code&gt; command to generate a set of commands to generate the colour scheme settings.  Delete any lines containing &lt;code&gt;cleared&lt;/code&gt; as they aren&#039;t valid.&lt;/li&gt;
&lt;li&gt;Save the colour scheme to a file in your vim &lt;code&gt;colors&lt;/code&gt; directory and load the scheme with a &lt;code&gt;colorscheme&lt;/code&gt; command in your &lt;code&gt;.vimrc&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;</description>
      <category>Tech</category>
    <comments>http://bleaklow.com:80/2011/09/21/hints_on_creating_256_colour_themes_for_vim.html#comments</comments>
    <guid isPermaLink="true">vim 256 colour</guid>
    <pubDate>Wed, 21 Sep 2011 20:06:00 GMT</pubDate>
  </item>
  <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>
  <item>
    <title>Childhood autism spikes in geek heartlands </title>
    <link>http://bleaklow.com:80/2011/06/24/childhood_autism_spikes_in_geek_heartlands.html</link>
    <description>
          &lt;p&gt;
From the &lt;a href=&#034;http://www.newscientist.com&#034;&gt;New Scientist&lt;/a&gt;:
&lt;/p&gt;
&lt;blockquote&gt;
Childhood autism is two to four times as common in Eindhoven, the centre of the Dutch information technology industry, as it is in two comparably sized Dutch cities with far fewer IT employees.
&lt;br/&gt;&lt;br/&gt;
The result supports the suggestion that people who work in hi-tech engineering and computing industries, which demand the kinds of systemising and analytical skills often seen in people with autism, are more likely to have autistic children too.
&lt;br/&gt;&lt;br/&gt;
&lt;i&gt;&lt;a href=&#034;http://www.newscientist.com/article/dn20589-childhood-autism-spikes-in-geek-heartlands.html&#034;&gt;New Scientist, 20 June 2011, Childhood autism spikes in geek heartlands &lt;/a&gt;&lt;/i&gt;
&lt;/blockquote&gt;
&lt;p&gt;
Who&#039;d have thought it :-)
&lt;/p&gt;</description>
      <category>Tech</category>
    <comments>http://bleaklow.com:80/2011/06/24/childhood_autism_spikes_in_geek_heartlands.html#comments</comments>
    <guid isPermaLink="true">http://bleaklow.com:80/2011/06/24/childhood_autism_spikes_in_geek_heartlands.html</guid>
    <pubDate>Fri, 24 Jun 2011 11:10:15 GMT</pubDate>
  </item>
  </channel>
</rss>

