<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0"><channel><atom:link rel="hub" href="http://tumblr.superfeedr.com/" xmlns:atom="http://www.w3.org/2005/Atom"/><description>tiny vessels of code slowly floating out to sea</description><title>a soft sea</title><generator>Tumblr (3.0; @asoftsea)</generator><link>http://asoftsea.tumblr.com/</link><item><title>Braindump: The Twitter stack</title><description>&lt;a href="http://blog.oskarsson.nu/post/40196324612/the-twitter-stack"&gt;Braindump: The Twitter stack&lt;/a&gt;: &lt;p&gt;&lt;a class="tumblr_blog" href="http://blog.oskarsson.nu/post/40196324612/the-twitter-stack" target="_blank"&gt;johanoskarsson&lt;/a&gt;:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;For various reasons, including performance and cost, Twitter has poured significant engineering effort into breaking down the site backend into smaller JVM based services. As a nice side effect we’ve been able to open source several of the libraries and other useful tools that came out of this…&lt;/p&gt;
&lt;/blockquote&gt;</description><link>http://asoftsea.tumblr.com/post/40476130043</link><guid>http://asoftsea.tumblr.com/post/40476130043</guid><pubDate>Sun, 13 Jan 2013 19:47:00 -0500</pubDate></item><item><title>Coderspiel: Testing Scala Libraries</title><description>&lt;a href="http://code.technically.us/post/20315018195/testing-scala-libraries"&gt;Coderspiel: Testing Scala Libraries&lt;/a&gt;: &lt;p&gt;&lt;a href="http://code.technically.us/post/20315018195/testing-scala-libraries" class="tumblr_blog" target="_blank"&gt;coderspiel&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In the course of developing some Scala libraries that have stuck around, I’ve generally found it helpful to users if I release artifacts compatible with the two most recent major versions of Scala. So at the moment I support everything between 2.8.0 and 2.9.1-1.&lt;/p&gt; &lt;p&gt;Binary compatibility across minor…&lt;/p&gt;&lt;/blockquote&gt;</description><link>http://asoftsea.tumblr.com/post/20347499998</link><guid>http://asoftsea.tumblr.com/post/20347499998</guid><pubDate>Mon, 02 Apr 2012 09:29:15 -0400</pubDate></item><item><title>Some ways to write function literals in Scala</title><description>&lt;p&gt;&lt;a href="http://code.technically.us/post/18399585251/some-ways-to-write-function-literals-in-scala" class="tumblr_blog" target="_blank"&gt;coderspiel&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;Just cuz iheardata asked and I can’t find a set of examples I like.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(i: Int) =&amp;gt; i + 1

{ (i) =&amp;gt; i + 1 }       : (Int =&amp;gt; Int)
( (i) =&amp;gt; i + 1 )       : (Int =&amp;gt; Int)
{ i =&amp;gt; i + 1 }         : (Int =&amp;gt; Int)
( i =&amp;gt; i + 1 )         : (Int =&amp;gt; Int)
{ _ + 1 }              : (Int =&amp;gt; Int)
( _ + 1 )              : (Int =&amp;gt; Int)

(_: Int) + 1
&lt;/code&gt;&lt;/pre&gt;&lt;/blockquote&gt;</description><link>http://asoftsea.tumblr.com/post/18889929819</link><guid>http://asoftsea.tumblr.com/post/18889929819</guid><pubDate>Wed, 07 Mar 2012 00:19:57 -0500</pubDate></item><item><title>giter8 0.3.2 - implicit.ly</title><description>&lt;a href="http://implicit.ly/giter8-032"&gt;giter8 0.3.2 - implicit.ly&lt;/a&gt;: &lt;p&gt;&lt;a href="http://code.technically.us/post/14430585446/giter8-0-3-2-implicit-ly" class="tumblr_blog" target="_blank"&gt;coderspiel&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;&lt;blockquote&gt;
  &lt;p&gt;Giter8 templates can now use ls to resolve the latest published version of a library.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;There is some serious Scala web API ecosystem Zen going on here. Check it out. Oh and it uses parser combinators &lt;a href="https://github.com/n8han/giter8/blob/master/app/src/main/scala/parser.scala" target="_blank"&gt;for the ls(…) business&lt;/a&gt;, so it’s l337.&lt;/p&gt;&lt;/blockquote&gt;</description><link>http://asoftsea.tumblr.com/post/14875622081</link><guid>http://asoftsea.tumblr.com/post/14875622081</guid><pubDate>Tue, 27 Dec 2011 15:08:13 -0500</pubDate></item><item><title>I think we can manage</title><description>&lt;p&gt;tl&amp;#8217;dr: sbt Managed dependencies using git submodules on heroku. It can be done.&lt;/p&gt;

&lt;p&gt;A &lt;a href="http://goodstuff.im/scalas-version-fragility-make-the-enterprise" target="_blank"&gt;post about source compatibility&lt;/a&gt; recently stirred up some &lt;a href="http://suereth.blogspot.com/2011/12/scala-fresh-is-alive.html" target="_blank"&gt;commotion&lt;/a&gt; in the Scala community.&lt;/p&gt;

&lt;p&gt;I too have felt some of the compatibility pain, but even though I have only been dancing the Odersky waltz for only a fraction of the time it takes to get on scala&amp;#8217;s &lt;a href="http://scala-tools.org/" target="_blank"&gt;Dancing with the Stars&lt;/a&gt;, I believe I have seen hope. And I believe that source &lt;em&gt;is&lt;/em&gt; the answer.&lt;/p&gt;

&lt;h2&gt;The &lt;code&gt;source&lt;/code&gt; of the problem&lt;/h2&gt;

&lt;p&gt;In recent times, the common tech wisdom says that after 6-7 years, you are pretty much old news. The unfortunate news I have for you is that I think this is actually the time it takes to mature into something both dependable and beautiful. Younger, possibly cuter, programming languages will eventually learn that they will need to evolve and mature too, assuming they stick around for that long.&lt;/p&gt;

&lt;p&gt;Evolution is in Scala&amp;#8217;s blood. Some languages resist evolution in order to honor dead ancestors by carrying around &lt;a href="http://docs.oracle.com/javase/7/docs/api/java/util/Date.html" target="_blank"&gt;spirits of the past&lt;/a&gt;. I don&amp;#8217;t see Scala as a language that attends that same summer camp. As such, during its youth, Scala traded dead weight for a better design. The compromise was backwards binary compatibility. This obviously will cause issues with dependencies, hence the great debacle.&lt;/p&gt;

&lt;h2&gt;Simple, the way nature intended it&lt;/h2&gt;

&lt;p&gt;It wasn&amp;#8217;t long before Sbt hit the Scala scene lookin&amp;#8217; all &lt;a href="http://www.latfh.com/" target="_blank"&gt;&lt;em&gt;hipster-like&lt;/em&gt;&lt;/a&gt; from the frames of xml-configured eye glasses and roaming &lt;a href="http://ant.apache.org/" target="_blank"&gt;ants&lt;/a&gt;. Sbt gave us a number of things, one of which was the ability to build Scala software using Scala source code. The issues with source compatibility were known at that time too. Sbt&amp;#8217;s the author couldn&amp;#8217;t have made is simpler for library authors to wrangle the the issue of publishing binaries for their libraries across any combination of Scala versions you can think of.&lt;/p&gt;

&lt;p&gt;The hat trick was to take something like&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; publish
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;and turn into something like&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;+ publish
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;To this day, it baffles me that some library authors haven&amp;#8217;t caught on to this and even complain about it. I can&amp;#8217;t see it getting any more simple that adding a single operator. Although I can see some talking issue with it &lt;em&gt;being&lt;/em&gt; an operator. ( Just kidding guys :) )&lt;/p&gt;

&lt;p&gt;But there was something even more beautiful that came out of those early versions of Sbt: source dependencies, dependencies that could be managed for you and be compiled right along side your code using the same version of Scala.&lt;/p&gt;

&lt;h3&gt;Fast forward, Let&amp;#8217;s &lt;code&gt;git&lt;/code&gt; to the point&lt;/h3&gt;

&lt;p&gt;The latest versions of Sbt also support source dependencies, but through a much more powerful tool, &lt;code&gt;git&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In addition to resolving dependencies through ivy/maven identifiers, Sbt now provides a &lt;em&gt;simple&lt;/em&gt; way for declaring managed source dependencies through git uris, another under-rated, and probably under used featured among library authors.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; lazy val mahSourceDep = uri("git://github.com/the/awesome.git")
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Here&amp;#8217;s the nice thing about git. It&amp;#8217;s all about the versions!&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;lazy val mahSourceDep = uri("git://github.com/the/awesome.git#mahbranch")

lazy val mahSourceDep2 = uri("git://github.com/the/awesome2.git#mahtag")

lazy val mahSourceDep3 = uri("git://github.com/the/awesome3.git#mahsha")
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;While I agree mvn published-versions of libraries work well for binary dependencies, I can&amp;#8217;t really see authors publishing a binary for every bug fix/commit. But you know where I can see them publishing them to often? &lt;code&gt;git&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;As such, for &lt;em&gt;agile&lt;/em&gt; development and a community-driven workflow, the more I look to a system mostly defined in terms of xml and reversed domain names that don&amp;#8217;t really mean anything anymore, the more it feels a bit, &lt;em&gt;antiquated&lt;/em&gt;? Don&amp;#8217;t quote me on that! I know you&amp;#8217;ve been doing this since before I was in short pants. I&amp;#8217;m just sayin&amp;#8217;.&lt;/p&gt;

&lt;h3&gt;Heroku me loco&lt;/h3&gt;

&lt;p&gt;That&amp;#8217;s not the only way to play the source fiddle. Once you have your highly-scalable, fault- tolerant web 4.8 online pet store ready to go, the next step is to get it hosted.&lt;/p&gt;

&lt;p&gt;My recent love affair with hosting has been with &lt;a href="http://www.heroku.com/" target="_blank"&gt;Heroku&lt;/a&gt;. Why is Heroku different than other hosting providers? Heroku gets developers. Heroku gets developer workflows. Heroku gets git.&lt;/p&gt;

&lt;p&gt;How do you deploy am app on Heroku?&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;git push heroku master
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;KA-POW!&lt;/p&gt;

&lt;p&gt;Don&amp;#8217;t worry Scala coders, Heroku speaks many dialects of &lt;code&gt;sbt.version&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So now that I&amp;#8217;ve got you all PUMPED about source dependencies and Heroku. I&amp;#8217;m going to tell you a stort fail story. And then I&amp;#8217;m going to tell you another story. About not failing. About source. And git. And not failing.&lt;/p&gt;

&lt;p&gt;I recently refitted a GAE armored app with the scantily garb of nothingness Heroku requires and tripped on a bump in the cloud.&lt;/p&gt;

&lt;p&gt;The way Sbt git source dependencies work is that it clones them into a staging area, outside of your project, before compilation. So what&amp;#8217;s the problem with that? When you push to Heroku you are actually triggering the compilation of something called a &lt;a href="http://devcenter.heroku.com/articles/slug-compiler" target="_blank"&gt;slug&lt;/a&gt;. Heroku is smart enough to know how to detect your flavor of slug (language), but there is one thing you should be aware of.&lt;/p&gt;

&lt;p&gt;There ain&amp;#8217;t no writing to disk in a cloud an their ain&amp;#8217;t no special treatment for staging a clone out side of your slugs play pen!&lt;/p&gt;

&lt;p&gt;insert crazy angry face here&lt;/p&gt;

&lt;p&gt;Well that&amp;#8217;s just great. All this show and dance and no payout. Well, that&amp;#8217;s not the end of the story my friends.&lt;/p&gt;

&lt;p&gt;If there is once thing for certain with Sbt&amp;#8217;s new awesome design, it&amp;#8217;s that you&amp;#8217;ve got options™.&lt;/p&gt;

&lt;p&gt;A &lt;a href="http://code.technically.us/" target="_blank"&gt;good friend of mine&lt;/a&gt; pointed out that you can actually use git as unintended  with Sbt for profit.&lt;/p&gt;

&lt;p&gt;Here are the secret ingredients.&lt;/p&gt;

&lt;p&gt;1) Git will, by default, allow you to create submodules in any subdirectory of a repo.&lt;/p&gt;

&lt;p&gt;2) Sbt will, by default, resolve &amp;#8220;unmanaged dependencies&amp;#8221; under your projects &amp;#8216;lib&amp;#8217; directory.&lt;/p&gt;

&lt;p&gt;3) Heroku will allow you &lt;a href="http://devcenter.heroku.com/articles/labs-git-submodules" target="_blank"&gt;enable submodules&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So if you declare a submodule for your awesome dependency&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; $ git submodule add awesome git://github.com/the/awesome.git#mahbranch lib/awesome
 $ git submodule init
 $ git submodule update
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And tell sbt to depend on it&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;  lazy val mahAwesomeDep = file("lib/awesome")
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;and enable gitsubmodules on Heroku&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;  $ heroku labs:enable git_submodules
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You may just get your wings after all&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;  git push heroku master
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Don&amp;#8217;t believe me? I&amp;#8217;m &lt;a href="https://github.com/softprops/northeast-scala-symposium/blob/master/.gitmodules" target="_blank"&gt;doing&lt;/a&gt; &lt;a href="https://github.com/softprops/northeast-scala-symposium/blob/999babba9f73bcd678953c974dd8552fa4328db9/project/build.scala#L7-19" target="_blank"&gt;it&lt;/a&gt; &lt;a href="http://nescala.org/" target="_blank"&gt;now&lt;/a&gt;.&lt;/p&gt;</description><link>http://asoftsea.tumblr.com/post/14157451282</link><guid>http://asoftsea.tumblr.com/post/14157451282</guid><pubDate>Tue, 13 Dec 2011 01:24:00 -0500</pubDate></item><item><title>411s, InputStreams and chunked content</title><description>&lt;p&gt;&lt;a href="http://blog.trondbjerkestrand.no/post/2070596585/411s-inputstreams-and-chunked-content" class="tumblr_blog" target="_blank"&gt;tbje&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Working on our API the other day I came over an incompatibility issue with our front end server and the &lt;a title="Dispatch" href="http://dispatch.databinder.net/About" target="_blank"&gt;Dispatch&lt;/a&gt; http library. Some web servers does not accept chunked data input by default (nginx for instance) and will reply with a 411 error code.&lt;/p&gt;
&lt;p&gt;If you post a file using an InputStream with Dispatch, it will send the data in chunks which is great unless an incompatible server is receiving the data.&lt;/p&gt;
&lt;script src="https://gist.github.com/725200.js?file=InputStreamUpload.scala"&gt;&lt;/script&gt;&lt;p&gt;&lt;a href="http://blog.trondbjerkestrand.no/post/2070596585/411s-inputstreams-and-chunked-content" target="_blank"&gt;Read More&lt;/a&gt;&lt;/p&gt;&lt;/blockquote&gt;</description><link>http://asoftsea.tumblr.com/post/13309277472</link><guid>http://asoftsea.tumblr.com/post/13309277472</guid><pubDate>Fri, 25 Nov 2011 14:38:08 -0500</pubDate></item><item><title>sticking ? where it doesn't belong</title><description>&lt;p&gt;One fondness I have in common with Ruby, Clojure, and even &lt;a href="http://jashkenas.github.com/coffee-script/" target="_blank"&gt;Coffeescript&lt;/a&gt; is a syntactic idiom of ending the names of functions and methods that return a false value with a &lt;code&gt;?&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The idea is to immediately convey the &lt;em&gt;intent&lt;/em&gt; of the function by means of an expectation you&amp;#8217;ve developed reading natural language. When you read something that ends in a &lt;code&gt;?&lt;/code&gt;, you intuitively assume, without thinking, that sentence will result in a yes or no answer. Agree?&lt;/p&gt;

&lt;p&gt;In Java land, the idiom is to prepend methods that return a boolean value with &lt;code&gt;is&lt;/code&gt;. An example would be &lt;code&gt;isJavaLike()&lt;/code&gt;.  The idiomatic way in the languages listed above for expressing this would be &lt;code&gt;javaLike?&lt;/code&gt;.  I prefer predicate methods that end with &lt;code&gt;?&lt;/code&gt; because they read nicer and follow consistent rules. Let&amp;#8217;s say you have a method &lt;code&gt;is()&lt;/code&gt; in Java that returns a boolean value. If you wanted to be consistent you&amp;#8217;d say &lt;code&gt;isIs()&lt;/code&gt;. Chances are, you would break consistency and just stick with &lt;code&gt;is()&lt;/code&gt; because &lt;code&gt;isIs()&lt;/code&gt; just sounds silly. In the languages mentioned above, &lt;code&gt;is?&lt;/code&gt; is idiomatically consistent and still readable.&lt;/p&gt;

&lt;h2&gt;flex time&lt;/h2&gt;

&lt;p&gt;In my spare time I like to whittle projects made of Scala. Scala is a super-flexible language that allows you to create fluent DSL&amp;#8217;s by combining first class functions with a relatively open list of characters that you are free to use for method names.&lt;/p&gt;

&lt;p&gt;The only complaint I have is that, while the method definition&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;def __^|(i: Int) = i % 2 == 0
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;is syntactically valid&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;def even?(i: Int) = i % 2 == 0
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;is not!&lt;/p&gt;

&lt;p&gt;If you paste that in a Scala REPL you are likely to get this error&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;console&amp;gt;:1: error: '=' expected but identifier found.
       def even?(i: Int) = i % 2 == 0
               ^
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Method names in Scala can &lt;strong&gt;not&lt;/strong&gt; end with &lt;code&gt;?&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;How displeasing. :/&lt;/p&gt;

&lt;p&gt;Some Scala libraries like &lt;a href="http://liftweb.net/" target="_blank"&gt;Lift&lt;/a&gt; have an awkward response to this grammar constraint. In Lift, it is idiomatic to end method names that return boolean values with &lt;a href="https://github.com/lift/lift/blob/Lift-2.2-RC6-2.8-release/framework/lift-base/lift-util/src/main/scala/net/liftweb/util/AnyVar.scala#L105" target="_blank"&gt;_?&lt;/a&gt;. The &lt;code&gt;even&lt;/code&gt; method above would be defined as &lt;code&gt;even_?&lt;/code&gt; in Lift. This feels very awkward to me because there is a disconnect in consistency with the mixing of &lt;code&gt;camelCase&lt;/code&gt; method names with &lt;code&gt;snake_casing&lt;/code&gt;. Something about &lt;code&gt;fooBarBaz_?&lt;/code&gt; just doesn&amp;#8217;t &lt;em&gt;feel&lt;/em&gt; right to me. Does it &lt;code&gt;feelRight_toYou_?&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;I should mention at this point that if you read the &lt;strong&gt;Variable Declarations and Deﬁnitions&lt;/strong&gt; section of the &lt;a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf" target="_blank"&gt;language spec&lt;/a&gt; you will see the use of &lt;code&gt;def x_= (y: T ): Unit&lt;/code&gt;, suggesting the using of &lt;code&gt;_&lt;/code&gt; as suffix for mutating members. This has always felt awkward to me as well. In these cases, I prefer returning a new copy of type &lt;code&gt;Foo&lt;/code&gt; given the method &lt;code&gt;def x(y: T): Foo&lt;/code&gt; which retains the method name, unchanged, verses appending a &lt;code&gt;_&lt;/code&gt; and mutating the instance.&lt;/p&gt;

&lt;p&gt;But I digress&amp;#8230;&lt;/p&gt;

&lt;p&gt;Well, Scala &lt;em&gt;is&lt;/em&gt; a flexible language so of course there are ways to work around this.&lt;/p&gt;

&lt;p&gt;And I&amp;#8217;m not talking about something like&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; def `even?`(n: Int) = n % 2 == 0
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In languages like Ruby, you can monkey patch a class. In Scala, you use types to implicitly decorate values.&lt;/p&gt;

&lt;p&gt;My first thought was to do something like&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;implicit def questionable[T](b: Boolean) = new { def ? = b }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This will enable you decorate any expression that returns a boolean value with a &lt;code&gt;?&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So, you could say something like&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;if(true?) ...
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;But that flavoring doesn&amp;#8217;t necessarily have the same sweetness to it with methods that take arguments.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;if(even(3)?) ...
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;?&lt;/code&gt; must come after then expression.&lt;/p&gt;

&lt;p&gt;Well then, how about&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; implicit def questionablefun[T](b: T =&amp;gt; Boolean) = new { def ? = b }

 def pred[T](f: T =&amp;gt; Boolean)(t: T) = f(t)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Let&amp;#8217;s hear if for first class functions and currying!&lt;/p&gt;

&lt;p&gt;Given the tools above, we can now assign a curried function that returns a boolean to a value and end that with a &lt;code&gt;?&lt;/code&gt; prior to applying the second argument.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;val even = pred((i: Int) =&amp;gt; i % 2 == 0)_

if(even?(2)) ...
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Ha. Take that silly syntax grammar &lt;em&gt;rules&lt;/em&gt;!&lt;/p&gt;

&lt;p&gt;Now, of course I would not recommend doing something like all that in a production environment, but it does put a smile on my face just knowing that I &lt;em&gt;can&lt;/em&gt;. For the time being I will be content with neither &lt;code&gt;is&lt;/code&gt;, &lt;code&gt;?&lt;/code&gt;, nor &lt;code&gt;_?&lt;/code&gt; and just stick unadorned &lt;em&gt;intention revealing&lt;/em&gt; method names with context&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;if(allElseFails(Seq(4, 8, 15, 16, 23, 42))) ...
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Works for me.&lt;/p&gt;</description><link>http://asoftsea.tumblr.com/post/3399138248</link><guid>http://asoftsea.tumblr.com/post/3399138248</guid><pubDate>Sun, 20 Feb 2011 02:55:00 -0500</pubDate></item><item><title>mime seating arrangments on airplanes</title><description>&lt;p&gt;Recently I&amp;#8217;ve been getting more into the idea of cloud hosting for tiny apps. &lt;a href="http://code.google.com/appengine/" target="_blank"&gt;Google App Engine&lt;/a&gt; is a perfect fitting glass shoe for applications written in java and other &lt;a href="http://clojure.org/" target="_blank"&gt;jvm&lt;/a&gt; &lt;a href="http://www.scala-lang.org/" target="_blank"&gt;languages&lt;/a&gt; of the future. The the world of scala we have &lt;a href="http://code.google.com/p/simple-build-tool/" target="_blank"&gt;sbt&lt;/a&gt;, and in the world of sbt we have &lt;a href="https://github.com/Yasushi/sbt-appengine-plugin" target="_blank"&gt;plugins&lt;/a&gt; and in a world of repeatable tasks, we even have &lt;a href="https://github.com/softprops/unfiltered-gae.g8" target="_blank"&gt;templates&lt;/a&gt;. Anyway&amp;#8230; Even with a world of such luxury we sometimes can&amp;#8217;t afford to make certain types of assumptions.&lt;/p&gt;

&lt;p&gt;Living in a western world, it easy for most to people to live complacently with applications configured to use western only character sets. In the case of of applications serving string data outside those character encodings we have a few options. One of the most common encodings for multi-byte string content is &lt;a href="http://en.wikipedia.org/wiki/UTF-8" target="_blank"&gt;utf-8&lt;/a&gt;.
To enforce it, we were given weapons. Get into the habit of always putting the following line at the top of the head section of any rendered html.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;meta http-equiv="Content-Type" content="text/html;charset=utf-8" /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Recently I learned a new trick. As much as I despise java&amp;#8217;s apparent fetish with xml configuration there is actually a handy block of code you can add to your web.xml config that should provide a suggestive hint to your servlet container to use a particular mime type for a given file extension when serving static content. It&amp;#8217;s called &lt;code&gt;mime-mapping&lt;/code&gt;. I think the original idea was to be able to provide a custom mime mapping for custom file extensions but you can also use it to override the default mime time for the most common string based file types of the interwebs. In doing so, you can have the oppurtunity encode the character encoding within the mime type as follows.&lt;/p&gt;

&lt;script src="https://gist.github.com/741574.js"&gt; &lt;/script&gt;&lt;p&gt;To reiterate.&lt;/p&gt;

&lt;p&gt;before&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;gt; curl -I &lt;a href="http://northeastscala.appspot.com" target="_blank"&gt;http://northeastscala.appspot.com&lt;/a&gt;
HTTP/1.1 200 OK
ETag: "zqO8fA"
Date: Wed, 15 Dec 2010 03:42:39 GMT
Expires: Wed, 15 Dec 2010 03:52:39 GMT
Cache-Control: public, max-age=600
Content-Type: text/html
Server: Google Frontend
Transfer-Encoding: chunked
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;after&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;gt; curl -I &lt;a href="http://northeastscala.appspot.com" target="_blank"&gt;http://northeastscala.appspot.com&lt;/a&gt;
HTTP/1.1 200 OK
ETag: "MJtj8Q"
Date: Wed, 15 Dec 2010 03:43:23 GMT
Expires: Wed, 15 Dec 2010 03:53:23 GMT
Cache-Control: public, max-age=600
Content-Type: text/html; charset=utf-8
Server: Google Frontend
Transfer-Encoding: chunked
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Don&amp;#8217;t expect utf-8 content to be served when your app first jumps off the app engine plane. I did and it was kind of &lt;a href="http://twitter.com/betehess/status/13706753996750848" target="_blank"&gt;embarassing&lt;/a&gt;. It&amp;#8217;s best to be safe and strap yourself in next to a mime you can trust.&lt;/p&gt;</description><link>http://asoftsea.tumblr.com/post/2320609009</link><guid>http://asoftsea.tumblr.com/post/2320609009</guid><pubDate>Tue, 14 Dec 2010 22:46:00 -0500</pubDate><category>gae</category></item><item><title>magic match sticks and burnt fingers</title><description>&lt;p&gt;First class &lt;a href="http://en.wikipedia.org/wiki/Pattern_matching" target="_blank"&gt;pattern matching&lt;/a&gt; support is one of the many ways Scala stole my heart. I now use pattern matching as a superior refactoring and design tool in place of most every use case where I would historically insert another stray branch into a nest of &lt;code&gt;if&lt;/code&gt;s or query methods. Scala&amp;#8217;s &lt;code&gt;match&lt;/code&gt; keyword does what most languages &lt;code&gt;switch&lt;/code&gt; or &lt;code&gt;case&lt;/code&gt; keywords &lt;strong&gt;should&lt;/strong&gt; do.  Some may argue there&amp;#8217;s too much magic behind it but once you understand that happens behind the curtains with &lt;code&gt;unapply&lt;/code&gt;, most of that unfounded distrust goes away. For me, it&amp;#8217;s just just the right amount of magic for me enjoy sticking my hand in the magician&amp;#8217;s hat without the fear of uncertainty.&lt;/p&gt;

&lt;p&gt;That doesn&amp;#8217;t ways mean what my hand draws from the magicians hat is always what I expect.&lt;/p&gt;

&lt;p&gt;Let&amp;#8217;s look an extremely contrived example of using pattern matching.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;def isRabbit(contents: String) =
  contents match {
    case "Buggs" =&amp;gt; true
    case "Roger" =&amp;gt; true
    case "Jessica" =&amp;gt; false
    case _ =&amp;gt; false 
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I&amp;#8217;ve seen this trick enough to know that when I put my hand in, I have a pretty good idea of what I&amp;#8217;ll get out depending on it&amp;#8217;s contents. When see the magician stuff Buggs bunny into his hat before the show, I know I will pull out a rabbit. When I see the magician stuff Jessica Rabbit into his hat before the show, I know I will not being pulling out a rabbit. We&amp;#8217;ve all been to that show before.&lt;/p&gt;

&lt;p&gt;Now. We are good at extracting the hardcoded string values of our applications are&amp;#8217;t we? Let&amp;#8217;s extract thems that be causin all the code smell.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;val buggs = "Buggs"
val roger = "Roger"
val jess = "Jessica"

def isRabbit(contents: String) =
  contents match {
    case buggs =&amp;gt; true
    case roger =&amp;gt; true
    case jess =&amp;gt; false
    case _ =&amp;gt; false 
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Can you guess what the contents of the hat will reveal when the magician puts &amp;#8220;Roger&amp;#8221; into the hat before the show?  You sure? Okay. Now paste the code above into your nearest scala repl.&lt;/p&gt;

&lt;p&gt;Ouch! Looks like you just got burnt. What&amp;#8217;s with all this &lt;code&gt;error: unreachable code&lt;/code&gt; business? Scala is supposed to prevent me from having &lt;code&gt;buggs&lt;/code&gt; in my program, isn&amp;#8217;t it?&lt;/p&gt;

&lt;p&gt;Unbeknownst to me until recently is a little bit of knowledge that I hope to bestow upon the likes of you.  So quiet the television and come sit close to the fire.  &lt;strong&gt;Scala&amp;#8217;s pattern matching engine will &lt;em&gt;not&lt;/em&gt; treat lowercase variable names the same as will their value in match clauses.&lt;/strong&gt; Don&amp;#8217;t assume you can just use variables in the values&amp;#8217; stead without getting burnt. But, there is hope for sorry souls.&lt;/p&gt;

&lt;p&gt;Scala has a convention for naming contants and so does English for its proper nouns: the first letter should be upper-cased.&lt;/p&gt;

&lt;p&gt;Let&amp;#8217;s revisit this ridiculous example so I can get to the point. Let&amp;#8217;s rename those proper nouns using upper case identifiers.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;val Buggs = "Buggs"
val Roger = "Roger"
val Jess = "Jessica"

def isRabbit(contents: String) =
  contents match {
    case Buggs =&amp;gt; true
    case Roger =&amp;gt; true
    case Jess =&amp;gt; false
    case _ =&amp;gt; false 
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Ah, ha! It totally works now. Why? Because scala treats pattern match case arguments that are identifiers as extractors and by convention an identifier starting with an uppercase letter is assumed to be a constant identifier. A constant can statically be compiled in bytecode because it will &lt;em&gt;not&lt;/em&gt; change.  A lowercase identifier by convention is not, and therefore is a lesser citizen and can be assumed to vary at runtime and cannot be statically compiled as a pattern match clause. So why do inline, hard coded strings work then? Duh. Strings are immutable and can not change so they can be statically compiled with the pattern match clause. With this information in hand, we should hopefully see why the semi-compiled version using lower case variable names errors out. The remaining case statements would never be reached and Scala thankfully lets us know this at compile time. One of the benefits you are afforded in a statically compiled language. At least this is my hypothesis :)&lt;/p&gt;

&lt;p&gt;Before this magic show ends, I&amp;#8217;d like to point out a lesser know feature of Scala&amp;#8217;s syntax. Enter: the backtick! In some rare cases where you can just not stand to not use lower case variable names or when you get the itch to name something using a language keyword, you can wrap the identifier in a set of backticks. This will say to Scala: &amp;#8220;Hey, I know this is wrong but give me a break. The magic business hasn&amp;#8217;t been all that great lately&amp;#8221;. Scala, being the nice language it is will reply: &amp;#8220;Okay, I&amp;#8217;ll let is one go for now&amp;#8221;.&lt;/p&gt;

&lt;p&gt;So if you absolutely wanted to use lower case identifiers in pattern match clauses, you can sneakily get away with  the following.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;def isRabbit(contents: String) =
  contents match {
    case `buggs` =&amp;gt; true
    case `roger` =&amp;gt; true
    case `jess` =&amp;gt; false
    case _ =&amp;gt; false 
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;An surprisingly you can also get away with tomfoolery like this.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;val `val` = "ue"
println(`val`)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;em&gt;the magician bows&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I&amp;#8217;d like to thank &lt;a href="http://twitter.com/boia01" target="_blank"&gt;@boia01&lt;/a&gt; and &lt;a href="http://twitter.com/jorgeortiz85" target="_blank"&gt;@jorgeortiz85&lt;/a&gt; for some helpful insight on some frustration I had with an almost unrelated &lt;a href="http://twitter.com/boia01/status/6105641764519936" target="_blank"&gt;issue&lt;/a&gt; but with a totally related answer.&lt;/p&gt;

&lt;p&gt;Enjoy the show!&lt;/p&gt;</description><link>http://asoftsea.tumblr.com/post/2102257493</link><guid>http://asoftsea.tumblr.com/post/2102257493</guid><pubDate>Sat, 04 Dec 2010 21:37:00 -0500</pubDate></item><item><title>Holy Blinking MAGENTA</title><description>&lt;h2&gt;a paraphrased back-in-my-day&lt;/h2&gt;

&lt;p&gt;Have you ever wondered how some command line programs add artistically horrid colors to textual output? Ever wonder why you might see an &lt;code&gt;ls&lt;/code&gt; listing showing directories in yellow or soft-linked files in red on some machine terminals and not others.  I &lt;a href="http://www.colourlovers.com/" target="_blank"&gt;love&lt;/a&gt; &lt;a href="http://www.colourlovers.com/" target="_blank"&gt;colors&lt;/a&gt; as much as anyone else does, so I&amp;#8217;m curious too.&lt;/p&gt;

&lt;p&gt;Well, back in the days before we had a &lt;a href="http://en.wikipedia.org/wiki/Cascading_Style_Sheets" target="_blank"&gt;style markup language&lt;/a&gt; for designers we had programmers design a style markup language for&amp;#8230; programmers.  Some OS&amp;#8217;s may vary  but basically the magic seeps from an environment variable called &lt;code&gt;LSCOLORS&lt;/code&gt; and the &lt;code&gt;ls&lt;/code&gt; command line flag &lt;code&gt;--color&lt;/code&gt; or another environment variable called &lt;code&gt;CLICOLOR&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Back in those arcane days we didn&amp;#8217;t have any of these fancy pants &lt;code&gt;style sheets&lt;/code&gt; so the best we could come up with was the following approach.&lt;/p&gt;

&lt;p&gt;First, define a set of finite codes that indicate which ANSI colors are available.&lt;/p&gt;

&lt;script src="http://gist.github.com/503889.js"&gt; &lt;/script&gt;&lt;p&gt;Then define an indexing for types of file listings based foreground+background pairings&lt;/p&gt;

&lt;script src="http://gist.github.com/503892.js"&gt; &lt;/script&gt;&lt;p&gt;The result of which being something like&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;export LSCOLORS=dxfxcxdxbxegedabagacad
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Pretty obvious and easy to read huh?&lt;/p&gt;

&lt;h3&gt;a Scala crayon box&lt;/h3&gt;

&lt;p&gt;The other day I was playing around with Scala&amp;#8217;s REPL and came across something new. By now I should hope we all know about Scala&amp;#8217;s Predef object. Where else do you think all of those implicit conversions are coming from? Ohio? Referenced inside Predef is one neat little class that&amp;#8217;s easy to look right past: &lt;code&gt;Console&lt;/code&gt;.  Console provides us with some wildly fun library methods like &lt;code&gt;println&lt;/code&gt; and &lt;code&gt;readLine&lt;/code&gt;. What fun right?&lt;/p&gt;

&lt;p&gt;So what&amp;#8217;s Console got to do with color? Everything.&lt;/p&gt;

&lt;p&gt;One of the newer features of Scala&amp;#8217;s REPL is method tab completion. I see some &lt;code&gt;irb&lt;/code&gt; users in the back yawning so I&amp;#8217;ll get to my point. One of the useful side effects of having tab completion is api discovery. Guess what I get if I crack open a Scala REPL and type&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;scala&amp;gt; Console.&amp;lt;tab&amp;gt;

BLACK          BLACK_B        BLINK          BLUE           BLUE_B         BOLD           CYAN
CYAN_B         GREEN          GREEN_B        INVISIBLE      MAGENTA        MAGENTA_B      RED
RED_B          RESET          REVERSED       UNDERLINED     WHITE          WHITE_B        YELLOW
YELLOW_B       asInstanceOf   err            flush          in             isInstanceOf   out
print          printf         println        readBoolean    readByte       readChar       readDouble
readFloat      readInt        readLine       readLong       readShort      readf          readf1
readf2         readf3         setErr         setIn          setOut         toString       withErr
withIn         withOut
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Hrm&amp;#8230; What&amp;#8217;s this?&lt;/p&gt;

&lt;p&gt;What are all these values named after ANSI colors doing in Console&amp;#8217;s pantry?&lt;/p&gt;

&lt;p&gt;What you will find is that Console has provided functions values for colorizing&amp;#8230; your console&amp;#8217;s output. What can we do with this? Colorize your CLI&amp;#8217;s interface and make it all pretty like. The convention is Console.{COLOR} for setting foreground colors and Console.{COLOR}_B for setting background colors. Pretty sweet.&lt;/p&gt;

&lt;p&gt;So in your REPL you can type&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;scala&amp;gt; Console.MAGENTA_B
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Which will turn your terminals background to a nice shade of magenta.&lt;/p&gt;

&lt;p&gt;Then add a tasteful green foreground with&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;scala&amp;gt; Console.GREEN
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You should see some thing like&lt;/p&gt;

&lt;div class="console-colored"&gt;
scala&amp;gt; &amp;#8220;colors&amp;#8221; :: &amp;#8220;are&amp;#8221; :: &amp;#8220;awesome&amp;#8221; :: Nil mkString(&amp;#8221; &amp;#8220;)
&lt;/div&gt;

&lt;p&gt;But wait there&amp;#8217;s more. There&amp;#8217;s &lt;code&gt;BLINK&lt;/code&gt;! Your remember &amp;lt;blink&amp;gt;&amp;lt;/blink&amp;gt; don&amp;#8217;t you?&lt;/p&gt;

&lt;p&gt;So does Scala&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;scala&amp;gt; Console.BLINK
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;and behold&lt;/p&gt;

&lt;div class="blink console-colored"&gt;
 scala&amp;gt; &amp;#8220;colors&amp;#8221; :: &amp;#8220;are&amp;#8221; :: &amp;#8220;awesome&amp;#8221; :: Nil mkString(&amp;#8221; &amp;#8220;)
&lt;/div&gt;

&lt;p&gt;Don&amp;#8217;t worry. You can always revert these changes to console output with &lt;code&gt;Console.RESET&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Optionally, if you are feeling like a coding ninja I&amp;#8217;d recommend &lt;code&gt;Console.INVISIBLE&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;These are the things I live for.&lt;/p&gt;</description><link>http://asoftsea.tumblr.com/post/890705270</link><guid>http://asoftsea.tumblr.com/post/890705270</guid><pubDate>Sun, 01 Aug 2010 20:24:00 -0400</pubDate><category>color</category></item><item><title>a well formed cupcake</title><description>&lt;p&gt;Here&amp;#8217;s a simple little sprinkle for anyone wondering how to serve xml with self-closing tags in Scala.&lt;/p&gt;

&lt;p&gt;The basic cooking ingredients in Scala&amp;#8217;s xml kitchen will output closing tags, even when the recipe does not call for them.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;val selfClosingCupcake = &amp;lt;cupcake /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Will always unsuspectingly return&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;cupcake&amp;gt;&amp;lt;/cupcake&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;When your cupcakes are html tags, sometimes the extra closing tag is not valid.&lt;/p&gt;

&lt;p&gt;The output&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;link rel="stylesheet" href="cupcakes.css"&amp;gt;&amp;lt;/link&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;may be tasty, but is less than valid.&lt;/p&gt;

&lt;p&gt;The solution is simple. Sprinkle your xml with &lt;code&gt;xml.Xhtml&lt;/code&gt;.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;val selfClosingCupcake = xml.Xhtml.toXhtml(&amp;lt;cupcake/&amp;gt;)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This yields pretty much what you&amp;#8217;d expect.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;cupcake/&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;sweet teething&lt;/h3&gt;

&lt;p&gt;Beware of a few caveats.&lt;/p&gt;

&lt;p&gt;Always put this call to &lt;code&gt;toXhtml&lt;/code&gt; at the end of your processing pipeline as this returns a String rather than one of the handful of native xml types Scala provides.&lt;/p&gt;

&lt;p&gt;If you were wondering, yes you can always step back into that native kitchen with &lt;code&gt;XML.loadString&lt;/code&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;xml.XML.loadString(xml.Xhtml.toXhtml(&amp;lt;cupcake /&amp;gt;))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Also of note.&lt;/p&gt;

&lt;p&gt;Be aware of the xml api changes between Scala 2.7.* and 2.8.*&lt;/p&gt;

&lt;p&gt;Scala 2.7.7.final requires you to pass in two extra args.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;def toXhtml(n: Node, stripComment: Boolean, convertAmp: Boolean): String = {
  val sb = new StringBuilder()
  toXhtml(n, TopScope, sb, stripComment, convertAmp)
  sb.toString()
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;While Scala 2.8 blessed the community with default args.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;def toXhtml(node: Node): String = sbToString(toXhtml(x = node, sb = _))
&lt;/code&gt;&lt;/pre&gt;</description><link>http://asoftsea.tumblr.com/post/830728351</link><guid>http://asoftsea.tumblr.com/post/830728351</guid><pubDate>Mon, 19 Jul 2010 01:22:39 -0400</pubDate><category>xml</category></item><item><title>please yield to passing monads</title><description>&lt;p&gt;One of the most common functional programming topics tossed about in the Scala community are &lt;a href="http://en.wikipedia.org/wiki/Monad_(functional_programming)" target="_blank"&gt;monads&lt;/a&gt;. Even if you may not know what they are, chances are, you probably use them all the time.&lt;/p&gt;

&lt;p&gt;Though there are already &lt;a href="http://james-iry.blogspot.com/2007/09/monads-are-elephants-part-1.html" target="_blank"&gt;some&lt;/a&gt; &lt;a href="http://lamp.epfl.ch/~emir/bqbase/2005/01/20/monad.html" target="_blank"&gt;decent&lt;/a&gt; &lt;a href="http://debasishg.blogspot.com/2008/03/monads-another-way-to-abstract.html" target="_blank"&gt;postings&lt;/a&gt; on monads in Scala,  I&amp;#8217;d like to take a time out and describe how they are used in the Scala you probably use every day.&lt;/p&gt;

&lt;p&gt;Raise your hand if you&amp;#8217;ve ever pattern matched on a value that returned &lt;code&gt;Some&lt;/code&gt; or &lt;code&gt;None&lt;/code&gt; in Scala. Now raise your hand if you&amp;#8217;ve ever used a &lt;code&gt;Map&lt;/code&gt; in scala. If you raised your hand at least once you already use the most common monad in scala, &lt;code&gt;Option&lt;/code&gt;. So what does it mean to be a monad?&lt;/p&gt;

&lt;h3&gt;That&amp;#8217;s Monad not Nomad&lt;/h3&gt;

&lt;p&gt;Monads enable you to encapsulate the application of a function to a typed set of arguments. In other words, function bindings. Scala provides a few simple idiomatic tools do so: &lt;code&gt;map&lt;/code&gt; and &lt;code&gt;flatMap&lt;/code&gt;. You will find these implemented on many of the built in collections classes.&lt;/p&gt;

&lt;p&gt;So why would you want to encapsulate a function&amp;#8217;s application? Functions can have side effects. Functions can result in exceptional or illegal states of an application. Sometimes these exceptional states are declarative.  With the support for &lt;a href="http://en.wikipedia.org/wiki/Higher-order_function" target="_blank"&gt;higher-order&lt;/a&gt; functions in Scala, you can describe &lt;em&gt;what&lt;/em&gt; something does without stating &lt;em&gt;how&lt;/em&gt;. Thats where monads come in.&lt;/p&gt;

&lt;p&gt;Back to our story. So. Option. What&amp;#8217;s it to you? If you take a look at its source you see Option implements &lt;code&gt;map&lt;/code&gt; and &lt;code&gt;flatMap&lt;/code&gt; as&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;def map[B](f: A =&amp;gt; B): Option[B] = 
  if (isEmpty) None else Some(f(this.get))

def flatMap[B](f: A =&amp;gt; Option[B]): Option[B] = 
  if (isEmpty) None else f(this.get)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;These are binding functions. If you are used to coming from most languages, you are probably used to writing defensive code in the form.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;if(obj != null) {
   obj.play();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This is actually just a poor man&amp;#8217;s abstraction for handling an erroneous state that is admittedly a &lt;a href="http://qconlondon.com/london-2009/presentation/Null+References:+The+Billion+Dollar+Mistake" target="_blank"&gt;a billion dollar mistake&lt;/a&gt;. &lt;code&gt;Option&lt;/code&gt; is a monad that shields you from this messy situation and provides a higher level of abstraction, you either have &lt;code&gt;Some&lt;/code&gt; value or &lt;code&gt;None&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;Are You or Are You Not Comprehending&lt;/h2&gt;

&lt;p&gt;That&amp;#8217;s cool and all but how is that actually useful? Comprehensions my friend Watson.&lt;/p&gt;

&lt;p&gt;Scala provides us with a pretty neat little control structure that might seem a bit strange in a world of inferior &lt;code&gt;loops&lt;/code&gt;. I say inferior because most modern languages have control structures that allow you to just loop over collections of values without returning anything resulting in implicit side effects. I will call these &lt;code&gt;for loops&lt;/code&gt;. In functional programming, it is idiomatic that everything returns a value.   In Scala, I will call a &lt;code&gt;for loop&lt;/code&gt;&amp;#8217;s analogy a &lt;code&gt;for comprehension&lt;/code&gt;. I say comprehension because Scala is doing more that just naively &lt;code&gt;looping&lt;/code&gt; over values.  It is &lt;a href="http://en.wikipedia.org/wiki/List_comprehension" target="_blank"&gt;comprehending&lt;/a&gt; their arguments and applying functions by monadic means. This has much in common with Haskell&amp;#8217;s &lt;a href="http://en.wikipedia.org/wiki/Monads_in_functional_programming#do-notation" target="_blank"&gt;do notation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;A refresher on &lt;code&gt;for comprehensions&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In Scala, there are two usages of the &lt;code&gt;for&lt;/code&gt; control structure.&lt;/p&gt;

&lt;p&gt;First up is a &lt;code&gt;for&lt;/code&gt; that returns &lt;code&gt;Unit&lt;/code&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;for(i &amp;lt;- List(1, 2, 3)) { println(i) }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This, in its most basic form, looks outwardly like a &lt;code&gt;loop&lt;/code&gt; control structure in other languages. In someways it is and in some ways it is not.&lt;/p&gt;

&lt;p&gt;The main difference here is that the contents of for&amp;#8217;s first argument can actually be stackable and contain logic.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;for(i &amp;lt;- List(1, 2, 3); if(i &amp;gt; 1);
    j &amp;lt;- List(4, 5, 6)) { println(i * j) }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The second form is a &lt;code&gt;for&lt;/code&gt; that returns a &lt;code&gt;generated&lt;/code&gt; value.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;for(i &amp;lt;- List(1, 2, 3)) yield { i * 2 }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This yields the value of &lt;code&gt;List(2, 4, 6)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The generative form is the preferred form as you are returning the value of computation rather than performing a side effect like printing to standard output. This form also lends itself toward better testability and &lt;a href="http://en.wikipedia.org/wiki/Referential_transparency_(computer_science)" target="_blank"&gt;referential transparency&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;This Ain&amp;#8217;t your Mom&amp;#8217;s Dad&lt;/h3&gt;

&lt;p&gt;So what&amp;#8217;s do comprehensions have to do with &lt;code&gt;Option&lt;/code&gt;?&lt;/p&gt;

&lt;p&gt;There is some sleight of hand going on behind the scenes in a &lt;code&gt;for comprehension&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Firstly, when you blur your eyes a bit you can see that &lt;code&gt;for&lt;/code&gt; has the same appearance as a curried function.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;foo( ... )( ... )
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In Scala, parens are interchangeable with curly braces which can make a curried function look or &lt;code&gt;feel&lt;/code&gt; more like a control structure.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;for( ... ) { ... }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And if you really wanted&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;for { ... } ( ... )
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;But enough with emoticons.&lt;/p&gt;

&lt;p&gt;You can actually apply any value as the first argument of a &lt;code&gt;for comprehension&lt;/code&gt; given the following constraints.&lt;/p&gt;

&lt;p&gt;For the &lt;code&gt;Unit&lt;/code&gt; flavor of for comprehensions, your object must implement&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;def foreach[U](f: A =&amp;gt; U): Unit
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;For the generative flavor, you must implement&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;def map[B](f: A =&amp;gt; B): M[B]
def flatMap[B](f: A =&amp;gt; M[B]): M[B] 
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Where M is the type of Monad&lt;/p&gt;

&lt;p&gt;Remember the currying I mentioned above? You can think the second half of the curry as the function &lt;code&gt;f&lt;/code&gt; in map and foreach. The function that gets passed to flapMap is the result of the map operation at the inner most &lt;code&gt;&amp;lt;-&lt;/code&gt;. This is the magic sauce that makes Scala&amp;#8217;s for comprehensions chainable.&lt;/p&gt;

&lt;p&gt;In Scala 2.8, you will also probably want to implement&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;def withFilter(f: A =&amp;gt; Boolean)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;WithFilter&lt;/code&gt; is used to support conditional logic within for loops.&lt;/p&gt;

&lt;p&gt;Again, what&amp;#8217;s all of this have to do with Options? Keep your bonnet on!&lt;/p&gt;

&lt;p&gt;Guess what, &lt;code&gt;Option&lt;/code&gt; just happens to implement all of these.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;for(i &amp;lt;- Option(1)) { println(i) }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Prints 1&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;for(i &amp;lt;- Option(1); if(i &amp;gt; 1);
    j &amp;lt;- Option(4)) { println(i * j) }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Prints nothing (because the value of &lt;code&gt;i&lt;/code&gt; is not greater than 1.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;for(i &amp;lt;- Option(1)) yield { i * 2 }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;yields Some(2)&lt;/p&gt;

&lt;p&gt;And&amp;#8230; Since these monadic for comprehending Options return Options you can append a default value at the end of a computation&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(for(i &amp;lt;- Option(1); if(i&amp;gt;1)) yield { i * 2 }).orElse(Some(4))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;yields Some(4)&lt;/p&gt;

&lt;p&gt;Mmmm. Ok.&lt;/p&gt;

&lt;h3&gt;For Tickling Monads&lt;/h3&gt;

&lt;p&gt;Let&amp;#8217;s take a closer look by implementing a for comprehending monad called Focomo that prints some output to show what in the tarnation for comprensions are doing with values. Sorry kids, for simplicity sake, Focomo will not do anything interesting based on what the the actual value is. We just want to see where &lt;code&gt;for comprehensions&lt;/code&gt; like to tickle their arguments.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;/** A for comprehension ready Monad */
case class Focomo[A](value: A) {
  self =&amp;gt;
  /** satisfies monadic binding of a function f that returns B */
  def map[B](f: A =&amp;gt; B): Focomo[B] = { 
    println("map!"); 
    Focomo(f(value)) 
  }
  /** satisfies monadic binding of a function f that returns Focomo[B] */
  def flatMap[B](f: A =&amp;gt; Focomo[B]): Focomo[B] = { 
    println("flatMap!"); 
    f(value) 
  }
  /** expect this to be called in a `Unit` for comprehension */
  def foreach[U](f: A =&amp;gt; U): Unit = { 
    println("foreach!"); 
    f(value) 
  }
  /** for comprehension's `if` statements trigger this. 
   *  In a more useful monad, the result of the application 
   *  of a value to function f would determine the a special subclass
   *  of the monad or other either-or behavior.
   */
  def filter(f: A =&amp;gt; Boolean): Focomo[A] = { 
    println("filter!"); 
    this
  }
  /** provides a delegate handler for calls to #withFilter */
  class WithFilter(p: A =&amp;gt; Boolean) {
    println("with filter!")
    def map[B](f: A =&amp;gt; B): Focomo[B] = self.filter(p).map(f)
    def flatMap[B](f: A =&amp;gt; Focomo[B]): Focomo[B] = self.filter(p).flatMap(f)
    def foreach[U](f: A =&amp;gt; U): Unit = self.filter(p).foreach(f)
    def withFilter(q: A =&amp;gt; Boolean): WithFilter =
      new WithFilter(x =&amp;gt; p(x) &amp;amp;&amp;amp; q(x))
  }
  /** called with conditional statement in for comprehension */
  def withFilter(p: A =&amp;gt; Boolean): WithFilter = new WithFilter(p)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Let&amp;#8217;s play.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;for { i &amp;lt;- Focomo(1) } { i }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Prints&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;foreach!


for { i &amp;lt;- Focomo(1); if(i &amp;lt; 2) } { i }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Prints&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;with filter!
filter!
foreach!

for { i &amp;lt;- Focomo(2) } yield { i  }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Prints&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;map!
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And returns&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Focomo(2)

for { i &amp;lt;- Focomo(2); j &amp;lt;- Focomo(3) } yield { i * j }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Prints&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;flatMap!
map!
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And returns&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Focomo(6)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And lastly,&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;for { i &amp;lt;- Focomo(2); if(i&amp;lt;4); j &amp;lt;- Focomo(3) } yield { i * j }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Prints&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;with filter!
filter!
flatMap!
map!
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And Returns&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Focomo(6)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;So what did we get out of all of this? Well, not much from our little Focomo :).  What I hope you &lt;em&gt;did&lt;/em&gt; learn was the basic building blocks of how you can take advantage of the monadic hooks Scala provides in for comprehensions and how monads themselves are useful for encapsulating function bindings and managing common side effects.&lt;/p&gt;</description><link>http://asoftsea.tumblr.com/post/796816599</link><guid>http://asoftsea.tumblr.com/post/796816599</guid><pubDate>Sun, 11 Jul 2010 02:41:00 -0400</pubDate><category>fp</category></item><item><title>referential transparencies</title><description>&lt;p&gt;Oftentimes when shifting back and forth between languages it&amp;#8217;s easy to fudge syntactic  nuances out of habit. Sometimes when you fudge you learn something new. Today I fudged some Java in my Scala.&lt;/p&gt;

&lt;p&gt;What would you guess the return type of the following method to be?&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;def guess(what: String) {
   "%s? really?" format what
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;One who is used to Scala by now knows there&amp;#8217;s no need to explicitly declare a return type, let alone use the return keyword at the end of a method thanks to this fancy pants thing called &lt;a href="http://en.wikipedia.org/wiki/Type_inference" target="_blank"&gt;type inference&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;So what&amp;#8217;s the fuss with this simple example? It obviously returns a &lt;code&gt;String&lt;/code&gt;, duh. &lt;strong&gt;WRONG&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Scala is part &lt;a href="http://en.wikipedia.org/wiki/Functional_programming" target="_blank"&gt;functional&lt;/a&gt; and part of that functional nature includes &lt;a href="http://en.wikipedia.org/wiki/Referential_transparency_(computer_science)" target="_blank"&gt;referential transparency&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;Functional Projectors&lt;/h3&gt;

&lt;p&gt;Referential what? Databases? Overhead projectors?&lt;/p&gt;

&lt;p&gt;So what does referential transparency mean? It&amp;#8217;s simple.&lt;/p&gt;

&lt;p&gt;Say you have a value &lt;code&gt;v&lt;/code&gt; and are assigning it the result of function &lt;code&gt;f&lt;/code&gt; which takes the argument &lt;code&gt;a&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;val v = f(a)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Function &lt;code&gt;f&lt;/code&gt; is said to be &lt;strong&gt;referentially transparent&lt;/strong&gt;, if you can replace all occurrences &lt;code&gt;f&lt;/code&gt; with the the result of applying the same value &lt;code&gt;a&lt;/code&gt; to &lt;code&gt;f&lt;/code&gt; and &lt;strong&gt;always&lt;/strong&gt; end up with the same result.&lt;/p&gt;

&lt;p&gt;Huh?&lt;/p&gt;

&lt;p&gt;In functional programming, the idea is that functions should be &lt;a href="http://en.wikipedia.org/wiki/Side_effect_(computer_science)" target="_blank"&gt;side effect&lt;/a&gt; free, so it&amp;#8217;s okay to replace &lt;code&gt;f(a)&lt;/code&gt; with just the resulting value of &lt;code&gt;f(a)&lt;/code&gt; because your program will not modify state that would adversely affect the result of &lt;code&gt;f(a)&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;Mixed Flavors&lt;/h3&gt;

&lt;p&gt;So what&amp;#8217;s this have to do with fudge and Java?&lt;/p&gt;

&lt;p&gt;As Scala has inherited some of Java&amp;#8217;s syntax, Javaisms can creep into your Scalaisms.&lt;/p&gt;

&lt;p&gt;Case in point:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;def guess(what: String) {
   "%s? really?" format what
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The Scala return type of this method is actually &lt;code&gt;Unit&lt;/code&gt;.  The correct way to declare this method in Scala is:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;def guess(what: String) = {
   "%s? really?" format what
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;or even better (more minimal)&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;def guess(what: String) = "%s? really?" format what
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;So what changed? You assign the &lt;strong&gt;name&lt;/strong&gt; of the function to the it&amp;#8217;s functional &lt;strong&gt;body&lt;/strong&gt;. If you&amp;#8217;ve been paying attention, you&amp;#8217;ll pick up on the fact that this is the functional style.  If you are being referentially transparent the about your design you can say&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;val stringGuessResult = "String? really?"
val guessResult = guess("String")
stringGuessResult == guessResult
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;where the resulting value is &lt;strong&gt;always&lt;/strong&gt; the same.&lt;/p&gt;

&lt;p&gt;In the same manner declaration of function names follow suit.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;fnNameWithValue = value
fnNameWithFunctionBody = functionBody
fnNameWithValue == fnNameWithFunctionBody
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Without the assignment operator, the &lt;code&gt;{ ... }&lt;/code&gt; brackets from the previous example merely serve as a means of scoping local variables. The environment surrounding the brackets can not know what kind of scheme the local variables are plotting.&lt;/p&gt;

&lt;h3&gt;Border Crossing&lt;/h3&gt;

&lt;p&gt;This key-value concept of assigning function names to function bodies is also common in other languages.  Let&amp;#8217;s take the same example in Javascript.&lt;/p&gt;

&lt;p&gt;As Javascript also inherited some of its syntax from C, you can declare methods in a C-like fashion&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;function guess(what) {
  what + "? really?"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You can also write this in the functional style&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var guess = function(what) {
  what + "? really?"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Does this look familiar?&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var someModule = (function(){
   return {
     guess: function(what) {
        what + "? really?"
     },
     tell: function(story) {
        "my story is "+ story
     }
   };
})();
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Or even&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$.ajax({ 
   url: "...", 
   context: document.body, 
   success: function() {
     alert("All done!");
   }
});
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Both are use cases for using the functional style of assigning a function body to a name.&lt;/p&gt;

&lt;h3&gt;Back to Our Story&lt;/h3&gt;

&lt;p&gt;Just for fun let&amp;#8217;s say you actually did assign and explicit return type for the previous example with out assigning the the method body.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;def guess(what: String): String {
   "%s? really?" format what
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You actually end up with a compile error.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;illegal start of declaration (possible cause: missing `=' in front of current method body)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Ops! You&amp;#8217;re Java&amp;#8217;s showing.&lt;/p&gt;

&lt;h3&gt;Overlapping Transparencies&lt;/h3&gt;

&lt;p&gt;When you think about it, referentially transparency seems the like same idea behind &lt;code&gt;val&lt;/code&gt;s. You are assigning some resulting value to a name which cannot change unless explicitly &lt;code&gt;overridden&lt;/code&gt; in a sub type. For a program to be side effect free, one might say that when part of a program accesses a &lt;code&gt;val&lt;/code&gt;, the &lt;code&gt;val&lt;/code&gt; should always return the same referentially transparent result.&lt;/p&gt;</description><link>http://asoftsea.tumblr.com/post/601213601</link><guid>http://asoftsea.tumblr.com/post/601213601</guid><pubDate>Sat, 15 May 2010 12:36:00 -0400</pubDate><category>fp</category></item><item><title>a transitional suitcase for source</title><description>&lt;p&gt;With the recent &lt;a href="http://www.scala-lang.org/node/5982" target="_blank"&gt;announcement&lt;/a&gt; of &lt;a href="http://www.scala-lang.org/downloads" target="_blank"&gt;Scala 2.8.0 RC1&lt;/a&gt;, I finally decided it was time to start transitioning some of my projects.  For more motivation, as 2.8 becomes main stream I&amp;#8217;m sure some &lt;a href="http://github.com/softprops/ryu/issues#issue/1" target="_blank"&gt;issues&lt;/a&gt; are bound to pop up.&lt;/p&gt;

&lt;h3&gt;does a cat&amp;#8217;s pur make a sound if no one is around to hear it&lt;/h3&gt;

&lt;p&gt;I picked a smaller &lt;a href="http://github.com/softprops/meow" target="_blank"&gt;project&lt;/a&gt; to start out with, because low hanging fruit usually tastes better when transitioning to the next major release of a language.&lt;/p&gt;

&lt;p&gt;Sbt&amp;#8217;s &lt;a href="http://code.google.com/p/simple-build-tool/wiki/CrossBuild" target="_blank"&gt;cross building&lt;/a&gt; feature makes it stupid simple to add another supported Scala version to your project by cracking open your &lt;code&gt;project/build.properties&lt;/code&gt; file and appending a new one.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;build.scala.versions=2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.8.0.Beta1
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If that wasn&amp;#8217;t easy enough, try compiling your project across all noted versions by executing the compile action with a &lt;code&gt;+&lt;/code&gt;.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;+compile
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;+&lt;/code&gt; applies action against all versions noted under &lt;code&gt;build.scala.versions&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;A wave of sbt&amp;#8217;s magic wand and&amp;#8230; &lt;strong&gt;FAIL&lt;/strong&gt;!&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;[info] Building project meow 0.1.0 against Scala 2.8.0.Beta1
[info]    using MeowProject with sbt 0.7.1 and Scala 2.7.7
[info] 
[info] == compile ==
[info]   Source analysis: 1 new/modified, 0 indirectly invalidated, 0 removed.
[info] Compiling main sources...
[error] [snip]/meow/src/main/scala/meow/Growl.scala:23: missing arguments for method getLines in class Source;
[error] follow this method with `_' if you want to treat it as a partially applied function
[error] Error occured in an application involving default arguments.
[error] Error occured in an application involving default arguments.
[error]   val bin = Source.fromInputStream(which getInputStream).getLines.mkString("").trim
[error]                                                          ^
[error] one error found
[info] == compile ==
[error] Error running compile: Compilation failed
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;What&amp;#8217;s this? &lt;code&gt;Source#getLines&lt;/code&gt; and default arguments?&lt;/p&gt;

&lt;p&gt;I hopped over the the scala doc online for this method and things looked &lt;a href="http://www.scala-lang.org/docu/files/api/scala/io/Source.html#getLines" target="_blank"&gt;fine&lt;/a&gt; according to the documentation.&lt;/p&gt;

&lt;p&gt;&lt;aside&gt;
On an aside, it should be noted that Scala&amp;#8217;s methods can have zero arguments &lt;strong&gt;implicitly&lt;/strong&gt; by declaring a method&amp;#8217;s signature to be &lt;code&gt;def foo&lt;/code&gt; with the &lt;a href="http://en.wikipedia.org/wiki/Uniform_access_principle" target="_blank"&gt;uniform access principle&lt;/a&gt; in mind or &lt;strong&gt;explicitly&lt;/strong&gt; by declaring a method&amp;#8217;s signature to be &lt;code&gt;def foo()&lt;/code&gt; with a set of empty parentheses. In general, those without parentheses are assumed to &lt;strong&gt;have no side effects&lt;/strong&gt; and those with parentheses are assumed to &lt;strong&gt;have some side effects&lt;/strong&gt;.  When you define a method without parentheses you &lt;strong&gt;can not&lt;/strong&gt; invoke it with parentheses.
&lt;/aside&gt;&lt;/p&gt;

&lt;p&gt;So what happened here?&lt;/p&gt;

&lt;p&gt;I cracked open the scala 2.8.0 beta library source jar to get the definition of its &lt;code&gt;Source#getLines&lt;/code&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;/** returns an iterator who returns lines (NOT including newline character(s)).
 *  If no separator is given, the platform-specific value "line.separator" is used.
 *  a line ends in \r, \n, or \r\n.
 */
def getLines(separator: String = compat.Platform.EOL): Iterator[String] =
  new LineIterator(separator)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Hrm, looks like we&amp;#8217;ve got a bit of backwards compatibility problem here. &lt;code&gt;Source#getLines&lt;/code&gt; will compile in anything &amp;lt; 2.8 but will not in anything &amp;gt; 2.8 and &lt;code&gt;Source#getLines()&lt;/code&gt; will compile in anything &amp;gt; 2.8 but won&amp;#8217;t in anything &amp;lt; 2.8.&lt;/p&gt;

&lt;h3&gt;a transitioning suitcase&lt;/h3&gt;

&lt;p&gt;After some thinking, I decided it might be best to wrap &lt;code&gt;Source&lt;/code&gt; in some new clothes that would insulate me from the cold winds of this compatibility issue until I can come up with a better solution.  I came up with a &lt;code&gt;Transitioning&lt;/code&gt; module that adds a &lt;code&gt;lines&lt;/code&gt; method to &lt;code&gt;Source&lt;/code&gt; and which I can add other implicit wrappers to if I find other issues.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;object Transitioning {
  import scala.io.Source
  class TransitionalSource(src: Source) {
    class LineIterator(iter: BufferedIterator[Char], separator: String) extends Iterator[String] {
      require(separator.length &amp;lt; 3, "Line separator may be 1 or 2 characters only.")

      private[this] val isNewline: Char =&amp;gt; Boolean =
        separator.length match {
          case 1 =&amp;gt; _ == separator(0)
          case 2 =&amp;gt; {
            _ == separator(0) &amp;amp;&amp;amp; iter.hasNext &amp;amp;&amp;amp; {
              val res = iter.head == separator(1) // peek ahead
              if (res) { iter.next } // incr iter
              res
            }
          }
        }

      private[this] val builder = new StringBuilder

      private def buildingLine() = iter.next match {
        case nl if(isNewline(nl)) =&amp;gt; false
        case ch =&amp;gt;  { 
          builder append ch
          true
        }
      }

      def hasNext = iter.hasNext
      def next = {
        builder.clear
        while (hasNext &amp;amp;&amp;amp; buildingLine()) {}
        builder.toString
      }
    }
    def lines = new LineIterator(src.buffered, compat.Platform.EOL)
  }
  implicit def src2transitionalSrc(src: Source) = new TransitionalSource(src)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;To test this, let&amp;#8217;s say we have a file called &lt;code&gt;test.txt&lt;/code&gt; that contains the contents:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;hi
there
file
of lines
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;As an example let&amp;#8217;s try the following.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;import Transitioning 
import scala.io.Source
import java.io.FileInputStream

Source.fromInputStream(new FileInputStream("test.txt")) lines // List(hi, there, file, of lines)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Sweet! It works.&lt;/p&gt;

&lt;p&gt;As a reconfirmation, I added &lt;a href="http://github.com/softprops/meow/blob/master/src/main/scala/meow/Transitioning.scala" target="_blank"&gt;this module&lt;/a&gt; to my project, swapped &lt;code&gt;Source#getLines&lt;/code&gt; with &lt;code&gt;Source#lines&lt;/code&gt; in my own source code, re-waved sbt&amp;#8217;s magic &lt;code&gt;+compile&lt;/code&gt; wand and&amp;#8230; &lt;strong&gt;WIN&lt;/strong&gt;!&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;[info] Building project meow 0.1.0 against Scala 2.8.0.Beta1
[info]    using MeowProject with sbt 0.7.1 and Scala 2.7.7
[info] 
[info] == compile ==
[info]   Source analysis: 2 new/modified, 0 indirectly invalidated, 0 removed.
[info] Compiling main sources...
[info] Compilation successful.
[info]   Post-analysis: 24 classes.
[info] == compile ==
[success] Successful.
&lt;/code&gt;&lt;/pre&gt;</description><link>http://asoftsea.tumblr.com/post/529750770</link><guid>http://asoftsea.tumblr.com/post/529750770</guid><pubDate>Sun, 18 Apr 2010 00:10:00 -0400</pubDate><category>scala2.8.0</category></item><item><title>there are only some ways to mix a cat</title><description>&lt;h3&gt;A newish mixology&lt;/h3&gt;

&lt;p&gt;There&amp;#8217;s something to be said for the ability of a language allow the design types of objects through composition rather than traditional class inheritance. &lt;strong&gt;It awesome!&lt;/strong&gt; Aristotle was correct in that things can be classified according to thier &lt;a href="http://en.wikipedia.org/wiki/Aristotle#Classification_of_living_things" target="_blank"&gt;common characteristics&lt;/a&gt;. In software, it seems kind of overly ambitious to think that this should be the same basis of all object design.  Most of my own charateristics did not come from my parents.  I aquired them from my own schooling and experiences and interactions with others.&lt;/p&gt;

&lt;p&gt;Designing software in terms of composition, &lt;a href="http://en.wikipedia.org/wiki/Mixin" target="_blank"&gt;mixins&lt;/a&gt;, also promotes code reuse, because you are isolating areas of concern which can be reused across multiple class hierarchies. When you narrow your areas of concern, your design of modules can be very specialized in a decoupled manner, not really carring about who thier daddy is.&lt;/p&gt;

&lt;p&gt;Think about this the next time you add new behavior to your classes: Is your class an instance of iterable or is iterable a trait that can be observed in multiple types? Is your class an instance of a behavior or is the behavior a trait that can be observered in multiple types. It&amp;#8217;s all a matter of perspective. Lately I&amp;#8217;ve been siding on the former side of the previous two statements.&lt;/p&gt;

&lt;h3&gt;Mix ins and outs of Scala and Ruby&lt;/h3&gt;

&lt;p&gt;In Scala, mixin composition comes in the form of abstractions called &lt;code&gt;traits&lt;/code&gt;. Languages like Ruby also provide a means of designing software through composition via &lt;code&gt;Modules&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In Ruby you express mixins as:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;module A
  def foo
    puts "I was born in module A"
  end
end
module B
  def bar
    puts "I was born in module B"
  end
end
class C
  include A
  include B
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And in Scala:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;trait A {
  def foo = "I was born in trait A"
}
trait B {
  def bar = "I was born in trait B"
}
class C extends A with B
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Sometimes it&amp;#8217;s you want to mix in behavior for only one instance of a type. In Scala you can do this easly in the form:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class D
val specialD = new D() with A with B
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;My first attempt do this in Ruby failed. At first I thought I could do something like:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class D
end
special_d = D.new.instance_eval {
  [A, B].each { |mod| self.class.send :include, mod }
} #=&amp;gt; [A, B]
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;but the &lt;code&gt;D.new.instance_val&lt;/code&gt; actually returns an array of &lt;code&gt;A&lt;/code&gt; and &lt;code&gt;B&lt;/code&gt; so you couldn&amp;#8217;t immediately call a module method on the resulting value.&lt;/p&gt;

&lt;p&gt;This actually adds the mixin modules &lt;code&gt;A&lt;/code&gt; and &lt;code&gt;B&lt;/code&gt; to all instances of &lt;code&gt;D&lt;/code&gt; defeating the purpose of the exercise.&lt;/p&gt;

&lt;p&gt;You can actually create a new anonymous class with the modules &lt;code&gt;A&lt;/code&gt; and &lt;code&gt;B&lt;/code&gt; included but I couldn&amp;#8217;t figure out a way to get this class to extend &lt;code&gt;D&lt;/code&gt; inline without &lt;code&gt;D&lt;/code&gt; itself being a Module.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;mixins_without_d = Class.new { |cls| 
   [A, B].each { |mod| self.class.send :include, mod } 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That aside, Scala still has on more trick up it&amp;#8217;s sleeve: &lt;strong&gt;a static type system&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;It&amp;#8217;s not really worth bringing up the static verses dynamic type systems debate here, but this static type system does provide a nice built-in mechanism for specifying what kinds of objects the trait can be mixed into.&lt;/p&gt;

&lt;h3&gt;Way down to mixico&lt;/h3&gt;

&lt;p&gt;Let&amp;#8217;s take and example of cat-like behavior.&lt;/p&gt;

&lt;p&gt;In Ruby:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;module CatLike
  def pur
    "purrrrrr"
  end
  def meow
    "meeeowww"
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And in Scala:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;trait CatLike {
  def pur = "purrrrrr"
  def meow = "meeeowww"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Let&amp;#8217;s mix in this behavior.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class Tiger
  include CatLike
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;and in Scala:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class Tiger extends CatLike
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The nice thing about mixins that that they can be reused in other objects. Let&amp;#8217;s try that.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class Spoon
  include CatLike
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And in Scala:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class Spoon extends CatLike
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Hey! Spoons aren&amp;#8217;t very cat-like! Let&amp;#8217;s revisit our CatLike mixin.&lt;/p&gt;

&lt;h3&gt;Me-ouch&lt;/h3&gt;

&lt;p&gt;In Scala, we can declare what types of objects a trait can be mixed into with &lt;code&gt;self types&lt;/code&gt;.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;trait Feline
trait CatLike { this: Feline =&amp;gt;
  def pur = "purrrrrr"
  def meow = "meeeowww"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now, let&amp;#8217;s try that one more time.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class Tiger extends Feline with CatLike
class Spoon extends CatLike // compile time error!
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You can &lt;em&gt;technically&lt;/em&gt; do this in Ruby, although you need to cleverly invent your own technique.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;module Feline
end
module CatLike
  # how often do you see module authors write code like this?
  def self.included(other)
    raise TypeError.new(
      "%s was not a feline" % other
    ) if !other.included_modules.include?(Feline)
  end
end

class Tiger
  include Feline
  include CatLike
end

class Spoon
  include CatLike # TypeError: Spoon was not a feline
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Not only is that more verbose, but it requires a metaprogramming trick. The Scala implementation also won&amp;#8217;t even compile. A client of the Ruby version of CatLike will not find the error until the class is required.&lt;/p&gt;

&lt;p&gt;To be fair I am using static type system idiom for filtering, using a type as inference to what kinds of behavior should be allowed. Let&amp;#8217;s revisit that once again with rubyism for determining type: &lt;code&gt;respond_to?&lt;/code&gt;.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;module CatLike
  # more Ruby-like but still not very common for module authors
  def self.included(other)
    raise TypeError.new(
      "%s did not respond to %s" % [other, "whip_tail"]
    ) if !other.respond_to? :whip_tail
  end
  def pur
    "purrrrrr"
  end
  def meow
    "meeeowww"
  end
end

class Tiger
  def whip_tail
    puts "tail whippin'"
  end
  include CatLike # what? TypeError: Tiger did not respond to whip_tail. Yes it does!
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The module author thumbs through their copy of the Ruby manual. Whoops!
&lt;code&gt;Other&lt;/code&gt; in this context is a &lt;code&gt;class&lt;/code&gt; object, not the instance of a class.&lt;/p&gt;

&lt;p&gt;Let&amp;#8217;s revise that module one more time.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;module CatLike
  # again, more Ruby-like but even still not very common for module authors
  def self.included(other)
    raise TypeError.new(
      "%s did not respond to %s" % [other, "whip_tail"]
    ) if !other.instance_methods.select { |method| method == :whip_tail }
  end
  def pur
    "purrrrrr"
  end
  def meow
    "meeeowww"
  end
end


class Tiger
  def whip_tail
    puts "tail whippin'"
  end
  include CatLike # ok
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Can Scala play the duck typing game with cats? Surely it&amp;#8217;s going to be way more difficult in a ridged staticaly typed language and we will have to resort to some ugly and verbose reflection code.&lt;/p&gt;

&lt;p&gt;Let&amp;#8217;s revise our scala mix in using the same type of Rubyism.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;trait CatLike { this: { def whipTail } =&amp;gt;
  def pur = "purrrrrr"
  def meow = "meeeowww"
}

class Tiger extends CatLike {
  def whipTail = println("tail whippin'")
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;What&amp;#8217;s this? This looks a bit more consise than the Ruby implementation while using a Rubyism &lt;em&gt;and&lt;/em&gt; it&amp;#8217;s statically typed.&lt;/p&gt;

&lt;h3&gt;Summary&lt;/h3&gt;

&lt;p&gt;Though this was a trival example of using mixin composition, I hope we covered some of the strengths of using Scala&amp;#8217;s traits as mixins: inline object composition and a built-in means of mixin restriction.&lt;/p&gt;

&lt;p&gt;Mixin composition is common in many Ruby libraries, though many module authors don&amp;#8217;t restrict the types of objects a module can be mixed into which sometimes leads to meowing flatware.&lt;/p&gt;

&lt;p&gt;Most language design exclusions in Ruby can be be worked around using metaprogramming techniques which is really powerfull but sometimes leads to indirectly verbose code, skewing attention away from the original intent of the code.&lt;/p&gt;

&lt;p&gt;On most days, if I end out writing less code to do the same task, I consider that a day well spent.&lt;/p&gt;</description><link>http://asoftsea.tumblr.com/post/496938466</link><guid>http://asoftsea.tumblr.com/post/496938466</guid><pubDate>Sun, 04 Apr 2010 21:13:00 -0400</pubDate></item><item><title>an old man and a park bench</title><description>&lt;p&gt;I sat down next to an old man on a park bench today. He said to me:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Are you just going to sit there?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So I said:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Why do you ask?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To which he replied:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;I&amp;#8217;m an old man.  These days, what I enjoy most is to sit here and watch others go about their business, over and over, and over again.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I went on my way. I came back to the bench the next day and the old man and the bench was gone.&lt;/p&gt;

&lt;p&gt;So I build myself a new bench to sit and watch on my own, occasionally glancing at my watch.&lt;/p&gt;

&lt;h3&gt;To Build a Bench&lt;/h3&gt;

&lt;p&gt;I bought a hammer and a few planks of wood and this is what I made:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;package park

trait Bench {
  object Timing {
    case class Time(cpu: Long, user: Long, system: Long, real: Long) {
      val tuple = (cpu, user, system, real)
      def - (t: Time) = Time(
        cpu - t.cpu, user - t.user, system - t.system, real - t.real
      )
      def + (t: Time) = Time(
        cpu + t.cpu, user + t.user, system + t.system, real + t.real
      )
    }
    def cpu = get(bean getCurrentThreadCpuTime)
    def user = get(bean getCurrentThreadUserTime)
    def real = System.nanoTime
    def system = cpu - user
    def now = Time(cpu, user, system, real)
    val empty = Time(0l, 0l, 0l, 0l)
    def get(fn: =&amp;gt; Long) = if(bean isCurrentThreadCpuTimeSupported) fn else 0l
    private val bean = java.lang.management.ManagementFactory.getThreadMXBean
  }

  def watch(n: Int, o: Int) = { 
    headers
    for(i &amp;lt;- Stream.range(0, o)) collect(n)
  }

  def collect(times: Int) = tell(subject, (Timing.empty /: observe(times))(_+_))

  def subject = getClass getName

  def observe(n: Int) = for(i &amp;lt;- Stream.range(0, n)) yield time { run }

  def time(block: =&amp;gt; Any) = {
    val before = Timing.now
    block
    val after = Timing.now
    after - before
  }

  def headers = println(
     (" "*subject.size)+"\t\tcpu\t\tuser\t\tsystem\t\treal"
   )

  def tell(subject: String, time: Timing.Time) = {
    def sec(n: Long) = n / 1000000000.0
    val (cpu, user, system, real) = time.tuple
    println(
      "%s\t\t%f\t%f\t%f\t%f" format(
        subject, sec(cpu), sec(user), sec(system), sec(real)
       )
    )
  }

  /** where unicorns do run wild */
  def run
}
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Bird Watching and other Leisurely Observations&lt;/h3&gt;

&lt;p&gt;I stepped back to have a look at this new park bench.&lt;/p&gt;

&lt;p&gt;From here, I can watch flocks of inline invokations fly by, one or more times.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;scala&amp;gt; val b = new Bench { def run = 1+1; override def subject = "plus" }
scala&amp;gt; b watch(2, 2)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I jotted down what I saw.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;            cpu             user        system     real
plus        0.000067        0.000027    0.000021   0.000046
plus        0.000054        0.000028    0.000021   0.000108
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Under a tree, I can watch those pass the time by reading favored books.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;object BookShelfBench extends BookShelf with park.Bench {
  def run = read(Book("a tale of two cities"))
  override def subject = "reading"
}

BookShelfBench watch(10, 1)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;To be honest, I really didn&amp;#8217;t have time to watch them finish this book.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;              cpu      user     system   real
reading        a       really   long     time...
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This was just a quick and silly re-composition of an idea I had while glancing at scala.testing.Benchmark.  This idea may become a &lt;a href="http://github.com/softprops/park-bench" target="_blank"&gt;real project&lt;/a&gt; as some point on the &lt;a href="http://github.com/softprops/park-bench" target="_blank"&gt;githubs&lt;/a&gt;.  For now it was just flash coding. Isn&amp;#8217;t composition fun?&lt;/p&gt;</description><link>http://asoftsea.tumblr.com/post/467308400</link><guid>http://asoftsea.tumblr.com/post/467308400</guid><pubDate>Tue, 23 Mar 2010 01:07:00 -0400</pubDate><category>benchmarks</category></item><item><title>scope escaping shadow of peter pan</title><description>&lt;p&gt;I just thought the error message the following code generates was a little silly.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;object PeterPan {
  private case class Shadow(length: Int)
  def shadow = Shadow(10)
}

error: private class Shadow escapes its defining scope as part of type PeterPan.Shadow
&lt;/code&gt;&lt;/pre&gt;</description><link>http://asoftsea.tumblr.com/post/464909029</link><guid>http://asoftsea.tumblr.com/post/464909029</guid><pubDate>Mon, 22 Mar 2010 00:28:46 -0400</pubDate></item><item><title>an applied case for red ninjas</title><description>&lt;p&gt;One of my favorite things about scala are &lt;code&gt;case&lt;/code&gt; classes.  Those coming from a java background might be used to writing code like this.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;public class Ninja {
  private int experience;
  private List&amp;lt;Weapon&amp;gt; weapons;
  private String color;

  public Ninja(int experience, List&amp;lt;Weapon&amp;gt; weapons, String color) {
    this.experience = experience;
    this.weapons = weapons;
    this.color = color;
  }

  public int getExperience() {
    return experience;
  }

  public String getColor() {
    return color;
  }

  public List&amp;lt;Weapon&amp;gt; getWeapons() {
    return weapons;
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This is commonly known as the &lt;strong&gt;bean&lt;/strong&gt; style of class design.  For now, I&amp;#8217;ll leave out the mutating &lt;code&gt;setABC(D d)&lt;/code&gt; methods that make beans mutable.  Code formatting may vary, but for this example, let&amp;#8217;s call that 23 lines of code (counting spaces) to define a type with three properties.&lt;/p&gt;

&lt;p&gt;Scala has a built-in, more simplified, idiom for this style of class design called &lt;code&gt;case&lt;/code&gt; classes.  To define the same class in scala we would write&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;case class Nina(experience: Int, weapons: List[Weapon], color: String)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Again, formatting may vary, but for this example let&amp;#8217;s call that &lt;strong&gt;one&lt;/strong&gt; line of code. This bit of syntactic nicety also provides us with a complementary &lt;code&gt;apply&lt;/code&gt; method that takes as its arguments the same as the constructor&amp;#8217;s so that we can create our classes sans the &lt;code&gt;new&lt;/code&gt; keyword by applying the arguments to our Ninja class with:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Ninja(10, List(new Sword), "red")
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Building an Army&lt;/h3&gt;

&lt;p&gt;Now lets say you want to build an army of red ninjas. You might find it more convenient to write an auxiliary constructor in the java class such as&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;  public Ninja(int experience, List&amp;lt;Weapon&amp;gt; weapons) {
    this(experience, weapons, "red");
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now you can build your army with:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;List&amp;lt;Ninja&amp;gt; redArmy = new ArrayList&amp;lt;Ninja&amp;gt;(){{
 add(new Ninja(50, Arrays.asList(new Sword())));
 add(new Ninja(60, Arrays.asList(new Bow())));
}};
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Scala too, has its own way of declaring auxiliary constructors by adding methods to classes in the form&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;def this(experience: Int, weapons: List[Weapon]) = this(experience, weapons, "red")
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Case Confusion&lt;/h3&gt;

&lt;p&gt;Recently, I ran into a gothca with auxiliary constructors in &lt;code&gt;case&lt;/code&gt; classes.&lt;/p&gt;

&lt;p&gt;Say we add an auxiliary constructor to the Scala version of Ninja as:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;case class Ninja(experience: Int, weapons: List[Weapon], color: String) {
  def this(experience: Int, weapons: List[Weapon]) = this(experience, weapons, "red")
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;When you invoke &lt;code&gt;Ninja(10, List(new Sword()))&lt;/code&gt; you will get&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;error: wrong number of arguments for method apply: (Int,List[Weapon],String)Ninja in object Ninja
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;wtf?&lt;/p&gt;

&lt;p&gt;What you are actually invoking there is Scala&amp;#8217;s implicit &lt;code&gt;apply&lt;/code&gt; method with two arguments.&lt;/p&gt;

&lt;p&gt;Ninja.apply(10, List(new Sword()))&lt;/p&gt;

&lt;p&gt;If you&amp;#8217;ve been paying attention the case class has not generated that for you.  Okay, so let&amp;#8217;s go ahead and add one to our Ninja case class.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;def apply(experience: Int, weapons: List[Weapon]) = new Ninja(experience, weapons)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This time when you invoke &lt;code&gt;Ninja(10, List(new Sword()))&lt;/code&gt; you should see the error:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;error: wrong number of arguments for method apply: (Int,List[Weapon],String)Ninja in object Ninja
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;A Friend of a Companion&lt;/h3&gt;

&lt;p&gt;Turns out the convenience of case classes also adds one restriction to defining them: &lt;strong&gt;case classes can only have one &lt;code&gt;apply&lt;/code&gt; method which is defined by the primary constructor&amp;#8217;s arguments&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Great. So now what?  Oh yea, classes can have friends too. They are called &lt;strong&gt;companion objects&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;We can work around this constraint a &lt;code&gt;companion&lt;/code&gt; object.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;object Ninja {
  /** I can haz called the auxiliary constructor of the case class Ninja */
  def apply(experience: Int, weapons: List[Weapon]) = new Ninja(experience, weapons)
}

Ninja(10, List(new Bow())) // -&amp;gt; I win!
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now we&amp;#8217;re are ready to fight.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Ninja(50, List(new Sword())) :: Ninja(60, List(new Bow())) :: Nil
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Have you any other suggestions?&lt;/p&gt;</description><link>http://asoftsea.tumblr.com/post/461410308</link><guid>http://asoftsea.tumblr.com/post/461410308</guid><pubDate>Sat, 20 Mar 2010 14:24:00 -0400</pubDate></item><item><title>implicitly subtle imports</title><description>&lt;p&gt;One of Scala&amp;#8217;s cheekier features, &lt;code&gt;implicit declarations&lt;/code&gt;, can sometimes conjure up subtle gotchas.  Besides being infamous for being a &lt;a href="http://lmgtfy.com/?q=scala+pimp+my+library" target="_blank"&gt;pimp&lt;/a&gt;, Scala&amp;#8217;s implicits are also play the role of a magician by providing the illusion of default function arguments, a first class feature in &lt;a href="http://www.scala-lang.org/node/2075" target="_blank"&gt;2.8&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I recently came across a subtle gotcha when using implicit function arguments, but first, it might be useful to catch up on a little background about Scala&amp;#8217;s implicits.&lt;/p&gt;

&lt;h3&gt;Implicit wha?&lt;/h3&gt;

&lt;p&gt;One of Scala&amp;#8217;s strong suits is freeing your tired little fingers from typing guessable information by inferring certain aspects about your code.&lt;/p&gt;

&lt;p&gt;This same inference feature can provide you with the ability to sprinkle code with hints that will tell the compiler how to react to certain syntactic events, making statically typed code feel more dynamic.&lt;/p&gt;

&lt;p&gt;Take the follow example of an implicit definition adds a ridiculous contrived &lt;code&gt;BoomLike&lt;/code&gt; behavior to all strings&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class BoomLike(str: String) {
  def boom = " %s ka-blam!!" format str
}
implicit def string2BoomLike(str: String) = new BoomLike(str)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You can now magically call the method &lt;code&gt;boom&lt;/code&gt; on any String value.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;"kitten".boom // kitten ka-blam!!
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Implicits can also be used to define arguments in function definitions.&lt;/p&gt;

&lt;p&gt;The following function will implicitly apply an &lt;code&gt;implicitly&lt;/code&gt; defined value of &lt;code&gt;b&lt;/code&gt; to the curried function &lt;code&gt;fn&lt;/code&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;def fn(a: Int)(implicit b: Int) = a + b
implicit val b = 3
fn(2) // 5
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;br/&gt;&lt;/p&gt;

&lt;h3&gt;What implicitly got me&lt;/h3&gt;

&lt;p&gt;I recently came across a use case for implicit function arguments that made me scratch my head.&lt;/p&gt;

&lt;p&gt;Let&amp;#8217;s say you have written shiny new library.  This shiny new library contains some functions that you want to apply implicit defaults values for. You don&amp;#8217;t want to obtrusively pollute the top level name space with them so you wrap them up in a module called &lt;code&gt;Defaults&lt;/code&gt;. The result of this shiny new library might then look something like the following:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;object Defaults {  
  implicit val height = 10
  implicit val frog = "geroro"  
}
def hop(distance: Int)(implicit height:Int) = distance * height
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Pop quiz!  What do you expect the execution of each to return?&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;// fig: A
import Defaults.height
hop(20)

// fig: B
import Defaults._
hop(20)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Time is up, kids!&lt;/p&gt;

&lt;p&gt;When you to try and hop with figure A, you would fail miserably with an error stating:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;error: no implicit argument matching parameter type Int was found.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You might find this odd, considering the value &lt;code&gt;height&lt;/code&gt; is visible.&lt;/p&gt;

&lt;p&gt;Were to you try and hop with figure B, you would get a value of 200.&lt;/p&gt;

&lt;p&gt;I can&amp;#8217;t yet provide a reasonable explanation of why.  Can you?&lt;/p&gt;</description><link>http://asoftsea.tumblr.com/post/451349622</link><guid>http://asoftsea.tumblr.com/post/451349622</guid><pubDate>Mon, 15 Mar 2010 22:58:57 -0400</pubDate></item><item><title>37 salad dressings</title><description>&lt;h3&gt;I&amp;#8217;m thinking of a spork with no knives&lt;/h3&gt;

&lt;p&gt;As a younger man, one of my favorite types of restaurants to eat at were buffets.  What can be better than an endless supply of everything? I pay one price and get as much as I want. So many choices!&lt;/p&gt;

&lt;p&gt;As I got a little older, I started noticing a couple things about these types of restaurants.  For starters, I never actually ate everything on the menu. I would go in and usually get the same thing every time.  It was the &lt;code&gt;idea&lt;/code&gt; that I could have anything that was so alluring, not the fact that I actually ever ate everything.  Over time I realized that the few things I actually did eat at buffets were actually pretty low in quality compared to a restaurants that solely made just one type of food.&lt;/p&gt;

&lt;h3&gt;Complexity leads to anger. Anger leads to hate. Hate leads to suffering.&lt;/h3&gt;

&lt;p&gt;Now that I am older, I am more aware of these things as they apply to other areas of my life and have come to appreciate &lt;strong&gt;single purpose&lt;/strong&gt; things.&lt;/p&gt;

&lt;p&gt;Of late, I&amp;#8217;ve been thinking about this a lot as it applies to software.&lt;/p&gt;

&lt;p&gt;I love software. I love writing software. I love reading software, but one of my biggest problems with software is when it wants to do everything.  A huge side effect of this type of software is that it tends to be unnecessarily slow.  Slow software makes for very angry bears in a campground with no jars of honey. &lt;strong&gt;People will get hurt&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;How does software get to be slow? How can we make it faster?&lt;/p&gt;

&lt;p&gt;The answer should be simple.  &lt;strong&gt;Do less&lt;/strong&gt;.  If you think your library is already pretty fast, how do you make if faster? The answer is also pretty simple.  &lt;strong&gt;Do even less&lt;/strong&gt;.  Reduce your complexity and make your code faster.  Just ask&lt;span&gt; &lt;a title="making jquery faster" target="_blank" href="http://jquery14.com/day-03/internal-changes-in-jquery-14-2"&gt;John Resig&lt;/a&gt;.&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;A while back, I read a short &lt;a title="getting real" target="_blank" href="http://gettingreal.37signals.com/index.php"&gt;book&lt;/a&gt; about a company who has had pretty good success with writing &lt;a title="building less" target="_blank" href="http://gettingreal.37signals.com/ch02_Build_Less.php"&gt;less&lt;/a&gt;.  Hey Bob! That&amp;#8217;s crazy talk!  How can you be successful by having less features?&lt;/p&gt;

&lt;p&gt;Less features means less running code. Less running code means less places where things can go wrong.  Less things going wrong means stable software.  Fast stable software makes for happy bears, content with sweet jars of honey.&lt;/p&gt;

&lt;h3&gt;Castle in the sky&lt;/h3&gt;

&lt;p&gt;The dream of O(bject)O(rientation) was to have these small interchangeable software modules that we could bind together to build bigger systems as we did as children building castles and helicopter robots with our lego sets.&lt;/p&gt;

&lt;p&gt;In a the giant Library of Congress of fat open source magic box abstractions, some of us might have &lt;a title="cut and paste" target="_blank" href="http://reprog.wordpress.com/2010/03/03/whatever-happened-to-programming/"&gt;noticed&lt;/a&gt; that the promises these libraries make often lead to frustration. How often do you find there is at least one problem of the many a library tries to solve that does not suit our needs?  Trying to hack around this one thing tends to break more than it fixes because it was not the original intent of the aforementioned library.&lt;/p&gt;

&lt;p&gt;The idea of an abstraction is to make a complex problem simple.  That is not a bad thing.  If it were, we&amp;#8217;d all be working with byte arrays rather than objects types.  Abstractions allow us to be more productive by focusing more the the problems we are trying to solve rather than low level details.  One thing we should be focusing on is giving these library abstractions a more narrow focus.&lt;/p&gt;

&lt;p&gt;I mainly deal my codings cards in the game of Uno called called web software.  By web software I usually mean web frameworks.  It&amp;#8217;s made me happy to see a &lt;a title="wsgi" target="_blank" href="http://wsgi.org/wsgi/"&gt;few&lt;/a&gt; &lt;a title="rack" target="_blank" href="http://rack.rubyforge.org/"&gt;great&lt;/a&gt; &lt;a title="plack" target="_blank" href="http://plackperl.org/"&gt;single purpose&lt;/a&gt; pieces of &lt;a title="jack" target="_blank" href="http://jackjs.org/"&gt;software&lt;/a&gt; have popped up in the past year or so to provide a &lt;strong&gt;simple&lt;/strong&gt;, &lt;strong&gt;common&lt;/strong&gt;, &lt;strong&gt;modular&lt;/strong&gt; substrate for web frameworks to build on top of.  You can bet your gingersnap cookies that I&amp;#8217;ve got something similar for scala baking in my kitchen.  On the other hand, what makes me unhappy is to see that there is still a bevy of web frameworks out there that will lock you into a &lt;a title="trap" target="_blank" href="http://go-get-me-so.me/its%20a%20trap/2"&gt;trap&lt;/a&gt;.  The trap is providing you with a buffet of features, most of which you will probably not be using in the end.&lt;/p&gt;

&lt;h3&gt;I&amp;#8217;d like to put in an order for take out&lt;/h3&gt;

&lt;p&gt;If there is one thing I&amp;#8217;d love to see in the future, it&amp;#8217;s more &lt;strong&gt;high-quality&lt;/strong&gt; &lt;strong&gt;single purpose&lt;/strong&gt; software working together as loosely coupled modules rather than one monolithic box full of assumptions about my needs.&lt;/p&gt;

&lt;p&gt;Most of the time I prefer to build my own castle with a box of colored legos over moving into the same prefab one all of the other dev&amp;#8217;s on my block live in.  If I do have to move into a prefab one, it had better be easy for me to convert into a giant helicopter robot.  If not, I&amp;#8217;d like to submit a bug.&lt;/p&gt;</description><link>http://asoftsea.tumblr.com/post/446281469</link><guid>http://asoftsea.tumblr.com/post/446281469</guid><pubDate>Sat, 13 Mar 2010 18:11:00 -0500</pubDate><category>less</category></item></channel></rss>
