<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-5336273</id><updated>2012-01-23T03:54:02.433+09:00</updated><category term='specs2'/><category term='jmock'/><category term='xml'/><category term='scala'/><category term='swing'/><category term='programming'/><category term='frp'/><category term='junit'/><category term='option'/><category term='migration'/><category term='scalacheck'/><category term='objectives'/><category term='monads'/><category term='language'/><category term='ruby functional programming'/><category term='algorithm'/><category term='lessismore'/><category term='testcomponent'/><category term='gui'/><category term='parsercombinator'/><category term='test'/><category term='decision'/><category term='arrows'/><category term='guice'/><category term='scalaz'/><category term='specs'/><category term='commando'/><category term='fit'/><category term='iterator'/><category term='haskell'/><category term='functional programming'/><category term='projectmanagement'/><category term='icfp08'/><category term='mockito'/><category term='2008'/><category term='json'/><title type='text'>A++ [Eric Torreborre's Blog]</title><subtitle type='html'>Lots of assertions, some pass, some fail</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://etorreborre.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5336273/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://etorreborre.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Eric</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://farm3.static.flickr.com/2203/2118752971_7becaf7476_t.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>81</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-5336273.post-5051232435859906640</id><published>2011-12-09T10:29:00.001+09:00</published><updated>2011-12-09T10:56:02.995+09:00</updated><title type='text'>Pragmatic IO - part 3</title><content type='html'>&lt;p&gt;A follow-up to &lt;a href="http://etorreborre.blogspot.com/2011/12/pragmatic-io-part-2.html"&gt;my previous posts about &lt;code class="prettyprint"&gt;IO&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;&lt;a name="Why+types+and+laws+matter"&gt;&lt;/a&gt;&lt;h4&gt;Why types and laws matter&lt;/h4&gt;&lt;p&gt;It's embarrassing to write post after post only to find out I keep being wrong :-). While the technique I described in my last post (using &lt;code class="prettyprint"&gt;StreamT&lt;/code&gt;) works ok, I actually don't have to use it. The &lt;code class="prettyprint"&gt;traverse&lt;/code&gt; technique explained in &lt;a href="http://etorreborre.blogspot.com/2011/06/essence-of-iterator-pattern.html"&gt;EIP&lt;/a&gt; works perfectly, provided that:&lt;/p&gt;&lt;ul&gt;  &lt;li&gt;&lt;p&gt;we write a proper &lt;code class="prettyprint"&gt;Traverse&lt;/code&gt; instance using &lt;code class="prettyprint"&gt;foldLeft&lt;/code&gt; (as explained in the &lt;a href="http://etorreborre.blogspot.com/2011/12/pragmatic-io-part-2.html"&gt;first part of this post&lt;/a&gt;)&lt;/p&gt;&lt;/li&gt;  &lt;li&gt;&lt;p&gt;we &lt;code class="prettyprint"&gt;trampoline&lt;/code&gt; the &lt;code class="prettyprint"&gt;State&lt;/code&gt; to avoid Stack overflows due to a long chain of &lt;code class="prettyprint"&gt;flatMaps&lt;/code&gt; during the traversal&lt;/p&gt;&lt;/li&gt;  &lt;li&gt;&lt;p&gt;we use the right types!&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;a name="Get+the+types+right"&gt;&lt;/a&gt;&lt;h5&gt;Get the types right&lt;/h5&gt;&lt;p&gt;That's the point I want to insist on today. Last evening I read the revisited &lt;a href="https://github.com/scalaz/scalaz/blob/scalaz-seven/example/src/main/scala/scalaz/example/WordCount.scala#L35"&gt;WordCount example&lt;/a&gt; in Scalaz-seven which is like the canonical example of using the EIP ideas. Two words in the comments struck my mind: &amp;quot;compose&amp;quot; and &amp;quot;fuse&amp;quot;. Indeed, one very important thing about EIP is the ability to compose &lt;code class="prettyprint"&gt;Applicatives&lt;/code&gt; so that their actions should &amp;quot;fuse&amp;quot; during a traversal. As if they were executed in the same &amp;quot;for&amp;quot; loop!&lt;/p&gt;&lt;p&gt;So, when I wrote in my previous post that I needed to traverse the stream of lines several times to get the results, something had to be wrong somewhere. The types were wrong!&lt;/p&gt;&lt;ol&gt;  &lt;li&gt;instead of traversing with &lt;code class="prettyprint"&gt;State[S, *]&lt;/code&gt; where &lt;code class="prettyprint"&gt;* =:= IO[B]&lt;/code&gt;, I should traverse with &lt;code class="prettyprint"&gt;State[S, IO[*]]&lt;/code&gt;&lt;/li&gt;  &lt;li&gt;what I get back is a &lt;code class="prettyprint"&gt;State[S, IO[Seq[B]]]&lt;/code&gt;, instead of a &lt;code class="prettyprint"&gt;State[S, Seq[IO[B]]&lt;/code&gt;&lt;/li&gt;  &lt;li&gt;this matters because passing in an initial state then returns &lt;code class="prettyprint"&gt;IO[Seq[B]]&lt;/code&gt; instead of &lt;code class="prettyprint"&gt;Seq[IO[B]]&lt;/code&gt;&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;Exactly what I want, without having to &lt;code class="prettyprint"&gt;sequence&lt;/code&gt; anything.&lt;/p&gt;&lt;a name="Use+the+laws"&gt;&lt;/a&gt;&lt;h5&gt;Use the laws&lt;/h5&gt;&lt;p&gt;Not only I get what I want, but also it is conceptually right as the result of an important &lt;code class="prettyprint"&gt;traverse&lt;/code&gt; law:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  traverse(f compose g) == traverse(f) compose traverse(g)&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;That &amp;quot;fusion&amp;quot; law guarantees that composing 2 effects can be fused, and executed in only one traversal. It's worth instantiating &lt;code class="prettyprint"&gt;f&lt;/code&gt; and &lt;code class="prettyprint"&gt;g&lt;/code&gt; to make that more concrete:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  // what to do with the line and line number&lt;br /&gt;  def importLine(i: Int, l: String): (Int, IO[Unit]) =&lt;br /&gt;    (i, storeLine(l) &amp;gt;&amp;gt;= println(&amp;quot;imported line &amp;quot;+i))&lt;br /&gt;&lt;br /&gt;  // `g` keeps track of the line number&lt;br /&gt;  val g : String =&amp;gt; State[Int, String] =&lt;br /&gt;    (s: String) =&amp;gt; state((i: Int) =&amp;gt; (i+1, s))&lt;br /&gt;&lt;br /&gt;  // `f` takes the current line/line number and do the import/reporting&lt;br /&gt;  val f : State[Int, String] =&amp;gt; State[Int, IO[Unit]] =&lt;br /&gt;    st =&amp;gt; state((i: Int) =&amp;gt; importLine(st(i)))&lt;br /&gt;&lt;br /&gt;  // `f compose g` fuses both actions&lt;br /&gt;  val f_g: String =&amp;gt; State[Int, IO[Unit]] = f compose g&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;I'm really glad that I was able to converge on established principles. Why couldn't I see this earlier?&lt;/p&gt;&lt;a name="Scala+and+Scalaz-seven+might+help"&gt;&lt;/a&gt;&lt;h4&gt;Scala and Scalaz-seven might help&lt;/h4&gt;&lt;p&gt;In retrospect, I remember what led me astray. When I realized that I had to use a State transformer with a &lt;code class="prettyprint"&gt;Trampoline&lt;/code&gt;, I just got scared by the &lt;code class="prettyprint"&gt;Applicative&lt;/code&gt; instance I had to provide. Its type is:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  /**&lt;br /&gt;   * Here we want M1 to `Trampoline` and M2 to be `IO`&lt;br /&gt;   */&lt;br /&gt;  implicit def StateTMApplicative[M1[_]: Monad, S, M2[_] : Applicative] =&lt;br /&gt;    Applicative.applicative[({type l[a] = StateT[M1, S, M2[a]]})#l]&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;I also need some &lt;code class="prettyprint"&gt;Pure&lt;/code&gt; and &lt;code class="prettyprint"&gt;Apply&lt;/code&gt; instances in scope:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  implicit def StateTMApply[M1[_]: Monad, S, M2[_] : Applicative] =&lt;br /&gt;    new Apply[({type l[a]=StateT[M1, S, M2[a]]})#l] {&lt;br /&gt;      def apply[A, B](f: StateT[M1, S, M2[A =&amp;gt; B]], a: StateT[M1, S, M2[A]]) =&lt;br /&gt;        f.flatMap(ff =&amp;gt; a.map(aa =&amp;gt; aa &amp;lt;*&amp;gt; ff))&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;  implicit def StateTMPure[M1[_] : Pure, S, M2[_] : Pure] =&lt;br /&gt;    new Pure[({type l[a]=StateT[M1, S, M2[a]]})#l] {&lt;br /&gt;      def pure[A](a: =&amp;gt; A) = stateT((s: S) =&amp;gt; (s, a.pure[M2]).pure[M1])&lt;br /&gt;    }&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Once it's written, it may not seem so hard but I got very confused trying to get there. How can it be made easier? First, we could have better type annotations for partial type application, like:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  // notice the * instead of the &amp;quot;type l&amp;quot; trick&lt;br /&gt;  implicit def StateTMApplicative[M1[_]: Monad, S, M2[_] : Applicative] =&lt;br /&gt;    Applicative.applicative[StateT[M1, S, M2[*]]]&lt;br /&gt;&lt;br /&gt;  implicit def StateTMApply[M1[_]: Monad, S, M2[_] : Applicative] =&lt;br /&gt;    new Apply[StateT[M1, S, M2[*]]] {&lt;br /&gt;      def apply[A, B](f: StateT[M1, S, M2[A =&amp;gt; B]], a: StateT[M1, S, M2[A]]) =&lt;br /&gt;        f.flatMap(ff =&amp;gt; a.map(aa =&amp;gt; aa &amp;lt;*&amp;gt; ff))&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;  implicit def StateTMPure[M1[_] : Pure, S, M2[_] : Pure] =&lt;br /&gt;    new Pure[StateT[M1, S, M2[*]]] {&lt;br /&gt;      def pure[A](a: =&amp;gt; A) = stateT((s: S) =&amp;gt; (s, a.pure[M2]).pure[M1])&lt;br /&gt;    }&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;And with a better type inference, the first definition could be even be (we can always dream :-)):&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  implicit def StateTMApplicative[M1[_]: Monad, S, M2[_] : Applicative]:&lt;br /&gt;    Applicative[StateT[M1, S, M2[*]]] = Applicative.applicative&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Which means that it may even be removed, just import &lt;code class="prettyprint"&gt;Applicative.applicative&lt;/code&gt;!&lt;/p&gt;&lt;p&gt;Actually &lt;a href="https://github.com/scalaz/scalaz/tree/scalaz-seven"&gt;Scalaz-seven&lt;/a&gt; might help by:&lt;/p&gt;&lt;ul&gt;  &lt;li&gt;&lt;p&gt;providing those instances out-of-the box&lt;/p&gt;&lt;/li&gt;  &lt;li&gt;&lt;p&gt;even better, provide combinators to create those instances easily. That's what the &lt;a href="https://github.com/scalaz/scalaz/blob/scalaz-seven/example/src/main/scala/scalaz/example/WordCount.scala#L36"&gt;&lt;code class="prettyprint"&gt;compose&lt;/code&gt; method&lt;/a&gt; does&lt;/p&gt;&lt;/li&gt;  &lt;li&gt;&lt;p&gt;give even better type inference. Look at the &lt;code class="prettyprint"&gt;traverseU&lt;/code&gt; method &lt;a href="https://github.com/scalaz/scalaz/blob/bd79e134e513bc5686db8109006b90eeff771a67/example/src/main/scala/scalaz/example/StateTUsage.scala#L26"&gt;here&lt;/a&gt;, no type annotations Ma!&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Now that the fundamentals are working ok for my application, I can go back to adding features, yay!&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5336273-5051232435859906640?l=etorreborre.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://etorreborre.blogspot.com/feeds/5051232435859906640/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5336273&amp;postID=5051232435859906640' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5336273/posts/default/5051232435859906640'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5336273/posts/default/5051232435859906640'/><link rel='alternate' type='text/html' href='http://etorreborre.blogspot.com/2011/12/pragmatic-io-part-3.html' title='Pragmatic IO - part 3'/><author><name>Eric</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://farm3.static.flickr.com/2203/2118752971_7becaf7476_t.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5336273.post-202161769885031536</id><published>2011-12-08T15:32:00.001+09:00</published><updated>2011-12-09T05:34:09.560+09:00</updated><title type='text'>Pragmatic IO - part 2</title><content type='html'>&lt;p&gt;A follow-up to &lt;a href="http://etorreborre.blogspot.com/2011/12/pragmatic-io.html"&gt;my previous post about &lt;code class="prettyprint"&gt;IO&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;&lt;a name="Learning+by+being+wrong%2C+very+wrong"&gt;&lt;/a&gt;&lt;h4&gt;Learning by being wrong, very wrong&lt;/h4&gt;&lt;p&gt;I didn't think that diving into the functional &lt;code class="prettyprint"&gt;IO&lt;/code&gt; world would have me think so hard about how to use some FP concepts.&lt;/p&gt;&lt;a name="Mind+the+law"&gt;&lt;/a&gt;&lt;h5&gt;Mind the law&lt;/h5&gt;&lt;p&gt;The technique I described at the end of my previous post didn't really work. It was wrong on many accounts. First of all, my&lt;br /&gt;&lt;code class="prettyprint"&gt;Traverse&lt;/code&gt; instance was buggy because I was reversing the traversed &lt;code class="prettyprint"&gt;Stream&lt;/code&gt;. This is why I had inverted messages in the console. During the traversal, starting from the left, I needed to &lt;em&gt;append&lt;/em&gt; each traversed element to the result, not &lt;em&gt;prepend&lt;/em&gt; it:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  /**&lt;br /&gt;   * WRONG: because each new element `b` must be appended to the result.&lt;br /&gt;   * it should be bs :+ b instead&lt;br /&gt;   */&lt;br /&gt;  def SeqLeftTraverse: Traverse[Seq] = new Traverse[Seq] {&lt;br /&gt;    def traverse[F[_]: Applicative, A, B](f: A =&amp;gt; F[B], as: Seq[A]): F[Seq[B]] =&lt;br /&gt;      as.foldl[F[Seq[B]]](Seq[B]().pure) { (ys, x) =&amp;gt;&lt;br /&gt;        implicitly[Apply[F]].apply(f(x) map ((b: B) =&amp;gt; (bs: Seq[B]) =&amp;gt; b +: bs), ys)&lt;br /&gt;      }&lt;br /&gt;  }&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;It is important to mention here that proper testing should have caught this. There are some laws that applicative traversals must satisfy (see &lt;a href="http://www.cs.ox.ac.uk/jeremy.gibbons/publications/iterator.pdf"&gt;EIP, section 5&lt;/a&gt;. One of them is that &lt;code class="prettyprint"&gt;seq.traverse(identity) == seq&lt;/code&gt;. This is obviously not the case when I returned a reverted sequence.&lt;/p&gt;&lt;a name="Not+at+the+right+time"&gt;&lt;/a&gt;&lt;h5&gt;Not at the right time&lt;/h5&gt;&lt;p&gt;Then I realized something also which was also worrying. On big files, my application would do a lot, &lt;em&gt;then&lt;/em&gt;, report its activity to the console. This would definitely not have happened in a simple for loop with unrestricted effects!&lt;/p&gt;&lt;p&gt;So I set out to find a better implementation for my file reading / records saving. The solution I'm going to present here is just one way of doing it, given &lt;em&gt;my&lt;/em&gt; way of constraining the problem. There are others, specifically the use of &lt;code class="prettyprint"&gt;Iteratees&lt;/code&gt;.&lt;/p&gt;&lt;a name="My+goal"&gt;&lt;/a&gt;&lt;h4&gt;My goal&lt;/h4&gt;&lt;p&gt;This is what I want to do: I want to read lines from a file, transform them into &amp;quot;records&amp;quot; and store them in a database. Along the way, I want to inform the user of the progress of the task, based on the number of lines already imported.&lt;/p&gt;&lt;p&gt;More precisely, I want 2 classes with the following interfaces:&lt;/p&gt;&lt;ul&gt;  &lt;li&gt;&lt;p&gt;the &lt;code class="prettyprint"&gt;Source&lt;/code&gt; class is capable of reading lines and do something on each line, possibly based on some state, like the number of lines already read.&lt;br /&gt; The &lt;code class="prettyprint"&gt;Source&lt;/code&gt; class should also be responsible for closing all the opened resources whether things go wrong or not&lt;/p&gt;&lt;/li&gt;  &lt;li&gt;&lt;p&gt;the &lt;code class="prettyprint"&gt;Extractor&lt;/code&gt; class should provide a function declaring what to do with the current line and state. Ideally it should be possible to create that function by composing independent actions: storing records in the db and / or printing messages on the console&lt;/p&gt;&lt;/li&gt;  &lt;li&gt;&lt;p&gt;and, by the way, I don't want this to go out-of-memory or stack overflow at run-time just because I'm importing a big file :-)&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Mind you, it took me quite a while to arrive there. In retrospect, I could have noticed that:&lt;/p&gt;&lt;ul&gt;  &lt;li&gt;&lt;p&gt;all the processing has to take place inside the &lt;code class="prettyprint"&gt;Source.readLines&lt;/code&gt; method. Otherwise there's no way to close the resources properly (well unless you're using Iteratees, what Paul Chiusano refers to as &lt;a href="https://groups.google.com/d/msg/scalaz/QPUs6TWTAm4/ccLBtRZB0KMJ"&gt;&amp;quot;Consumer&amp;quot; processing&lt;/a&gt;)&lt;/p&gt;&lt;/li&gt;  &lt;li&gt;&lt;p&gt;the signature of the function passed to &lt;code class="prettyprint"&gt;Source.readLines&lt;/code&gt; has to be &lt;code class="prettyprint"&gt;String =&amp;gt; State[IO[A]]&lt;/code&gt;, meaning that for each line, we're doing an &lt;code class="prettyprint"&gt;IO&lt;/code&gt; action (like saving it to the database) but based on a &lt;code class="prettyprint"&gt;State&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;  &lt;li&gt;&lt;p&gt;the end result of &lt;code class="prettyprint"&gt;readLines&lt;/code&gt; has to be &lt;code class="prettyprint"&gt;IO[Seq[A]]&lt;/code&gt; which is an action returning the sequence of all the return values when all the &lt;code class="prettyprint"&gt;IO&lt;/code&gt; actions have been performed&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Considering all that, I ended up with the following signatures:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  /**&lt;br /&gt;   * @param  filePath the file to read from&lt;br /&gt;   * @f      a function doing an IO action for each line, based on a State&lt;br /&gt;   * @init   the initial value of the State&lt;br /&gt;   * @return an IO action reading all the lines and returning the sequence of computed values&lt;br /&gt;   */&lt;br /&gt;   def readLinesIO[S, A](filePath: String)(f: String =&amp;gt; State[S, IO[A]])(init: S): IO[Seq[A]]&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;and the &lt;code class="prettyprint"&gt;Extractor&lt;/code&gt; code goes:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  // the initial state&lt;br /&gt;  val counter = new LogarithmicCounter(level = 100, scale = 10)&lt;br /&gt;&lt;br /&gt;  // a function to notify the user when we've reached a given level&lt;br /&gt;  val onLevel            = (i: Int) =&amp;gt; printTime(&amp;quot;imported from &amp;quot;+filePath+&amp;quot;: &amp;quot;+i+&amp;quot; lines&amp;quot;)&lt;br /&gt;  // a function to create and store each line&lt;br /&gt;  def onCount(l: String) = (i: Int) =&amp;gt; createAndStoreRecord(Line(file, l), creator)&lt;br /&gt;&lt;br /&gt;  // for each line, apply the actions described as a State[LogarithmicCounter, IO[A]] object&lt;br /&gt;  readLinesIO(file.getPath)(l =&amp;gt; counter.asState(onLevel, onCount(l)))(counter)&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;I like that interface because it keeps things separated and generic enough:&lt;/p&gt;&lt;ul&gt;  &lt;li&gt;reading the lines and opening/closing the resources goes to one class: &lt;code class="prettyprint"&gt;Source&lt;/code&gt;&lt;/li&gt;  &lt;li&gt;defining how the state evolves when an action is done goes to the &lt;code class="prettyprint"&gt;LogarithmicCounter&lt;/code&gt; class&lt;/li&gt;  &lt;li&gt;defining what to do exactly with each line goes in one class, the &lt;code class="prettyprint"&gt;Extractor&lt;/code&gt; class&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Now, the tricky implementation question is: how do you implement &lt;code class="prettyprint"&gt;readLinesIO&lt;/code&gt;?&lt;/p&gt;&lt;a name="What+not+to+do"&gt;&lt;/a&gt;&lt;h4&gt;What not to do&lt;/h4&gt;&lt;p&gt;Definitely the wrong way to do it is to use a &amp;quot;regular&amp;quot; &lt;code class="prettyprint"&gt;traverse&lt;/code&gt; on the stream of input lines. Even with the trampolining trick described in my previous post.&lt;/p&gt;&lt;p&gt;Indeed &lt;code class="prettyprint"&gt;traverse&lt;/code&gt; is going to fold everything once to go from &lt;code class="prettyprint"&gt;Stream[String]&lt;/code&gt; to &lt;code class="prettyprint"&gt;State[S, Seq[IO[B]]]&lt;/code&gt; then, with the initial &lt;code class="prettyprint"&gt;State&lt;/code&gt; value, to &lt;code class="prettyprint"&gt;Seq[IO[B]]&lt;/code&gt; which then has to be &lt;code class="prettyprint"&gt;sequence&lt;/code&gt;d into &lt;code class="prettyprint"&gt;IO[Seq[B]]&lt;/code&gt;. This is guaranteed to do more work than necessary. More than what a single &lt;code class="prettyprint"&gt;for&lt;/code&gt; loop would do.&lt;/p&gt;&lt;p&gt;The other thing &lt;em&gt;not to do&lt;/em&gt; is so silly that I'm ashamed to write it here. Ok, I'll write it. At least to avoid someone else the same idea. I had an implicit conversion from &lt;code class="prettyprint"&gt;A&lt;/code&gt; to &lt;code class="prettyprint"&gt;IO[A]&lt;/code&gt;,...&lt;/p&gt;&lt;p&gt;That's a very tempting thing to do and very convenient at first glance. Whenever a function was expecting an &lt;code class="prettyprint"&gt;IO[A]&lt;/code&gt;, I could just pass &lt;code class="prettyprint"&gt;a&lt;/code&gt; without having to write &lt;code class="prettyprint"&gt;a.pure[IO]&lt;/code&gt;. The trouble is, some actions were never executed! I don't have a specific example to show, but I'm pretty sure that, at some point, I had values of type &lt;code class="prettyprint"&gt;IO[IO[A]]&lt;/code&gt;. In that case, doing &lt;code class="prettyprint"&gt;unsafePerformIO&lt;/code&gt;, i.e. &amp;quot;executing&amp;quot; the &lt;code class="prettyprint"&gt;IO&lt;/code&gt; action only returns &lt;em&gt;another&lt;/em&gt; action to execute and doesn't do anything really!&lt;/p&gt;&lt;a name="Learn+your+FP+classes"&gt;&lt;/a&gt;&lt;h4&gt;Learn your FP classes&lt;/h4&gt;&lt;p&gt;I must admit I've been reluctant to go into what was advised on the Scalaz mailing-list: &amp;quot;go with StreamT&amp;quot;, &amp;quot;use Iteratees&amp;quot;. Really? I just want to do &lt;code class="prettyprint"&gt;IO&lt;/code&gt;! Can I learn all that stuff &lt;em&gt;later&lt;/em&gt;? I finally decided to go with the first option and describe it here in detail.&lt;/p&gt;&lt;p&gt;&lt;code class="prettyprint"&gt;StreamT&lt;/code&gt; is a &amp;quot;Stream transformer&amp;quot;. It is the Scala version of the &lt;a href="http://www.haskell.org/haskellwiki/ListT_done_right"&gt;&lt;code class="prettyprint"&gt;ListT&lt;/code&gt; done right&lt;/a&gt; in Haskell. That looks a bit scary at first but it is not so hard. It is an infinite list of elements which are not values but computations. So it more or less has the type &lt;code class="prettyprint"&gt;Seq[M[A]]&lt;/code&gt; where &lt;code class="prettyprint"&gt;M&lt;/code&gt; is some kind of effect, like changing a &lt;code class="prettyprint"&gt;State&lt;/code&gt; or doing &lt;code class="prettyprint"&gt;IO&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;My first practical question was: &amp;quot;how do I even build that thing from a Stream of elements?&amp;quot;&lt;/p&gt;&lt;a name="Unfolding+a+data+structure"&gt;&lt;/a&gt;&lt;h5&gt;Unfolding a data structure&lt;/h5&gt;&lt;p&gt;&lt;em&gt;Unfolding&lt;/em&gt; is the way. In the Scala world we are used to &amp;quot;folding&amp;quot; data structures into elements: get the sum of the elements of a list, get the maximum element in a tree. We less often do the opposite: generate a data structure from one element and a function. One example of this is generating the list of Fibonacci numbers out of an initial pair &lt;code class="prettyprint"&gt;(0, 1)&lt;/code&gt; and a function computing the next &amp;quot;Fibonacci step&amp;quot;.&lt;/p&gt;&lt;p&gt;With the &lt;code class="prettyprint"&gt;StreamT&lt;/code&gt; class in Scalaz, you do this:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  /** our record-creation function */&lt;br /&gt;  def f[B]: String =&amp;gt; M[B]&lt;br /&gt;&lt;br /&gt;  /**&lt;br /&gt;   * unfolding the initial Stream to a StreamT where we apply&lt;br /&gt;   * a `State` function `f` to each element&lt;br /&gt;   */&lt;br /&gt;  StreamT.unfoldM[M, B, Seq[A]](seq: Seq[A]) { (ss: Seq[A]) =&amp;gt;&lt;br /&gt;    if (ss.isEmpty)  (None: Option[(B, Seq[A])]).pure[M]&lt;br /&gt;    else              f(ss.head).map((b: B) =&amp;gt; Some((b, ss.tail)))&lt;br /&gt;  }&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The code above takes an initial sequence &lt;code class="prettyprint"&gt;seq&lt;/code&gt; (the Stream of lines to read) and:&lt;/p&gt;&lt;ul&gt;  &lt;li&gt;&lt;p&gt;if there are no more elements, you return &lt;code class="prettyprint"&gt;None.pure[M]&lt;/code&gt;, there's nothing left to do. In my specific use case that'll be &lt;code class="prettyprint"&gt;State[LogarithmicCounter, None]&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;  &lt;li&gt;&lt;p&gt;if there is an element in the &lt;code class="prettyprint"&gt;Stream&lt;/code&gt;, &lt;code class="prettyprint"&gt;f&lt;/code&gt; is applied to that element, that is, we create a record from the line and store it in the database (that's the &lt;code class="prettyprint"&gt;b:B&lt;/code&gt; parameter, which is going to be an &lt;code class="prettyprint"&gt;IO&lt;/code&gt; action)&lt;/p&gt;&lt;/li&gt;  &lt;li&gt;&lt;p&gt;because &lt;code class="prettyprint"&gt;M&lt;/code&gt; is the &lt;code class="prettyprint"&gt;State&lt;/code&gt; monad, when we apply &lt;code class="prettyprint"&gt;f&lt;/code&gt;, this also computes the next state to keep track of how many rows we've imported so far&lt;/p&gt;&lt;/li&gt;  &lt;li&gt;&lt;p&gt;then the rest of the stream of lines to process, &lt;code class="prettyprint"&gt;ss.tail&lt;/code&gt;, is also returned and &lt;code class="prettyprint"&gt;unfoldM&lt;/code&gt; will use that to compute the next &amp;quot;unfolding&amp;quot; step&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;One very important thing to notice here is that we haven't consumed any element at that stage. We've only declared what we &lt;em&gt;plan&lt;/em&gt; to do with each element of the input stream.&lt;/p&gt;&lt;a name="Running+the+StreamT"&gt;&lt;/a&gt;&lt;h5&gt;Running the StreamT&lt;/h5&gt;&lt;p&gt;In the &lt;code class="prettyprint"&gt;StreamT&lt;/code&gt; object in Scalaz there is this method call &lt;code class="prettyprint"&gt;runStreamT&lt;/code&gt; taking in:&lt;/p&gt;&lt;ul&gt;  &lt;li&gt;a &lt;code class="prettyprint"&gt;StreamT[State[S, *], A]&lt;/code&gt;, so that's a &lt;code class="prettyprint"&gt;StreamT&lt;/code&gt; where the state may change with each element of the &lt;code class="prettyprint"&gt;Stream&lt;/code&gt;&lt;/li&gt;  &lt;li&gt;an initial state &lt;code class="prettyprint"&gt;s0&lt;/code&gt;&lt;/li&gt;  &lt;li&gt;and returning a &lt;code class="prettyprint"&gt;StreamT[Id, A]&lt;/code&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;em&gt;Note: &lt;code class="prettyprint"&gt;State[S, *]&lt;/code&gt; is a non-existing notation for &lt;code class="prettyprint"&gt;({type l[a]=State[S, a]})#l&lt;/code&gt;&lt;/em&gt;&lt;/p&gt;&lt;p&gt;That doesn't see to be game-changing, but this does a useful thing for us. &lt;code class="prettyprint"&gt;runStreamT&lt;/code&gt; &amp;quot;threads&amp;quot; the &lt;code class="prettyprint"&gt;State&lt;/code&gt; through all elements, starting with the initial value. In our case that means that we're going to create IO actions, where each created action depends on the current state (the number of lines read so far).&lt;/p&gt;&lt;p&gt;The cool thing is that so far we've described transformations to apply: &lt;code class="prettyprint"&gt;Stream[String]&lt;/code&gt; to &lt;code class="prettyprint"&gt;StreamT[State, IO[B]]&lt;/code&gt; to &lt;code class="prettyprint"&gt;StreamT[Id, IO[B]]&lt;/code&gt; but we still haven't executed anything!&lt;/p&gt;&lt;a name="UnsafePerformIO+at+last"&gt;&lt;/a&gt;&lt;h5&gt;UnsafePerformIO at last&lt;/h5&gt;&lt;p&gt;Then, I coded an &lt;code class="prettyprint"&gt;unsafePerformIO&lt;/code&gt; method specialized for &lt;code class="prettyprint"&gt;Stream[Id, IO[A]]&lt;/code&gt; to execute all the &lt;code class="prettyprint"&gt;IO&lt;/code&gt; actions. The method itself is not difficult to write but we need to make sure it's tail-recursive:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  /** execute all the IO actions on a StreamT when it only contains IO actions */&lt;br /&gt;  def unsafePerformIO: Seq[A] = toSeq(streamT, Seq[A]())&lt;br /&gt;&lt;br /&gt;  /**&lt;br /&gt;   * execute a StreamT containing `IO` actions.&lt;br /&gt;   *&lt;br /&gt;   * Each IO action is executed and the result goes into a list of results&lt;br /&gt;   */&lt;br /&gt;  @tailrec&lt;br /&gt;  private def toSeq[A](streamT: StreamT[Id, IO[A]], result: Seq[A]): Seq[A] =&lt;br /&gt;    streamT.uncons match {&lt;br /&gt;      case Some((head, tail)) =&amp;gt; toSeq(tail, result :+ head.unsafePerformIO)&lt;br /&gt;      case None               =&amp;gt; result&lt;br /&gt;    }&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;a name="Hidding+everything+under+the+rug"&gt;&lt;/a&gt;&lt;h5&gt;Hidding everything under the rug&lt;/h5&gt;&lt;p&gt;Let's assemble the pieces of the puzzle now. With an implicit conversion, it is possible to transform any &lt;code class="prettyprint"&gt;Seq&lt;/code&gt; to a &lt;code class="prettyprint"&gt;StreamT&lt;/code&gt; performing some action on its elements:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;   /**&lt;br /&gt;    * for this to work, M[_] has to be a &amp;quot;PointedFunctor&amp;quot;, i.e. have &amp;quot;pure&amp;quot; and&lt;br /&gt;    * &amp;quot;fmap&amp;quot; methods. It is implemented using the `unfoldM` method code seen above&lt;br /&gt;    */&lt;br /&gt;   seq.toStreamT(f: A =&amp;gt; M[B]): StreamT[M, B]&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Then, we can traverse a whole sequence with a composition of a &lt;code class="prettyprint"&gt;State&lt;/code&gt; and &lt;code class="prettyprint"&gt;IO&lt;/code&gt; actions:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;   seq.traverseStateIO(f)&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Where &lt;code class="prettyprint"&gt;traverseStateIO&lt;/code&gt; is using the &lt;code class="prettyprint"&gt;toStreamT&lt;/code&gt; transformation and is defined as:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  /**&lt;br /&gt;   * traverse a stream with a State and IO actions by using a Stream transformer&lt;br /&gt;   */&lt;br /&gt;  def traverseStateIO[S, B](f: A =&amp;gt; State[S, IO[B]])(init: S): Seq[B] =&lt;br /&gt;    StreamT.runStreamT(seq.toStreamT[State[S, *], IO[B]](f), init).unsafePerformIO&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;em&gt;[you'll get bonus points for anyone providing a &lt;code class="prettyprint"&gt;Traverse[Stream]&lt;/code&gt; instance based on &lt;code class="prettyprint"&gt;StreamT&lt;/code&gt; - if that exists]&lt;/em&gt;&lt;/p&gt;&lt;p&gt;Finally the &lt;code class="prettyprint"&gt;Source.readLines&lt;/code&gt; method is implemented as:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  /**&lt;br /&gt;   * this method reads the lines of a file and apply stateful actions.&lt;br /&gt;   * @return an IO action doing all the reading and return a value for each processed line&lt;br /&gt;   */&lt;br /&gt;  def readLinesIO[S, A](path: String)(f: String =&amp;gt; State[S, IO[A]])(init: S): IO[Seq[A]] =&lt;br /&gt;    readLines(path)(f)(init).pure[IO]&lt;br /&gt;&lt;br /&gt;  private&lt;br /&gt;  def readLines[S, A](path: String)(f: String =&amp;gt; State[S, IO[A]])(init: S): Seq[A] = {&lt;br /&gt;    // store the opened resource&lt;br /&gt;    var source: Option[scala.io.Source] = None&lt;br /&gt;    def getSourceLines(src: scala.io.Source) = { source = Some(src); getLines(src) }&lt;br /&gt;&lt;br /&gt;    try {&lt;br /&gt;      // read the lines and execute the actions&lt;br /&gt;      getSourceLines(fromFile(path)).toSeq.traverseStateIO(f)(init)&lt;br /&gt;    } finally { sources.map(_.close()) }&lt;br /&gt;  }&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;And the client code, the &lt;code class="prettyprint"&gt;Extractor&lt;/code&gt; class does:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  val counter = new LogarithmicCounter(level = 100, scale = 10)&lt;br /&gt;&lt;br /&gt;  // notify the user when we've reached a given level&lt;br /&gt;  val onLevel            = (i: Int) =&amp;gt; printTime(&amp;quot;imported from &amp;quot;+filePath+&amp;quot;: &amp;quot;+i+&amp;quot; lines&amp;quot;)&lt;br /&gt;  // create and store a record for each new line&lt;br /&gt;  def onCount(l: String) = (i: Int) =&amp;gt; createAndStoreRecord(Line(file, l), creator)&lt;br /&gt;&lt;br /&gt;  // an `IO` action doing the extraction&lt;br /&gt;  readLinesIO(file.getPath)(l =&amp;gt; counter.asState(onLevel, onCount(l)))(counter)&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;a name="Conclusion"&gt;&lt;/a&gt;&lt;h4&gt;Conclusion&lt;/h4&gt;&lt;p&gt;I've had more than one WTF moment when trying to mix-in &lt;code class="prettyprint"&gt;State&lt;/code&gt;, &lt;code class="prettyprint"&gt;IO&lt;/code&gt; and resources management together. I've been very tempted to go back to vars and unrestricted effects :-).&lt;/p&gt;&lt;p&gt;Yet, I got to learn useful abstractions like &lt;code class="prettyprint"&gt;StreamT&lt;/code&gt; and I've seen that it was indeed possible to define generic, composable and functional interfaces between components. I also got the impression that there lots of different situations where we are manually passing state around which is error-prone and which can be done generically by &lt;code class="prettyprint"&gt;traverse&lt;/code&gt;-like methods.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5336273-202161769885031536?l=etorreborre.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://etorreborre.blogspot.com/feeds/202161769885031536/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5336273&amp;postID=202161769885031536' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5336273/posts/default/202161769885031536'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5336273/posts/default/202161769885031536'/><link rel='alternate' type='text/html' href='http://etorreborre.blogspot.com/2011/12/pragmatic-io-part-2.html' title='Pragmatic IO - part 2'/><author><name>Eric</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://farm3.static.flickr.com/2203/2118752971_7becaf7476_t.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5336273.post-8317162155128232655</id><published>2011-12-05T15:51:00.000+09:00</published><updated>2011-12-05T15:51:50.353+09:00</updated><title type='text'>Pragmatic IO</title><content type='html'>&lt;p&gt;This post is my exploration of the use of the &lt;code class="prettyprint"&gt;IO&lt;/code&gt; type in a small Scala application.&lt;/p&gt;&lt;a name="A+short+IO+introduction"&gt;&lt;/a&gt;&lt;h4&gt;A short &lt;code class="prettyprint"&gt;IO&lt;/code&gt; introduction&lt;/h4&gt;&lt;p&gt;Scala is a pragmatic language when you start learning Functional Programming (FP). Indeed there are some times when you can't apply the proper FP techniques just because &lt;em&gt;you don't know them yet&lt;/em&gt;. In those cases you can still resort to variables and side-effects to your heart's content. One of these situations is IO (input/output).&lt;/p&gt;&lt;p&gt;Let's take an example: you need to save data in a database (not a rare thing :-)). The signature of your method will certainly look like that:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  /** @return the unique database id of the saved user */&lt;br /&gt;  def save(user: User): Int&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This method is not &lt;a href="http://en.wikipedia.org/wiki/Referential_transparency_(computer_science)"&gt;referentially transparent&lt;/a&gt; since it changes the state of the &amp;quot;outside world&amp;quot;: if you call it twice, you'll get a different result. This kind of thing is very troubling for a Functional Programmer, that doesn't make him sleep well at night. And anyway, in a language like Haskell, where each function is pure you &lt;em&gt;can't&lt;/em&gt; implement it. So how do you do?&lt;/p&gt;&lt;p&gt;The &lt;a href="https://groups.google.com/d/msg/scala-debate/xYlUlQAnkmE/hbnx7L_cTDYJ"&gt;neat trick&lt;/a&gt; is to return an action saying &lt;em&gt;&amp;quot;I'm going to save the user&amp;quot;&lt;/em&gt; instead of actually saving it:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  def save(user: User): IO[Int]&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;At first, it seems difficult to do anything from there because we just have an &lt;code class="prettyprint"&gt;IO[Int]&lt;/code&gt;, not a real &lt;code class="prettyprint"&gt;Int&lt;/code&gt;. If we want to display the result on the screen, what do we do?&lt;/p&gt;&lt;p&gt;We use &lt;code class="prettyprint"&gt;IO&lt;/code&gt; as a &lt;code class="prettyprint"&gt;Monad&lt;/code&gt;, with the &lt;code class="prettyprint"&gt;flatMap&lt;/code&gt; operation, to sequence the 2 actions, into a new one:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  /** create an IO action to print a line on the screen */&lt;br /&gt;  def printLine(line: Any): IO[Unit] = println(line).pure[IO]&lt;br /&gt;&lt;br /&gt;  /** save a user and print the new id on the screen */&lt;br /&gt;                                           // or save(user) &amp;gt;&amp;gt;= printLine&lt;br /&gt;  def saveAndPrint(user: User): IO[Unit] = save(user).flatMap(id =&amp;gt; printLine(id))&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Then finally when we want to execute the actions for real, we call &lt;code class="prettyprint"&gt;unsafePerformIO&lt;/code&gt; in the &lt;code class="prettyprint"&gt;main&lt;/code&gt; method:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  def main(args: Array[String]) {&lt;br /&gt;    saveAndPrint(User(&amp;quot;Eric&amp;quot;)).unsafePerformIO&lt;br /&gt;  }&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;That's more or less the Haskell way to do IO in Scala but &lt;em&gt;nothing forces us to do so&lt;/em&gt;. The natural question which arises is: what &lt;em&gt;should&lt;/em&gt; we do?&lt;/p&gt;&lt;a name="IO+or+not+IO"&gt;&lt;/a&gt;&lt;h4&gt;&lt;code class="prettyprint"&gt;IO&lt;/code&gt; or not &lt;code class="prettyprint"&gt;IO&lt;/code&gt;&lt;/h4&gt;&lt;p&gt;Before starting this experiment, I fully re-read &lt;a href="https://groups.google.com/d/topic/scala-debate/xYlUlQAnkmE/discussion"&gt;this thread&lt;/a&gt; on the Scala mailing-list, and the answer is not clear for many people apparently. Martin Odersky doesn't seemed convinced that this is the way to go. He thinks that there should be &lt;a href="https://groups.google.com/d/msg/scala-debate/xYlUlQAnkmE/hbnx7L_cTDYJ"&gt;a more lightweight and polymorphic way to get effect checking&lt;/a&gt; and Bob Harper &lt;a href="http://existentialtype.wordpress.com/2011/05/01/of-course-ml-has-monads/"&gt;doesn't see the point&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;Surely the best way to form my own judgement was to try it out by myself (&lt;a href="https://groups.google.com/d/msg/scala-debate/xYlUlQAnkmE/b4BkUz4nKTIJ"&gt;as encouraged by Runar&lt;/a&gt;). One thing I know for sure is that, &lt;a href="https://github.com/etorreborre/specs2/blob/1.6/notes/1.6.markdown"&gt;the moment I started printing out files in specs2&lt;/a&gt;, I got an uneasy feeling that something could/would go wrong. Since then,&lt;br /&gt;&lt;code class="prettyprint"&gt;IO&lt;/code&gt; has been on my radar of things to try out.&lt;/p&gt;&lt;p&gt;Luckily, these days, I'm developing an application which is just the perfect candidate for that. This application:&lt;/p&gt;&lt;ul&gt;  &lt;li&gt;reads log files&lt;/li&gt;  &lt;li&gt;stores the records in a MongoDB database&lt;/li&gt;  &lt;li&gt;creates graphs to analyze the data (maximums, average, distribution of some variables)&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Perfect but,... where do I start :-)?&lt;/p&gt;&lt;a name="Theory+%3D%3E+Practice"&gt;&lt;/a&gt;&lt;h4&gt;Theory =&amp;gt; Practice&lt;/h4&gt;&lt;p&gt;That was indeed my first question. Even in a small Swing application like mine the effects were interleaved in many places:&lt;/p&gt;&lt;ul&gt;  &lt;li&gt;when I opened the MongoDB connection, I was printing some messages on the application console to inform the user about the db name, the MongoDB version,...&lt;/li&gt;  &lt;li&gt;the class responsible for creating the reports was fetching its own data, effectively doing IO&lt;/li&gt;  &lt;li&gt;datastore queries are cached, and getting some records (an IO action) required to get other data (the date of the latest import, another IO action) to decide if we could reuse the cached ones instead&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;The other obvious question was: &amp;quot;where do I call &lt;a href="http://users.skynet.be/jyp/html/base/System-IO-Unsafe.html"&gt;&lt;code class="prettyprint"&gt;unsafePerformIO&lt;/code&gt;&lt;/a&gt;?&amp;quot;. This in an interactive application, I can't put just one&lt;br /&gt;&lt;code class="prettyprint"&gt;unsafePerformIO&lt;/code&gt; in the main method!&lt;/p&gt;&lt;p&gt;The last question was: &amp;quot;OMG, I started to put IO in a few places, now it's eating all of my application, what can I do?&amp;quot; :-))&lt;/p&gt;&lt;a name="Segregating+the+effects"&gt;&lt;/a&gt;&lt;h4&gt;Segregating the effects&lt;/h4&gt;&lt;p&gt;Here's what I ended up doing. First of all let's draw a diagram of the main components of my application before refactoring:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;   +-------+    extract/getReport       +------ IO +   store      +------ IO +&lt;br /&gt;   + Gui   + &amp;lt;------------------------&amp;gt; + Process  + -----------&amp;gt; + Store    +&lt;br /&gt;   +-------+     Query/LogReport        +----------+              +----------+&lt;br /&gt;                                             | getReport               ^&lt;br /&gt;                                             v                         |&lt;br /&gt;                                        +------ IO +     getRecords    |&lt;br /&gt;                                        + Reports  + ------------------+&lt;br /&gt;                                        +----------+&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Simple app, I told you.&lt;/p&gt;&lt;p&gt;The first issue was that the &lt;code class="prettyprint"&gt;Reports&lt;/code&gt; class (actually a bit more than just one class) was fetching records and building reports at the same time. So if I decided to put &lt;code class="prettyprint"&gt;IO&lt;/code&gt; types on the store I would drag them in any subsequent transformation (marked as &lt;code class="prettyprint"&gt;IO&lt;/code&gt; on the diagram).&lt;/p&gt;&lt;p&gt;The next issue was that the &lt;code class="prettyprint"&gt;Process&lt;/code&gt; class was also doing too much: reading files and storing records.&lt;/p&gt;&lt;p&gt;Those 2 things led me to this refactoring and &amp;quot;architectural&amp;quot; decision, &lt;code class="prettyprint"&gt;IO&lt;/code&gt; annotations would only happen on the middle layer:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;                                        +------- IO +            store&lt;br /&gt;                               read     + Extractor + -----------------------------+&lt;br /&gt;                               files    +-----------+          |                   |&lt;br /&gt;                                             ^                 |                   |&lt;br /&gt;                                             | extract         v                   v&lt;br /&gt;   +-------+    extract/getReport       +----------+      +------ IO +        +----------+&lt;br /&gt;   + Gui   + &amp;lt;------------------------&amp;gt; + Process  +      + Status   +        + Store    +&lt;br /&gt;   +-------+     Query/LogReport        +----------+      +----------+        +----------+&lt;br /&gt;                                             | getReport       ^                   ^&lt;br /&gt;                                             v                 |                   |&lt;br /&gt;                                        +------ IO +           |    getRecords     |&lt;br /&gt;                                        + Reporter + ------------------------------+&lt;br /&gt;                                        +----------+&lt;br /&gt;                                             | createReport&lt;br /&gt;                                             v&lt;br /&gt;                                        +----------+&lt;br /&gt;                                        + Reports  +&lt;br /&gt;                                        +----------+&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;ul&gt;  &lt;li&gt;&lt;p&gt;all the &lt;code class="prettyprint"&gt;IO&lt;/code&gt; actions are being segregrated to special classes. For example extracting lines for log files creates a big composite &lt;code class="prettyprint"&gt;IO&lt;/code&gt; action to read the lines, to store the lines as a record and to report the status of the extraction. This is handled by the &lt;code class="prettyprint"&gt;Extractor&lt;/code&gt; class&lt;/p&gt;&lt;/li&gt;  &lt;li&gt;&lt;p&gt;the &lt;code class="prettyprint"&gt;Status&lt;/code&gt; class handles all the printing on the the console. It provides methods like:&lt;/p&gt;  &lt;pre&gt;&lt;code class="prettyprint"&gt;  /**&lt;br /&gt;   * print a message with a timestamp, execute an action,&lt;br /&gt;   * and print an end message with a timestamp also.&lt;br /&gt;   *&lt;br /&gt;   * This method used by the `Extractor`, `Reporter` and `Process` classes.&lt;br /&gt;   */&lt;br /&gt;   def statusIO[T](before: String, action: IO[T], after: String): IO[T]`&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;  &lt;li&gt;&lt;p&gt;the &lt;code class="prettyprint"&gt;Reports&lt;/code&gt; class only deals with the aggregation of data from records coming from the &lt;code class="prettyprint"&gt;Store&lt;/code&gt;. However it is completely ignorant of this fact, so testing becomes easier (true story, that really helped!)&lt;/p&gt;&lt;/li&gt;  &lt;li&gt;&lt;p&gt;the &lt;code class="prettyprint"&gt;Store&lt;/code&gt; does not have any &lt;code class="prettyprint"&gt;IO&lt;/code&gt; type. In that sense my approach is not very pure, since nothing prevents &amp;quot;someone&amp;quot; from directly calling the store from the middle layer without encapsulating the call in an &lt;code class="prettyprint"&gt;IO&lt;/code&gt; action. I might argue that this is pragmatic. Instead of sprinkling &lt;code class="prettyprint"&gt;IO&lt;/code&gt; everywhere I started by defining a layer isolating the &lt;code class="prettyprint"&gt;IO&lt;/code&gt; world (the data store, the file system) from the non-&lt;code class="prettyprint"&gt;IO&lt;/code&gt; world (the reports, the GUI. Then, if I want, I can extend the &lt;code class="prettyprint"&gt;IO&lt;/code&gt; marking the rest of the application if I want to. That being said I didn't do it because I didn't really see the added-value at that stage. A name for this approach could be &amp;quot;gradual effecting&amp;quot; (the equivalent of &lt;a href="http://lambda-the-ultimate.org/node/1707"&gt;&amp;quot;gradual typing&amp;quot;&lt;/a&gt;)&lt;/p&gt;&lt;/li&gt;  &lt;li&gt;&lt;p&gt;the &lt;code class="prettyprint"&gt;Process&lt;/code&gt; class is not marked as &lt;code class="prettyprint"&gt;IO&lt;/code&gt; because all the methods provided by that class are actually calling&lt;br /&gt; &lt;code class="prettyprint"&gt;unsafePerformIO&lt;/code&gt; to really execute the actions. This is the only place in the application where this kind of call occurs and the GUI layer could be left unchanged&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;All of that was not too hard, but was not a walk in the park either. Some details of the implementation were hard to come up with.&lt;/p&gt;&lt;a name="Under+the+bonnet%3A+syntactic+tricks"&gt;&lt;/a&gt;&lt;h4&gt;Under the bonnet: syntactic tricks&lt;/h4&gt;&lt;p&gt;First of all, what was easy?&lt;/p&gt;&lt;a name="%22Monadic%22+sequencing"&gt;&lt;/a&gt;&lt;h6&gt;&amp;quot;Monadic&amp;quot; sequencing&lt;/h6&gt;&lt;p&gt;Sequencing &lt;code class="prettyprint"&gt;IO&lt;/code&gt; actions is indeed easy. I've used the &lt;code class="prettyprint"&gt;for&lt;/code&gt; comprehension:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  def statusIO[T](before: String, action: IO[T], after: String): IO[T] = {&lt;br /&gt;    for {&lt;br /&gt;      _      &amp;lt;- printTime(before)&lt;br /&gt;      result &amp;lt;- action&lt;br /&gt;      _      &amp;lt;- printTime(after)&lt;br /&gt;    } yield result&lt;br /&gt;  }&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;a name="%22Semi-column%22+sequencing"&gt;&lt;/a&gt;&lt;h6&gt;&amp;quot;Semi-column&amp;quot; sequencing&lt;/h6&gt;&lt;p&gt;But also the &lt;code class="prettyprint"&gt;&amp;gt;&amp;gt;=|&lt;/code&gt; operator in Scalaz to just sequence 2 actions when you don't care about the first one:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  def statusIO[T](before: String)(action: IO[T]): IO[T] = printTime(before) &amp;gt;&amp;gt;=| action&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;a name="%22Conditional%22+sequencing"&gt;&lt;/a&gt;&lt;h6&gt;&amp;quot;Conditional&amp;quot; sequencing&lt;/h6&gt;&lt;p&gt;And, for the fun of it, I implemented a &amp;quot;conditional flatMap operator&amp;quot;, &lt;code class="prettyprint"&gt;&amp;gt;&amp;gt;=?&lt;/code&gt;:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  action1 &amp;gt;&amp;gt;=? (condition, action2)&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;In the code above the &lt;code class="prettyprint"&gt;action1&lt;/code&gt; is executed and, if the &lt;code class="prettyprint"&gt;condition&lt;/code&gt; is true, we execute &lt;code class="prettyprint"&gt;action2&lt;/code&gt; and forget about the result of &lt;code class="prettyprint"&gt;action1&lt;/code&gt;, otherwise we keep the result of &lt;code class="prettyprint"&gt;action1&lt;/code&gt;.&lt;/p&gt;&lt;a name="%22if+else%22+with+a+twist"&gt;&lt;/a&gt;&lt;h6&gt;&amp;quot;if else&amp;quot; with a twist&lt;/h6&gt;&lt;p&gt;Not completely related to &lt;code class="prettyprint"&gt;IO&lt;/code&gt; I also liked the &lt;code class="prettyprint"&gt;??&lt;/code&gt; operator. Say I want to execute an action only if a file exists:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;   if (file.exists) createRecords(file)&lt;br /&gt;   else             ()&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The &lt;code class="prettyprint"&gt;else&lt;/code&gt; line really feels ugly. This kind of &amp;quot;default value&amp;quot; should be inferred from somewhere. Indeed! This &amp;quot;default value&amp;quot; is the &lt;code class="prettyprint"&gt;Zero&lt;/code&gt; of a given type in Scalaz, so it is possible to write:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;   file.exists ?? createRecords(file)&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;It just works (tm) but it's worth knowing where that zero values really comes from:&lt;/p&gt;&lt;ol&gt;  &lt;li&gt;&lt;p&gt;&lt;code class="prettyprint"&gt;createRecords(file)&lt;/code&gt; returns &lt;code class="prettyprint"&gt;IO[Int]&lt;/code&gt; (the last created id - that's a simplification of the real program)&lt;/p&gt;&lt;/li&gt;  &lt;li&gt;&lt;p&gt;there is a way to create a &lt;code class="prettyprint"&gt;Monoid&lt;/code&gt; from another &lt;code class="prettyprint"&gt;Monoid&lt;/code&gt; + an &lt;code class="prettyprint"&gt;Applicative&lt;/code&gt;:&lt;/p&gt;  &lt;pre&gt;&lt;code class="prettyprint"&gt;// from scalaz.Monoid&lt;br /&gt;/** A monoid for sequencing Applicative effects. */&lt;br /&gt;def liftMonoid[F[_], M](implicit m: Monoid[M], a: Applicative[F]): Monoid[F[M]] =&lt;br /&gt;  new Monoid[F[M]] {&lt;br /&gt;    val zero: F[M] = a.pure(m.zero)&lt;br /&gt;    def append(x: F[M], y: =&amp;gt; F[M]): F[M] =&lt;br /&gt;      a.liftA2(x, y, (m1: M, m2: M) =&amp;gt; m.append(m1, m2))&lt;br /&gt;  }&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;  &lt;li&gt;&lt;p&gt;in this case &lt;code class="prettyprint"&gt;IO&lt;/code&gt; has an &lt;code class="prettyprint"&gt;Applicative&lt;/code&gt;, &lt;code class="prettyprint"&gt;M&lt;/code&gt; is &lt;code class="prettyprint"&gt;Int&lt;/code&gt; so it has a &lt;code class="prettyprint"&gt;Monoid&lt;/code&gt; hence &lt;code class="prettyprint"&gt;IO[Int]&lt;/code&gt; defines a &lt;code class="prettyprint"&gt;Monoid&lt;/code&gt; where the zero is &lt;strong&gt;&lt;em&gt;&lt;code class="prettyprint"&gt;IO(0)&lt;/code&gt;&lt;/em&gt;&lt;/strong&gt;. I had to open up the debugger to check that precisely :-)&lt;/p&gt;&lt;/li&gt;&lt;/ol&gt;&lt;a name="Under+the+bonnet%3A+it+just+works%2C...+not"&gt;&lt;/a&gt;&lt;h4&gt;Under the bonnet: it just works,... not&lt;/h4&gt;&lt;p&gt;The next piece of implementation I was really happy with was the &amp;quot;logarithmic reporting&amp;quot;. This is a feature I implemented using vars at first, which I wanted to make pure in my quest for &lt;code class="prettyprint"&gt;IO&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;What I want is to extract log lines and notify the user when a bunch of lines have been imported (so that he doesn't get too bored). But I don't know how many lines there are in a given file. It could be 100 but it could be 30.000 or 100.000. So I thought that a &amp;quot;logarithmic&amp;quot; counter would be nice. With that counter, I notify the user every 100, 1000, 10.000, 100.000 lines.&lt;/p&gt;&lt;p&gt;The &lt;code class="prettyprint"&gt;LogarithmicCounter&lt;/code&gt; works by creating a &lt;code class="prettyprint"&gt;State&lt;/code&gt; object encapsulating 2 actions to do, one on each &lt;code class="prettyprint"&gt;tick&lt;/code&gt;, one when a&lt;br /&gt;&lt;code class="prettyprint"&gt;level&lt;/code&gt; is reached:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;    // create and store a line on each `tick`&lt;br /&gt;    def onCount(line: String) = (i: Int) =&amp;gt; createAndStoreRecord(line, creator)&lt;br /&gt;&lt;br /&gt;    // and notify the user when we've reached a given level&lt;br /&gt;    val onLevel = (i: Int) =&amp;gt; printTime(&amp;quot;imported from &amp;quot;+filePath+&amp;quot;: &amp;quot;+i+&amp;quot; lines&amp;quot;)&lt;br /&gt;&lt;br /&gt;    /** @return a State[LogarithmicCounter, IO[Unit]] */&lt;br /&gt;    val readLine = (line: String) =&amp;gt; counter.asState(onLevel, onCount(line))&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The &lt;code class="prettyprint"&gt;readLine&lt;/code&gt; method is used in a &lt;code class="prettyprint"&gt;traversal&lt;/code&gt; of the lines returned by a &lt;code class="prettyprint"&gt;FileReader&lt;/code&gt;:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;    (lines traverseState readLine): State[LogarithmicCounter, Stream[IO[Unit]]]&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Pass it an initial counter and you get back a &lt;code class="prettyprint"&gt;Stream&lt;/code&gt; of &lt;code class="prettyprint"&gt;IO&lt;/code&gt; actions which you can then &lt;code class="prettyprint"&gt;sequence&lt;/code&gt; to get an &lt;code class="prettyprint"&gt;IO[Stream[Unit]]&lt;/code&gt;:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;    // initial traversal with a function returning a `State`&lt;br /&gt;    val traversed: State[LogarithmicCounter, Stream[IO[Unit]]] = lines traverseState readLine&lt;br /&gt;&lt;br /&gt;    // feed in the initial values: count = 1, level = 100 to get the end `State`&lt;br /&gt;    val traversedEndState: Stream[IO[Unit]] = traversed ! new LogarithmicCounter&lt;br /&gt;&lt;br /&gt;    // finally get a global action which will execute a stream of&lt;br /&gt;    // record-storing actions and printing actions&lt;br /&gt;    val toStore: IO[Stream[Unit]] = traversedEndState.sequence&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Some readers of this blog may recognize one usage of the &lt;a href="http://etorreborre.blogspot.com/2011/06/essence-of-iterator-pattern.html"&gt;Essence of the Iterator Pattern - EIP&lt;/a&gt; and that's indeed what it is (my first real use case, yes!). &lt;code class="prettyprint"&gt;traverseState&lt;/code&gt; is just a way to use the more traditional &lt;code class="prettyprint"&gt;traverse&lt;/code&gt; method but hiding the ugly type annotations.&lt;/p&gt;&lt;p&gt;This kind of type-directed development is nice. You add some type here and there and you let the compiler guide you to which method to apply in order to get the desired results:&lt;/p&gt;&lt;ol&gt;  &lt;li&gt;after the traversal I get back a &lt;code class="prettyprint"&gt;State&lt;/code&gt;&lt;/li&gt;  &lt;li&gt;if I want to get the final state, the &lt;code class="prettyprint"&gt;Stream&lt;/code&gt; of &lt;code class="prettyprint"&gt;IO&lt;/code&gt;, I need to feed in some initial values, that's the '!' method&lt;/li&gt;  &lt;li&gt;if I want to get an action equivalent to the stream of actions, I need the &lt;code class="prettyprint"&gt;sequence&lt;/code&gt; method which has exactly the type signature doing what I want&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;I was about to call it a day when I actually tried my application,... StackOverflow! What?!?&lt;/p&gt;&lt;a name="Trampolining+to+the+rescue"&gt;&lt;/a&gt;&lt;h5&gt;Trampolining to the rescue&lt;/h5&gt;&lt;p&gt;It turns out that traversing a &amp;quot;big&amp;quot; sequence with a &lt;code class="prettyprint"&gt;State&lt;/code&gt; is not so easy. First of all, &lt;code class="prettyprint"&gt;State&lt;/code&gt; is an &lt;code class="prettyprint"&gt;Applicative&lt;/code&gt; because it is also a &lt;code class="prettyprint"&gt;Monad&lt;/code&gt; (please read the earlier EIP post for the details). So basically this amounts to chaining a &lt;em&gt;lot&lt;/em&gt; of &lt;code class="prettyprint"&gt;flatMap&lt;/code&gt; operations which blows up the stack.&lt;/p&gt;&lt;p&gt;Fortunately for me, Runar has implemented a generic solution for this kind of issue, like, &lt;a href="http://apocalisp.wordpress.com/2011/10/26/tail-call-elimination-in-scala-monads/"&gt;&lt;em&gt;less than 2 months ago!&lt;/em&gt;&lt;/a&gt; I leave you to his excellent post for a detailed explanation but the gist of it is to use continuations to describe computations and store them on the heap instead of letting calls happen on the stack. So instead of using &lt;code class="prettyprint"&gt;State[S, A]&lt;/code&gt; I use &lt;code class="prettyprint"&gt;StateT[Trampoline, S, A]&lt;/code&gt; where each computation &lt;code class="prettyprint"&gt;(S, A)&lt;/code&gt; returned by the &lt;code class="prettyprint"&gt;State&lt;/code&gt; monad is actually encapsulated in a &lt;code class="prettyprint"&gt;Trampoline&lt;/code&gt; to be executed on the heap.&lt;/p&gt;&lt;p&gt;The application of this idea was not too easy at first and Runar helped me with a code snippet (thanks!). Eventually I managed to keep everything well hidden behind the &lt;code class="prettyprint"&gt;traverseState&lt;/code&gt; function. The first thing I did was to &amp;quot;trampoline&amp;quot; the function passed to &lt;code class="prettyprint"&gt;traverseState&lt;/code&gt;:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  /**&lt;br /&gt;   * transform a function into its &amp;quot;trampolined&amp;quot; version&lt;br /&gt;   * val f:             T =&amp;gt; State[S, A]              = (t: T) =&amp;gt; State[S, A]&lt;br /&gt;   * val f_trampolined: T =&amp;gt; StateT[Trampoline, S, A] = f.trampolined&lt;br /&gt;   */&lt;br /&gt;  implicit def liftToTrampoline[T, S, A](f: T =&amp;gt; State[S, A]) = new LiftToTrampoline(f)&lt;br /&gt;&lt;br /&gt;  class LiftToTrampoline[T, S, A](f: T =&amp;gt; State[S, A]) {&lt;br /&gt;    def trampolined = (t: T) =&amp;gt; stateT((s: S) =&amp;gt; suspend(st.apply(s)))&lt;br /&gt;  }&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;So the &lt;code class="prettyprint"&gt;traverseState&lt;/code&gt; function definition becomes:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  // with the full ugly type annotation&lt;br /&gt;  def traverseState(f: T =&amp;gt; State[S, B]) =&lt;br /&gt;    seq.traverse[({type l[a]=StateT[Trampoline, S, a]})#l, B](f.trampolined)&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;However I can't leave things that like that because &lt;code class="prettyprint"&gt;traverseState&lt;/code&gt; then returns a &lt;code class="prettyprint"&gt;StateT[Trampoline, S, B]&lt;/code&gt; when the client of the function expects a &lt;code class="prettyprint"&gt;State[S, B]&lt;/code&gt;. So I added an &lt;code class="prettyprint"&gt;untrampolined&lt;/code&gt; method to recover a &lt;code class="prettyprint"&gt;State&lt;/code&gt; from a &amp;quot;trampolined&amp;quot; one:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  /** @return a normal State from a &amp;quot;trampolined&amp;quot; one */&lt;br /&gt;  implicit def fromTrampoline[S, A](st: StateT[Trampoline, S, A]) = new FromTrampoline(st)&lt;br /&gt;&lt;br /&gt;  class FromTrampoline[S, A](st: StateT[Trampoline, S, A]) {&lt;br /&gt;    def untrampolined: State[S, A] = state((s: S) =&amp;gt; st(s).run)&lt;br /&gt;  }&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The end result is not so bad. The &amp;quot;trampoline&amp;quot; trick is hidden as an implementation detail and I don't get StackOverflows anymore. Really? Not really,...&lt;/p&gt;&lt;a name="The+subtleties+of+foldRight+and+foldLeft"&gt;&lt;/a&gt;&lt;h5&gt;The subtleties of &lt;code class="prettyprint"&gt;foldRight&lt;/code&gt; and &lt;code class="prettyprint"&gt;foldLeft&lt;/code&gt;&lt;/h5&gt;&lt;p&gt;I was still getting a StackOverflow error but not in the same place as before (&lt;code class="prettyprint"&gt;#&amp;amp;$^@!!!&lt;/code&gt;). It was in the traversal function itself, not in the chaining of &lt;code class="prettyprint"&gt;flatMaps&lt;/code&gt;. The reason for that one was that the &lt;code class="prettyprint"&gt;Traverse&lt;/code&gt; instance for a &lt;code class="prettyprint"&gt;Stream&lt;/code&gt; in Scalaz is using &lt;code class="prettyprint"&gt;foldRight&lt;/code&gt; (or &lt;code class="prettyprint"&gt;foldr&lt;/code&gt;):&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  implicit def StreamTraverse: Traverse[Stream] = new Traverse[Stream] {&lt;br /&gt;    def traverse[F[_]: Applicative, A, B](f: A =&amp;gt; F[B], as: Stream[A]): F[Stream[B]] =&lt;br /&gt;      as.foldr[F[Stream[B]]](Stream.empty.pure) { (x, ys) =&amp;gt;&lt;br /&gt;        implicitly[Apply[F]].apply(f(x) map ((a: B) =&amp;gt; (b: Stream[B]) =&amp;gt; a #:: b), ys)&lt;br /&gt;      }&lt;br /&gt;  }&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;and &lt;code class="prettyprint"&gt;foldr&lt;/code&gt; is recursively intensive. It basically says: &lt;code class="prettyprint"&gt;foldRight(n) = f(n, foldRight(n-1))&lt;/code&gt; whereas &lt;code class="prettyprint"&gt;foldl&lt;/code&gt; is implemented with a for loop and a variable to accumulate the result.&lt;/p&gt;&lt;p&gt;The workaround for this situation is simple: just provide a &lt;code class="prettyprint"&gt;Traverse&lt;/code&gt; instance using &lt;code class="prettyprint"&gt;foldLeft&lt;/code&gt;. But then you can wonder: &amp;quot;why is &lt;code class="prettyprint"&gt;traverse&lt;/code&gt; even using &lt;code class="prettyprint"&gt;foldRight&lt;/code&gt; in the first place?&amp;quot;. The answer is in my next bug! After doing the modifications above I didn't get a SOE anymore but the output in the console was like:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  imported from test.log: 10000 lines [12:33:30]&lt;br /&gt;  imported from test.log: 1000 lines  [12:33:30]&lt;br /&gt;  imported from test.log: 100 lines   [12:33:30]&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Cool, I have invented a Time Machine, Marty! That one left me puzzled for a while but I found the solution if not the explanation. The &amp;quot;left-folding&amp;quot; &lt;code class="prettyprint"&gt;Traverse&lt;/code&gt; instance I had left in scope was being used by the &lt;code class="prettyprint"&gt;sequence&lt;/code&gt; method to transform a &lt;code class="prettyprint"&gt;Stream[IO]&lt;/code&gt; into an &lt;code class="prettyprint"&gt;IO[Stream]&lt;/code&gt;. Changing that to the standard &amp;quot;right-folding&amp;quot; behaviour for a &lt;code class="prettyprint"&gt;Stream&lt;/code&gt; traversal was ok. So there &lt;em&gt;is&lt;/em&gt; a difference (meaning that something is not &lt;a href="http://en.wikipedia.org/wiki/Associative_property"&gt;associative&lt;/a&gt; somewhere,...)&lt;/p&gt;&lt;a name="Conclusion"&gt;&lt;/a&gt;&lt;h4&gt;Conclusion&lt;/h4&gt;&lt;p&gt;The main conclusion from this experiment is that tagging methods with &lt;code class="prettyprint"&gt;IO&lt;/code&gt; made me really &lt;strong&gt;&lt;em&gt;think&lt;/em&gt;&lt;/strong&gt; about where are the effects of my application. It also encouraged functional programming techniques such as &lt;code class="prettyprint"&gt;traverse&lt;/code&gt;, &lt;code class="prettyprint"&gt;sequence&lt;/code&gt; and al.&lt;/p&gt;&lt;p&gt;I must however say that I was surprised on more than one account:&lt;/p&gt;&lt;ul&gt;  &lt;li&gt;&lt;p&gt;I stumbled upon a whole new class of bugs: non-execution. My effects were not executed improperly, they were not executed at all because I forgot to call &lt;code class="prettyprint"&gt;unsafePerformIO&lt;/code&gt;!&lt;/p&gt;&lt;/li&gt;  &lt;li&gt;&lt;p&gt;I was not expecting to be needing an optimisation which had just been added to Scalaz, for something which I thought was a casual traversal&lt;/p&gt;&lt;/li&gt;  &lt;li&gt;&lt;p&gt;there are still some FP mysteries to me. For example I don't know yet &lt;a href="https://groups.google.com/d/msg/scalaz/QPUs6TWTAm4/Srypc61Agg0J"&gt;how to traverse a full &lt;code class="prettyprint"&gt;Stream&lt;/code&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;  &lt;li&gt;&lt;p&gt;I also don't get why my messages were inverted on the console. I tried different experiments, with a &lt;code class="prettyprint"&gt;Seq&lt;/code&gt; instead of a &lt;code class="prettyprint"&gt;Stream&lt;/code&gt;, with &lt;code class="prettyprint"&gt;Identity&lt;/code&gt; or &lt;code class="prettyprint"&gt;Option&lt;/code&gt; as an &lt;code class="prettyprint"&gt;Applicative&lt;/code&gt; instead of &lt;code class="prettyprint"&gt;IO&lt;/code&gt; and I could only reproduce this specific behavior with &lt;code class="prettyprint"&gt;Stream&lt;/code&gt; and &lt;code class="prettyprint"&gt;IO&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Anyway, I would say that it was overall worth using the &lt;code class="prettyprint"&gt;IO&lt;/code&gt; type, at least for the architectural clarification and the better testing. It also brought back the &lt;a href="http://bit.ly/saCAyu"&gt;warm, fuzzy feeling&lt;/a&gt; that things are under control. On the other hand refactoring to &lt;code class="prettyprint"&gt;IO&lt;/code&gt; took me longer than expected and required more knowledge than just using vars and regular I/O. But that should really be accounted for in the 'investment for the future' column.&lt;/p&gt;&lt;a name="PS"&gt;&lt;/a&gt;&lt;h6&gt;PS&lt;/h6&gt;&lt;p&gt;There are certainly many stones left unturned in that post for programmers new to Functional Programming and Scalaz. I do apologize for that (this post is certainly long enough :-)) and I encourage those readers to ask questions on the &lt;a href="https://groups.google.com/forum/#!forum/scalaz"&gt;Scalaz mailing-list&lt;/a&gt;.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5336273-8317162155128232655?l=etorreborre.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://etorreborre.blogspot.com/feeds/8317162155128232655/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5336273&amp;postID=8317162155128232655' title='10 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5336273/posts/default/8317162155128232655'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5336273/posts/default/8317162155128232655'/><link rel='alternate' type='text/html' href='http://etorreborre.blogspot.com/2011/12/pragmatic-io.html' title='Pragmatic IO'/><author><name>Eric</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://farm3.static.flickr.com/2203/2118752971_7becaf7476_t.jpg'/></author><thr:total>10</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5336273.post-3573552439796353059</id><published>2011-11-11T21:49:00.000+09:00</published><updated>2011-11-11T22:54:16.555+09:00</updated><title type='text'>Practical uses for Unboxed Tagged Types</title><content type='html'>&lt;p&gt;Another blog post to show that the esoteric type system tricks you read about Haskell or Scala actually have real uses.&lt;/p&gt;&lt;a name="Groovy+vs+Scala+for+log+analysis"&gt;&lt;/a&gt;&lt;h4&gt;Groovy vs Scala for log analysis&lt;/h4&gt;&lt;p&gt;These days I'm implementing a non-critical application which deals with:&lt;/p&gt;&lt;ol&gt;  &lt;li&gt;importing performance log files&lt;/li&gt;  &lt;li&gt;parsing them and storing some structured data corresponding to each line&lt;/li&gt;  &lt;li&gt;creating some graphs to show the maximum execution times, the cumulated maximum execution times, the events inside a given time range, and so on,...&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;This, in itself, is a very interesting exercise for me since I had coded a similar application more than 3 years ago in Groovy. After deciding that the Groovy implementation was slow and cumbersome, I decided to give it a go with Scala (and MongoDB for the backend :-)).&lt;/p&gt;&lt;p&gt;I've been really amazed to see that many of the things I learnt for free, on my spare time, were applicable in the case of that application, to yield much better code. This post shows one of these techniques: &amp;quot;Unboxed Tagged Types&amp;quot;.&lt;/p&gt;&lt;a name="Unboxed+what%3F"&gt;&lt;/a&gt;&lt;h4&gt;Unboxed what?&lt;/h4&gt;&lt;p&gt;Two months ago, I saw this &lt;a href="http://twitter.com/#!/milessabin/status/116561125537099776"&gt;enigmatic tweet&lt;/a&gt; by @milessabin. I followed the link, read the code and thought: &amp;quot;oh nice, I see, at least Miles is having fun playing with Scala's type system&amp;quot;. But I wasn't really able to see what that thing could be used for.&lt;/p&gt;&lt;p&gt;Then, this week, developing my log analysis application, I became midly annoyed about one specific messy point.&lt;/p&gt;&lt;a name="There+is+time+and%2C...+time"&gt;&lt;/a&gt;&lt;h4&gt;There is time and,... time&lt;/h4&gt;&lt;p&gt;The log records I'm getting from the log files are all timestamped with a number of millis which are what Java's &lt;code class="prettyprint"&gt;Date.getTime&lt;/code&gt; returns when you ask for it. That is to say, the number of milliseconds, as a &lt;code class="prettyprint"&gt;Long&lt;/code&gt;, elapsed from &lt;code class="prettyprint"&gt;January, 1st, 1970, 00:00:00.000 GMT&lt;/code&gt; (the so-called &lt;code class="prettyprint"&gt;EPOCH&lt;/code&gt; time by people thinking that the world started in the seventies).&lt;/p&gt;&lt;p&gt;Not very user friendly. Usually you would like to display that as readable date, using the &lt;code class="prettyprint"&gt;java.text.SimpleDateFormat&lt;/code&gt; class for example. So in Scala, you are very tempted to write code like that:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  val hhmmFormat = new SimpleDateFormat(&amp;quot;hh:mm&amp;quot;)&lt;br /&gt;&lt;br /&gt;  implicit def toTimeDisplay(t: Long) = new TimeDisplay(t)&lt;br /&gt;&lt;br /&gt;  case class TimeDisplay(time: Long) {&lt;br /&gt;    def hhmm = hhmmFormat.format(new Date(time))&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  &amp;gt; val startTime: Long = 12398234093458L&lt;br /&gt;  &amp;gt; startTime.hhmm&lt;br /&gt;  res0: java.lang.String = 12:54&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Then you move on, there's so much to do. In particular I wanted to be able to specify a time range to select exactly the events occuring during that time. The most straightforward way to do that is to give a &amp;quot;start time&amp;quot; and an &amp;quot;end time&amp;quot;:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;   /**&lt;br /&gt;    * DaytimeRange(0, 2*60*60*1000) is 00:00 -&amp;gt; 02:00&lt;br /&gt;    */&lt;br /&gt;   case class DaytimeRange(start: Long, end: Long)&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Here starts the ugliness. When I want to check if a &lt;code class="prettyprint"&gt;startTime&lt;/code&gt; given by the log file is included in the &lt;code class="prettyprint"&gt;DaytimeRange&lt;/code&gt; I have to do a conversion to make sure I'm using the proper &lt;code class="prettyprint"&gt;Long&lt;/code&gt;s: the number of milliseconds since the start of the day, not the milliseconds since the &lt;code class="prettyprint"&gt;EPOCH&lt;/code&gt; time!&lt;/p&gt;&lt;p&gt;Similarly, if I blindly try to reuse the &lt;code class="prettyprint"&gt;hhmm&lt;/code&gt; method defined above, I need to make sure I apply that to a number of milliseconds corresponding to an &lt;code class="prettyprint"&gt;EPOCH&lt;/code&gt; time and not just since the beginning of the day.&lt;/p&gt;&lt;p&gt;That's the recipe for disaster,...&lt;/p&gt;&lt;a name="Twitter+forever"&gt;&lt;/a&gt;&lt;h4&gt;Twitter forever&lt;/h4&gt;&lt;p&gt;Fortunately the answer was right there, in my Twitter timeline (well in my memory of the timeline to be more precise :-)): use &amp;quot;Unboxed newtypes&amp;quot;.&lt;/p&gt;&lt;p&gt;It all fits in a few lines of code but makes everything incredibly clear. First we define &amp;quot;Tagged types&amp;quot;:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  type Tagged[U] = { type Tag = U }&lt;br /&gt;  type @@[T, U] = T with Tagged[U]&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Then we declare that there are 2 different types of time:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  trait Day&lt;br /&gt;  trait Epoch&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;And we declare that a given &lt;code class="prettyprint"&gt;Long&lt;/code&gt; will either represent the number of millis since 1970 or since the beginning of the day:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  type Epochtime = Long @@ Epoch&lt;br /&gt;  type Daytime   = Long @@ Day&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;code class="prettyprint"&gt;Daytime&lt;/code&gt; simply means that we have a &lt;code class="prettyprint"&gt;Long&lt;/code&gt; value, with an additional &lt;code class="prettyprint"&gt;Day&lt;/code&gt; type.&lt;/p&gt;&lt;p&gt;Finally, we provide 2 functions to create instances of those types from &lt;code class="prettyprint"&gt;Long&lt;/code&gt;s:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  def daytime(i: java.lang.Long): Daytime     = i.asInstanceOf[Daytime]&lt;br /&gt;  def epochtime(i: java.lang.Long): Epochtime = i.asInstanceOf[Epochtime]&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;with a method which explicitly converts &lt;code class="prettyprint"&gt;EPOCH&lt;/code&gt; millis to &amp;quot;day&amp;quot; millis:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  def epochtimeToDaytime(time: Long): Daytime = {&lt;br /&gt;    val calendar = Calendar.getInstance&lt;br /&gt;    calendar.setTime(new Date(time))&lt;br /&gt;    daytime(((calendar.get(HOUR_OF_DAY)* 60 +&lt;br /&gt;              calendar.get(MINUTE))    * 60 +&lt;br /&gt;              calendar.get(SECOND))    * 1000 +&lt;br /&gt;              calendar.get(MILLISECOND))&lt;br /&gt;  }&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;a name="Using+the+new+toys"&gt;&lt;/a&gt;&lt;h4&gt;Using the new toys&lt;/h4&gt;&lt;p&gt;We can use the &lt;code class="prettyprint"&gt;Daytime&lt;/code&gt; type for our &lt;code class="prettyprint"&gt;DaytimeRange&lt;/code&gt; class:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  case class DaytimeRange(start: Daytime, end: Daytime)&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;There's no risk that we now accidentally create a &lt;code class="prettyprint"&gt;DaytimeRange&lt;/code&gt; instance with Longs which do not represent elapsed millis since the beginning of the day. The compiler reminds us to write code like:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  /** @return the number of millis from a string representing hours and minutes */&lt;br /&gt;  def hhmmToDaytime(s: String): Daytime = ...&lt;br /&gt;&lt;br /&gt;  DaytimeRange(hhmmToDaytime(&amp;quot;10:00&amp;quot;), hhmmToDaytime(&amp;quot;10:20&amp;quot;))&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;And if we want to create a &lt;code class="prettyprint"&gt;DaytimeRange&lt;/code&gt; instance from 2 &lt;code class="prettyprint"&gt;startTime&lt;/code&gt;s found in the log file:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  DaytimeRange(epochtimeToDaytime(s1), epochtimeToDaytime(s2))&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Similarly, we can use the &lt;code class="prettyprint"&gt;Epochtime&lt;/code&gt; for the &lt;code class="prettyprint"&gt;hhmm&lt;/code&gt; display&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  implicit def toEpochtimeDisplay(t: Epochtime) = new EpochtimeDisplay(t)&lt;br /&gt;&lt;br /&gt;  case class EpochtimeDisplay(time: Epochtime) {&lt;br /&gt;    // here new Date expects a Long, but this is ok because Epochtime *is* a Long&lt;br /&gt;    def hhmm = hhmmFormat.format(new Date(time))&lt;br /&gt;  }&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;We can safely reuse this code to display a &lt;code class="prettyprint"&gt;DaytimeRange&lt;/code&gt; instance:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  case class DaytimeRange(start: Daytime, end: Daytime) {&lt;br /&gt;&lt;br /&gt;    // the developer *has* to think about which kind of time he's handling&lt;br /&gt;    def show = daytimeToEpochtime(start).hhmm + &amp;quot; -&amp;gt; &amp;quot; + daytimeToEpochtime(end).hhmm&lt;br /&gt;&lt;br /&gt;  }&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;a name="Final+comments"&gt;&lt;/a&gt;&lt;h4&gt;Final comments&lt;/h4&gt;&lt;ul&gt;  &lt;li&gt;It's practical&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;This technique is very pratical because it avoids making silly mistakes with Longs representing different concepts while still keeping the ability to use them as Long objects without having to &amp;quot;Unbox&amp;quot; them. Indeed we could also have created a case class like:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  case class Daytime(time: Long)&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;But then we would have had to &amp;quot;unbox&amp;quot; the &lt;code class="prettyprint"&gt;time&lt;/code&gt; value everytime we wanted to do an addition or a comparison.&lt;/p&gt;&lt;ul&gt;  &lt;li&gt;WTF?&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;I had a compiler puzzler when with my first implementation:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  case class DaytimeRange(start: Daytime, end: Daytime)&lt;br /&gt;             ^&lt;br /&gt;  found   : Double&lt;br /&gt;  required: AnyRef&lt;br /&gt;&lt;br /&gt;  Note: an implicit exists from scala.Double =&amp;gt; java.lang.Double, but methods inherited from Object are&lt;br /&gt;  rendered ambiguous.  This is to avoid a blanket implicit which would convert any scala.Double to any AnyRef.&lt;br /&gt;  You may wish to use a type ascription: `x: java.lang.Double`.&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Go figure what that means in this context,... After much head scratching, I found a workaround:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  type Epochtime = java.lang.Long @@ Epoch&lt;br /&gt;  type Daytime   = java.lang.Long @@ Day&lt;br /&gt;&lt;br /&gt;  def daytime(i: java.lang.Long): Daytime     = i.asInstanceOf[Daytime]&lt;br /&gt;  def epochtime(i: java.lang.Long): Epochtime = i.asInstanceOf[Epochtime]&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;I used &lt;code class="prettyprint"&gt;java.lang.Long&lt;/code&gt; instead of &lt;code class="prettyprint"&gt;scala.Long&lt;/code&gt; because it looks like we need to get &lt;code class="prettyprint"&gt;AnyRef&lt;/code&gt; objects while &lt;code class="prettyprint"&gt;scala.Long&lt;/code&gt; is only &lt;code class="prettyprint"&gt;AnyVal&lt;/code&gt;. But the compiler message is still very obscure in that case.&lt;/p&gt;&lt;ul&gt;  &lt;li&gt;This is not a unit system!&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Because &lt;code class="prettyprint"&gt;Epochtime&lt;/code&gt; and &lt;code class="prettyprint"&gt;Daytime&lt;/code&gt; are still &lt;code class="prettyprint"&gt;Longs&lt;/code&gt;, it is still possible to add them and make a mess!&lt;/p&gt;&lt;ul&gt;  &lt;li&gt;Kudos to @retronym too&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;You'll see that Unboxed Tagged Types are also part of the next &lt;a href="https://github.com/retronym/scalaz7-experimental/blob/master/core/src/main/scala/scalaz/std/AnyVal.scala#L35"&gt;scalaz7&lt;/a&gt;. Jason came up with the &lt;code class="prettyprint"&gt;@@&lt;/code&gt; type alias and is using the tag types to distinguish &amp;quot;multiplicative&amp;quot; monoids from &amp;quot;additive&amp;quot; monoids. Or conjonctive vs disjonctive. This means that given a &lt;code class="prettyprint"&gt;Monoid[Boolean]&lt;/code&gt;, we can specify if it does an &lt;code class="prettyprint"&gt;AND&lt;/code&gt; or if it does an &lt;code class="prettyprint"&gt;OR&lt;/code&gt;. Scalaz is becoming the ultimate toolbox,...&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5336273-3573552439796353059?l=etorreborre.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://etorreborre.blogspot.com/feeds/3573552439796353059/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5336273&amp;postID=3573552439796353059' title='14 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5336273/posts/default/3573552439796353059'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5336273/posts/default/3573552439796353059'/><link rel='alternate' type='text/html' href='http://etorreborre.blogspot.com/2011/11/practical-uses-for-unboxed-tagged-types.html' title='Practical uses for Unboxed Tagged Types'/><author><name>Eric</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://farm3.static.flickr.com/2203/2118752971_7becaf7476_t.jpg'/></author><thr:total>14</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5336273.post-4745348747411895268</id><published>2011-10-26T07:45:00.001+09:00</published><updated>2011-10-26T21:22:22.947+09:00</updated><title type='text'>Scala collections are awesome</title><content type='html'>&lt;p&gt;This is just a small post to show how incredibly easy it is to use scala concurrency constructs to speed-up your work.&lt;/p&gt;&lt;a name="Doing+the+job"&gt;&lt;/a&gt;&lt;h4&gt;Doing the job&lt;/h4&gt;&lt;p&gt;This question occured to me as a &lt;a href="https://github.com/etorreborre/specs2/issues/39"&gt;new enhancement was requested for specs2&lt;/a&gt;. In specs2, the processing of examples was very sequential: &lt;/p&gt;&lt;ol&gt;  &lt;li&gt;all the examples are executed concurrently&lt;/li&gt;  &lt;li&gt;then they are reported to the console&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;The concurrent execution of examples makes it generally fast enough that the reporting appears fast on the screen. However, if examples take a long time to execute, you might think that sbt is stuck with nothing whatsoever being reported back to you. You might even think that the compilation is still going on!&lt;/p&gt;&lt;p&gt;I changed that in specs2 and now the examples are reported as soon as they are executed. But I also thought: &amp;quot;this question must happen all the time, for all sorts of tasks&amp;quot;. This is why I'm going to demonstrate how straightforward it is to speed-up your computations or your scripts with just a few annotations and imports.&lt;/p&gt;&lt;p&gt;Let's say I have some jobs to execute, each of them taking a random time:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  def job(i: Int) = {&lt;br /&gt;    Thread.sleep(50 * random.nextInt(i))&lt;br /&gt;    println(&amp;quot;executed &amp;quot;+i)&lt;br /&gt;    i&lt;br /&gt;  } &lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;And I have a function to report the results:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  def report(i: Int) = println(&amp;quot;  reported &amp;quot;+i) &lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;a name="Naively"&gt;&lt;/a&gt;&lt;h4&gt;Naively&lt;/h4&gt;&lt;p&gt;The naive approach for executing and reporting the jobs would be:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  def naive(n: Int) = (1 to n).map(job).foreach(report)&lt;br /&gt;&lt;br /&gt;  scala&amp;gt;naive(3)&lt;br /&gt;  executed 1&lt;br /&gt;  executed 2&lt;br /&gt;  executed 3&lt;br /&gt;    reported 1&lt;br /&gt;    reported 2&lt;br /&gt;    reported 3&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Unfortunately this is slow since all the jobs are executed in sequence, then reported. &lt;/p&gt;&lt;a name="Wrongly"&gt;&lt;/a&gt;&lt;h4&gt;Wrongly&lt;/h4&gt;&lt;p&gt;If we want to make a better use of our laptop's cores, we can write:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  def parallel(n: Int) = (1 to n).par.map(job).foreach(report)&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This is better, because now the jobs are executed in parallel, but not satisfying because the reporting now comes out of order:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  scala&amp;gt;parallel(3)&lt;br /&gt;  executed 3&lt;br /&gt;  executed 1&lt;br /&gt;  executed 2&lt;br /&gt;    reported 3&lt;br /&gt;    reported 1&lt;br /&gt;    reported 2&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;a name="Fast+and+right"&gt;&lt;/a&gt;&lt;h4&gt;Fast and right&lt;/h4&gt;&lt;p&gt;The trick to have best of both worlds is to use futures and &lt;code class="prettyprint"&gt;seq&lt;/code&gt;:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  import actors._&lt;br /&gt;  import Futures._&lt;br /&gt;&lt;br /&gt;  def best(n: Int) = (1 to n).par.map(future(job)).seq.foreach(f =&amp;gt; report(f()))&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;In the code above, we execute concurrently each job in a &lt;code class="prettyprint"&gt;Future&lt;/code&gt;. Then we force the collection to be evaluated sequentially again with &lt;code class="prettyprint"&gt;seq&lt;/code&gt; and we report each result with &lt;code class="prettyprint"&gt;f()&lt;/code&gt;, using the &lt;code class="prettyprint"&gt;Future.apply&lt;/code&gt; method to wait until the value is available:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  scala&amp;gt;best(3)&lt;br /&gt;  executed 3&lt;br /&gt;  executed 1&lt;br /&gt;    reported 1&lt;br /&gt;  executed 2&lt;br /&gt;    reported 2&lt;br /&gt;    reported 3&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;a name="Conclusion"&gt;&lt;/a&gt;&lt;h4&gt;Conclusion&lt;/h4&gt;&lt;p&gt;With a few minor modifications to the first code version we managed to do exactly what we wanted. Having this kind of power in the toolbox feels great, and I hope this post contributes a bit more to showing that Scala is &lt;strong&gt;&lt;em&gt;also&lt;/em&gt;&lt;/strong&gt; a practical language (&lt;em&gt;because there's nothing wrong with being academic :-)&lt;/em&gt;).&lt;/p&gt;&lt;a name="Update"&gt;&lt;/a&gt;&lt;h4&gt;Update&lt;/h4&gt;&lt;p&gt;Before I get flamed down in the comments, here an interesting update :-).&lt;/p&gt;&lt;p&gt;As soon as I published this post, I started wondering if the &lt;code class="prettyprint"&gt;best&lt;/code&gt; behavior had anything to do with parallel collections at all. It turns out that using futures only gives good results as well:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  def futures(n: Int) = (1 to n).map(future(job)).foreach(f =&amp;gt; report(f()))&lt;br /&gt;&lt;br /&gt;  scala&amp;gt;futures(3)&lt;br /&gt;  executed 2&lt;br /&gt;  executed 1&lt;br /&gt;    reported 1&lt;br /&gt;  executed 3&lt;br /&gt;    reported 2&lt;br /&gt;    reported 3&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;However, if the input collection is a view, the results are completely different:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  def futures(n: Int) = (1 to n).view.map(future(job)).foreach(f =&amp;gt; report(f()))&lt;br /&gt;&lt;br /&gt;  scala&amp;gt;futures(3)&lt;br /&gt;  executed 1&lt;br /&gt;    reported 1&lt;br /&gt;  executed 2&lt;br /&gt;    reported 2&lt;br /&gt;  executed 3&lt;br /&gt;    reported 3&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Hence the use of &lt;code class="prettyprint"&gt;par&lt;/code&gt; is really necessary, in this scenario, to get a fully parallel execution.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5336273-4745348747411895268?l=etorreborre.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://etorreborre.blogspot.com/feeds/4745348747411895268/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5336273&amp;postID=4745348747411895268' title='9 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5336273/posts/default/4745348747411895268'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5336273/posts/default/4745348747411895268'/><link rel='alternate' type='text/html' href='http://etorreborre.blogspot.com/2011/10/scala-collections-are-awesome.html' title='Scala collections are awesome'/><author><name>Eric</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://farm3.static.flickr.com/2203/2118752971_7becaf7476_t.jpg'/></author><thr:total>9</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5336273.post-3830475929188027497</id><published>2011-10-12T08:49:00.000+09:00</published><updated>2011-10-12T12:09:59.988+09:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='swing'/><category scheme='http://www.blogger.com/atom/ns#' term='frp'/><category scheme='http://www.blogger.com/atom/ns#' term='gui'/><category scheme='http://www.blogger.com/atom/ns#' term='scala'/><title type='text'>Counting words - part 2</title><content type='html'>&lt;p&gt;In this small post, I'm going to show how I used &lt;a href="http://wikipedia.org/Functional_Reactive_Programming.html"&gt;Functional Reactive Programming (FRP)&lt;/a&gt; to improve the GUI part of the small application presented in the &lt;a href="http://etorreborre.blogspot.com/2011/10/counting-words.html"&gt;previous post&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;The best article to read on the subject is &lt;a href="http://lamp.epfl.ch/~imaier/pub/DeprecatingObserversTR2010.pdf"&gt;Deprecating the Observer Pattern (DOP)&lt;/a&gt;. This article explains what are the drawbacks of using a listener-based approach to components communication in a user interface and proposes FRP as an alternative.&lt;/p&gt;&lt;a name="Be+reactive"&gt;&lt;/a&gt;&lt;h4&gt;Be reactive&lt;/h4&gt;&lt;p&gt;What I've been using is a simplified yet powerful version of the code described in DOP, using the &lt;a href="http://github.com/nafg/reactive"&gt;Reactive&lt;/a&gt; library by Naftoli Gugenheim. In this library you have 2 essential concepts: &lt;code class="prettyprint"&gt;EventStream&lt;/code&gt;s and &lt;code class="prettyprint"&gt;Signal&lt;/code&gt;s:&lt;/p&gt;&lt;ul&gt;  &lt;li&gt;&lt;code class="prettyprint"&gt;EventStream[T]&lt;/code&gt; is a collection of values of type &lt;code class="prettyprint"&gt;T&lt;/code&gt; happening at certain moments in time&lt;/li&gt;  &lt;li&gt;&lt;code class="prettyprint"&gt;Signal[T]&lt;/code&gt; is a value of type &lt;code class="prettyprint"&gt;T&lt;/code&gt; which can change from time to time&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;They are actually 2 sides of the same coin (more or less) as explained in &lt;a href="http://skillsmatter.com/podcast/scala/reactors"&gt;this video&lt;/a&gt;: from an &lt;code class="prettyprint"&gt;EventStream&lt;/code&gt; you can get a &lt;code class="prettyprint"&gt;Signal&lt;/code&gt; by &lt;code class="prettyprint"&gt;holding&lt;/code&gt; the last emitted value and from a &lt;code class="prettyprint"&gt;Signal&lt;/code&gt; you can get an &lt;code class="prettyprint"&gt;EventStream&lt;/code&gt; by getting all the &lt;code class="prettyprint"&gt;changes&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;The great thing about an &lt;code class="prettyprint"&gt;EventStream&lt;/code&gt; (let's focus on that one for now) is that you can manipulate it like a collection of objects:&lt;/p&gt;&lt;ul&gt;  &lt;li&gt;you can &lt;code class="prettyprint"&gt;filter&lt;/code&gt; it to get only the events you're interested in&lt;/li&gt;  &lt;li&gt;you can &lt;code class="prettyprint"&gt;map&lt;/code&gt; the events to other events&lt;/li&gt;  &lt;li&gt;you can &lt;code class="prettyprint"&gt;flatMap&lt;/code&gt; with a function returning another &lt;code class="prettyprint"&gt;EventStream&lt;/code&gt; and so on,...&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;But how is this helpful for GUI programming? &lt;/p&gt;&lt;a name="With+GUI+components"&gt;&lt;/a&gt;&lt;h4&gt;With GUI components&lt;/h4&gt;&lt;p&gt;The hard reality of Swing GUI components is that they are really mutable at heart. Once you compose a GUI with components (say a &lt;code class="prettyprint"&gt;Frame&lt;/code&gt;) containing other components (say a &lt;code class="prettyprint"&gt;TextField&lt;/code&gt;), then, when anything happens in your application, you mutate the innermost components heavily (by changing the text color for example). When you add publish/subscribe mechanisms on top of that, you add even more developer-managed mutability since you need to add-remove listeners to the whole graph of components. It is also not very easy to understand how events &amp;quot;flow&amp;quot; between components.&lt;/p&gt;&lt;p&gt;The way I see the usage of FRP with GUIs is:&lt;/p&gt;&lt;ul&gt;  &lt;li&gt;&lt;p&gt;components are seen as either &lt;em&gt;creating&lt;/em&gt; values to propagate (like a button action) or &lt;em&gt;consuming&lt;/em&gt; values (like a text field change with the new value). Hence they have a role of an &lt;code class="prettyprint"&gt;EventStream&lt;/code&gt; source or of an &lt;code class="prettyprint"&gt;EventStream&lt;/code&gt; sink (sometimes both)&lt;/p&gt;&lt;/li&gt;  &lt;li&gt;&lt;p&gt;those components explicitely declare the &lt;em&gt;abstract type&lt;/em&gt; of streamed events they're expecting. For a given &lt;code class="prettyprint"&gt;TextField&lt;/code&gt; this might be something like &lt;code class="prettyprint"&gt;NewSelectedFile&lt;/code&gt; whether the selection comes from a &lt;code class="prettyprint"&gt;FileChooser&lt;/code&gt; or from a simple &lt;code class="prettyprint"&gt;TextField&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;  &lt;li&gt;&lt;p&gt;the event streams can be merged, filtered, mapped functionally, with no side-effect so that the logic of events routing is very composable and explicit&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Let's see that more precisely in the context of &lt;a href="http://github.com/etorreborre/wordcount/blob/reactive/src/main/scala/essays/CountApp.scala#L35"&gt;my simple application&lt;/a&gt;.&lt;/p&gt;&lt;a name="An+example"&gt;&lt;/a&gt;&lt;h4&gt;An example&lt;/h4&gt;&lt;p&gt;In my WordCount application, the first thing the user does is to select a file to count. Then she is supposed to click on the &amp;quot;Count&amp;quot; button to start the count before the results are displayed. In terms of graphical components I have:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;    val openFileMenuItem = OpenFileMenuItem(&amp;quot;./src/test/resources&amp;quot;)&lt;br /&gt;    val countAction      = CountAction()&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;What you don't see above is that those 2 custom GUI components have been declared as extending &lt;code class="prettyprint"&gt;EventStream[T]&lt;/code&gt; (simplified code for the explanation):&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;    OpenFileMenuItem ... with EventStream[File]&lt;br /&gt;    CountAction      ... with EventStream[Unit]&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This means that when you open a file using the &lt;code class="prettyprint"&gt;OpenFileMenuItem&lt;/code&gt; you're providing new &lt;code class="prettyprint"&gt;File&lt;/code&gt; events which other components can react on, and when you invoke the &lt;code class="prettyprint"&gt;CountAction&lt;/code&gt; you, well,... you just pressed the button, there's no meaningful value to convey, so the &lt;code class="prettyprint"&gt;Unit&lt;/code&gt; type is appropriate here (the clients just want to know that something happened).&lt;/p&gt;&lt;p&gt;Then we can compose those 2 &lt;code class="prettyprint"&gt;EventStream&lt;/code&gt;s:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  val filePath: Signal[String]  = openFileMenuItem.map(_.getPath).hold(&amp;quot;&amp;quot;)&lt;br /&gt;  val doCount: EventStream[Any] = filePath.distinct.change | countAction&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;First we do a bit of filtering, because we just need the file path as a &lt;code class="prettyprint"&gt;Signal[String]&lt;/code&gt; (using &lt;code class="prettyprint"&gt;hold&lt;/code&gt; to transform the stream to a signal, with an empty initial value).&lt;br /&gt;Then we declare that we need to do a count whether there's a distinct change in the file path value, or (&lt;code class="prettyprint"&gt;|&lt;/code&gt;), if the user pressed countAction button.&lt;/p&gt;&lt;p&gt;How do we &amp;quot;consume&amp;quot; this &lt;code class="prettyprint"&gt;doCount&lt;/code&gt; stream of events? We &lt;code class="prettyprint"&gt;flatMap&lt;/code&gt; it to another stream of events providing the results of the counting:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt; val results: EventStream[Results] =&lt;br /&gt;   doCount flatMap { doIt =&amp;gt; WordCounter.count(filePath.now).inBackground }&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;For each &lt;code class="prettyprint"&gt;doCount&lt;/code&gt; event we &lt;code class="prettyprint"&gt;flatMap&lt;/code&gt; it (actually we forget about it,...) and we use the current value of the &lt;code class="prettyprint"&gt;filePath&lt;/code&gt; to count the number of words.&lt;/p&gt;&lt;p&gt;The expression &lt;code class="prettyprint"&gt;computeValue.inBackground&lt;/code&gt; computes a value using the &lt;code class="prettyprint"&gt;SwingUtils.invokeLater&lt;/code&gt; method to avoid computations being done on the event dispatch thread (this might cause grey screens). The &lt;code class="prettyprint"&gt;inBackground&lt;/code&gt; method returns an &lt;code class="prettyprint"&gt;EventStream[T]&lt;/code&gt; to signal consumers that the value is ready.&lt;/p&gt;&lt;p&gt;Since the result of counting is an &lt;code class="prettyprint"&gt;EventStream[Results]&lt;/code&gt; I can then &amp;quot;plug&amp;quot; it into the GUI component doing the display:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  val resultsPanel = ResultsPanel(results)&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;And that's it. The &lt;code class="prettyprint"&gt;ResultsPanel&lt;/code&gt; component doesn't care where the values come from, who created them. It is also interesting to see how the &lt;code class="prettyprint"&gt;ResultsPanel&lt;/code&gt; declares its sub-components:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  object ResultsPanel {&lt;br /&gt;    def apply(results: EventStream[Results]) = {&lt;br /&gt;      new ResultsPanel(TotalWordsNumbers(results),&lt;br /&gt;                       ReferencesTable(results.map(_.references)),&lt;br /&gt;                       ErrorMessageBox(results.map(_.message)))&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;There are 3 sub-components and they use different parts of the &lt;code class="prettyprint"&gt;Results&lt;/code&gt; event stream, so we use the &lt;code class="prettyprint"&gt;map&lt;/code&gt; function to restrict exactly the stream to what is needed:&lt;/p&gt;&lt;ul&gt;  &lt;li&gt;&lt;p&gt;&lt;code class="prettyprint"&gt;TotalWordsNumbers&lt;/code&gt; uses the full &lt;code class="prettyprint"&gt;Results&lt;/code&gt; object to display the total words count (the one we really want), the references word count and the full text word count (to check that the count is ok)&lt;/p&gt;&lt;/li&gt;  &lt;li&gt;&lt;p&gt;&lt;code class="prettyprint"&gt;ReferencesTable&lt;/code&gt; just needs the references&lt;/p&gt;&lt;/li&gt;  &lt;li&gt;&lt;p&gt;&lt;code class="prettyprint"&gt;ErrorMessageBox&lt;/code&gt; needs the error message so we just &lt;code class="prettyprint"&gt;map&lt;/code&gt; that&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Finally, how is the event stream used in the component itself? Let's look at the &lt;code class="prettyprint"&gt;ErrorMessageBox&lt;/code&gt; component:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  case class ErrorMessageBox(message: EventStream[String]) extends TextField with Observing {&lt;br /&gt;    foreground = Color.red&lt;br /&gt;    font = new Font(&amp;quot;Courier&amp;quot;, Font.PLAIN, 15);&lt;br /&gt;    editable = false&lt;br /&gt;&lt;br /&gt;    message.foreach { m =&amp;gt; text = m }&lt;br /&gt;  }&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The important line is the last one where we declare that for each message event, we change the &lt;code class="prettyprint"&gt;text&lt;/code&gt; attribute of the &lt;code class="prettyprint"&gt;TextField&lt;/code&gt; to the new value &lt;code class="prettyprint"&gt;m&lt;/code&gt;.&lt;/p&gt;&lt;a name="Some+implementation+notes"&gt;&lt;/a&gt;&lt;h4&gt;Some implementation notes&lt;/h4&gt;&lt;p&gt;If you read the actual code, you'll find quite a few differences with the real implementation:&lt;/p&gt;&lt;ul&gt;  &lt;li&gt;&lt;p&gt;the &lt;code class="prettyprint"&gt;inBackground&lt;/code&gt; mechanism is enhanced with an additional &lt;code class="prettyprint"&gt;actionProgress&lt;/code&gt; eventStream which fires events before and after the action to execute. This is used to change the main frame cursor to a waiting watch when the computation takes some time&lt;/p&gt;&lt;/li&gt;  &lt;li&gt;&lt;p&gt;I found useful to introduce a trait called &lt;code class="prettyprint"&gt;Trigger&lt;/code&gt; for &lt;code class="prettyprint"&gt;EventStream[()]&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;  &lt;li&gt;&lt;p&gt;I also added an &lt;code class="prettyprint"&gt;EventStreamSourceProxy[T] extends EventStream[T]&lt;/code&gt; trait which can be mixed in any GUI class. This trait uses an internal &lt;code class="prettyprint"&gt;EventSource[T]&lt;/code&gt; which is the implementation of the &lt;code class="prettyprint"&gt;EventStream[T]&lt;/code&gt; and can be used to fire events. For example:&lt;/p&gt;  &lt;pre&gt;&lt;code class="prettyprint"&gt;// When the action is executed (a file is selected) we fire an event and&lt;br /&gt;// the whole `OpenFileMenuItem` component acts as an `EventStream[File]`.&lt;br /&gt;case class OpenFileMenuItem(start: String) extends MenuItem(&amp;quot;Open&amp;quot;) with EventStreamSourceProxy[File] {&lt;br /&gt;  ...&lt;br /&gt;  action = new Action(&amp;quot;Open&amp;quot;) {&lt;br /&gt;    def apply() {&lt;br /&gt;      ...&lt;br /&gt;      source.fire(fileChooser.selectedFile)&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;  &lt;li&gt;&lt;p&gt;I had some difficult debugging time with the &lt;code class="prettyprint"&gt;Observing&lt;/code&gt; trait. If you place it on an object that's going to be garbage collected, your components will not be notified of new events. This happened to me as I placed it on a class used for an implicit conversion, in order to get a new &lt;code class="prettyprint"&gt;shinyMethod&lt;/code&gt; (tm):&lt;/p&gt;  &lt;pre&gt;&lt;code class="prettyprint"&gt;implicit def toComponent(a: A): ImplicitComponent = new ImplicitComponent(a)&lt;br /&gt;class ImplicitComponent(a: A) extends Observing {&lt;br /&gt;  def shinyMethod = a&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;&lt;/ul&gt;&lt;a name="Features+ideas"&gt;&lt;/a&gt;&lt;h4&gt;Features ideas&lt;/h4&gt;&lt;p&gt;This is something which amazed me: just having to think about event streams made me rethink the application functionalities. How? When I started thinking about what would trigger a word count I realized that:&lt;/p&gt;&lt;ul&gt;  &lt;li&gt;&lt;p&gt;there is no reason why the user should have to click the &amp;quot;Count&amp;quot; button when the file is selected, we can do the count and display the results right away&lt;/p&gt;&lt;/li&gt;  &lt;li&gt;&lt;p&gt;thinking about event stream as a flux made me realize that the file can be polled regularly for changes and the results displayed whenever there's a change&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Changing my program to incorporate those 2 ideas was soooo easy:&lt;/p&gt;&lt;ol&gt;  &lt;li&gt;&lt;p&gt;the first idea is reflected by the &lt;code class="prettyprint"&gt;doCount&lt;/code&gt; event stream definition given above, we just say that we want to count whenever there's a file selection or a count action&lt;/p&gt;  &lt;pre&gt;&lt;code class="prettyprint"&gt;val doCount: EventStream[Any] = filePath.distinct.change | countAction&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;  &lt;li&gt;&lt;p&gt;creating a file poller is easy using the &lt;a href="https://github.com/nafg/reactive/blob/master/reactive-core/src/main/scala/reactive/Timer.scala"&gt;&lt;code class="prettyprint"&gt;Timer&lt;/code&gt;&lt;/a&gt; class from the reactive library&lt;/p&gt;  &lt;pre&gt;&lt;code class="prettyprint"&gt;class FilePoller(path: Signal[String], delay: Long = 500) extends Trigger {&lt;br /&gt;&lt;br /&gt;  private var previousLastModified = new File(path.now).lastModified()&lt;br /&gt;&lt;br /&gt;  val timer = new Timer(0, delay, {t =&amp;gt; false}) foreach { tick =&amp;gt;&lt;br /&gt;    def newLastModified = new File(path.now).lastModified()&lt;br /&gt;    if (newLastModified &amp;gt; previousLastModified || newLastModified == 0) {&lt;br /&gt;      previousLastModified = newLastModified&lt;br /&gt;      source.fire(())&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;The `FilePoller` uses a `path` signal regularly. If the underlying file is modified, a notification event is triggered.&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;  &lt;li&gt;&lt;p&gt;then the final version of &lt;code class="prettyprint"&gt;doCount&lt;/code&gt; becomes:&lt;/p&gt;  &lt;pre&gt;&lt;code class="prettyprint"&gt;lazy val filePoller                = new FilePoller(filePath)&lt;br /&gt;lazy val doCount: EventStream[Any] = filePath.distinct.change | countAction | filePoller&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;I don't know about you but I really find nice that the abstractions in my implementation give me hints about what the application could do! For me this is a good sign that FRP is really well-suited for the job of GUI programming.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5336273-3830475929188027497?l=etorreborre.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://etorreborre.blogspot.com/feeds/3830475929188027497/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5336273&amp;postID=3830475929188027497' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5336273/posts/default/3830475929188027497'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5336273/posts/default/3830475929188027497'/><link rel='alternate' type='text/html' href='http://etorreborre.blogspot.com/2011/10/counting-words-part-2.html' title='Counting words - part 2'/><author><name>Eric</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://farm3.static.flickr.com/2203/2118752971_7becaf7476_t.jpg'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5336273.post-9093661194415181439</id><published>2011-10-11T16:47:00.000+09:00</published><updated>2011-10-12T12:09:38.453+09:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='swing'/><category scheme='http://www.blogger.com/atom/ns#' term='specs2'/><category scheme='http://www.blogger.com/atom/ns#' term='xml'/><category scheme='http://www.blogger.com/atom/ns#' term='gui'/><category scheme='http://www.blogger.com/atom/ns#' term='scala'/><title type='text'>Counting words</title><content type='html'>&lt;p&gt;In this 3 parts post I want to show:&lt;/p&gt;&lt;ol&gt;  &lt;li&gt;how standard, out-of-the-box, Scala helped me to code a small application&lt;/li&gt;  &lt;li&gt;how &lt;a href="http://en.wikipedia.org/wiki/Functional_reactive_programming"&gt;Functional Reactive Programming&lt;/a&gt; brings a real improvement on how the GUI is built&lt;/li&gt;  &lt;li&gt;how to replace Parser Combinators with the &lt;a href="http://www.parboiled.org"&gt;Parboiled&lt;/a&gt; library to enhance error reporting&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;You can access the application project via &lt;a href="http://github.com/etorreborre/wordcount"&gt;github&lt;/a&gt;.&lt;/p&gt;&lt;a name="I+can+do+that+in+2+hours%21"&gt;&lt;/a&gt;&lt;h4&gt;I can do that in 2 hours!&lt;/h4&gt;&lt;p&gt;That's more or less what I told my wife as she was explaining one small problem she had. My wife is studying psychology and she has lots of essays to write, week after week. One small burden she's facing is keeping track of the number of words she writes because each essay must fit in a specific number of words, say 4000 +/- 10%. The difficulty is that quotations and references must &lt;em&gt;not&lt;/em&gt; be counted. So she cannot check the file properties in Word or Open Office and she has to keep track manually.&lt;/p&gt;&lt;p&gt;For example, she may write: &lt;em&gt;&amp;quot;... as suggested by the Interpretation of Dreams (Freud, 1905, p.145) ...&amp;quot;&lt;/em&gt;. The reference &lt;em&gt;&amp;quot;(Freud, 1905, p.145)&amp;quot;&lt;/em&gt; must not be counted. Or, &lt;em&gt;&amp;quot;Margaret Malher wrote: &amp;quot;if the infant has an optimal experience of the symbiotic union with the mother, then the infant can make a smooth psychological differentiation from the mother to a further psychological expansion beyond the symbiotic state.&amp;quot; (Malher cited in St. Clair, 2004, p.92)&amp;quot;&lt;/em&gt; (good luck with that :-)). In that case the quotation is not counted either and we must only count 3 words.&lt;/p&gt;&lt;p&gt;Since this counting is a bit tedious and has to be adjusted each time she does a revision of her essay, I proposed to automate this check. I thought &amp;quot;a few lines of Parser Combinators should be able to do the trick, 2 hours max&amp;quot;. It actually took me a bit more (tm) to:&lt;/p&gt;&lt;ul&gt;  &lt;li&gt;&lt;p&gt;write a parser robust enough to accommodate for all sorts of variations and irregularities. For example, pages can be written as &amp;quot;p.154&amp;quot; or &amp;quot;pp.154-155&amp;quot;, years can also be written &amp;quot;[1905] 1962&amp;quot; where 1905 is the first edition, and so on&lt;/p&gt;&lt;/li&gt;  &lt;li&gt;&lt;p&gt;use scala-swing to display the results: number of words, references table, file selection&lt;/p&gt;&lt;/li&gt;  &lt;li&gt;&lt;p&gt;write readers to extract the text from .docx or .odt files&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Let's see now how Scala helped me with those 3 tasks.&lt;/p&gt;&lt;a name="Parsing+the+text"&gt;&lt;/a&gt;&lt;h3&gt;Parsing the text&lt;/h3&gt;&lt;p&gt;The idea behind parser combinators is very powerful. Instead of building a monolithic parser with lots of sub-routines and error-prone tracking of character indices, you describe the grammar of the text to parse by combining smaller parsers in many different ways.&lt;/p&gt;&lt;p&gt;In Scala, to do this, you need to extend one of the &lt;code class="prettyprint"&gt;Parsers&lt;/code&gt; traits. The one I've choosen is &lt;code class="prettyprint"&gt;RegexParsers&lt;/code&gt;. This is a parser which is well suited for unstructured text. If you were to parse something more akin to a programming language you might prefer &lt;code class="prettyprint"&gt;StdTokenParsers&lt;/code&gt; which already define keywords, numeric/string literals, identifiers,...&lt;/p&gt;&lt;p&gt;I'm now just going to comment on a few points regarding the &lt;a href="https://github.com/etorreborre/wordcount/tree/reactive/src/main/scala/essays/TextParsing"&gt;&lt;code class="prettyprint"&gt;TextParsing&lt;/code&gt;&lt;/a&gt; trait which is parsing the essay text. If you want to understand how parser combinators work in detail, please read the excellent blog post by Daniel Spiewak: &lt;a href="http://www.codecommit.com/blog/scala/the-magic-behind-parser-combinators"&gt;The Magic behind Parser Combinators&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;The main definition for this parser is:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  def referencedText: Parser[Results] =&lt;br /&gt;    rep((references | noRefParenthesised | quotation | words | space) &amp;lt;~ opt(punctuation)) ^^ reduceResults&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This means that I expect the text to be:&lt;/p&gt;&lt;ul&gt;  &lt;li&gt;&lt;p&gt;a repetition (the &lt;code class="prettyprint"&gt;rep&lt;/code&gt; combinator) of a parser&lt;/p&gt;&lt;/li&gt;  &lt;li&gt;&lt;p&gt;the repeated parser is an alternation (written &lt;code class="prettyprint"&gt;|&lt;/code&gt;) of references, parenthetised text, quotations, words or spaces. For each of these &amp;quot;blocks&amp;quot; I'm able to count the number of words. For example, a reference will be 0 and parenthetised text will be the number of words between the parentheses&lt;/p&gt;&lt;/li&gt;  &lt;li&gt;&lt;p&gt;there can be a following punctuation sign (optional, using the &lt;code class="prettyprint"&gt;opt&lt;/code&gt; combinator), but we don't really care about it, so it can be discarded (hence the &lt;code class="prettyprint"&gt;&amp;lt;~&lt;/code&gt; combinator, instead of &lt;code class="prettyprint"&gt;~&lt;/code&gt; which sequences 2 parsers)&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Then I have a function called &lt;code class="prettyprint"&gt;reduceResults&lt;/code&gt; taking the result of the parsing of each repeated parser, to create the final &lt;a href="https://github.com/etorreborre/wordcount/tree/master/src/main/scala/essays/Result"&gt;&lt;code class="prettyprint"&gt;Result&lt;/code&gt;&lt;/a&gt;, which is a case class providing:&lt;/p&gt;&lt;ul&gt;  &lt;li&gt;the number of counted words&lt;/li&gt;  &lt;li&gt;the references in the text&lt;/li&gt;  &lt;li&gt;the quotations in the text&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Using the &lt;code class="prettyprint"&gt;RegexParser&lt;/code&gt; trait is very convenient. For example, if I want to specify how to parse &amp;quot;Pages&amp;quot; in a reference: &lt;code class="prettyprint"&gt;(Freud, 1905, p.154)&lt;/code&gt;, I can sequence 2 parsers built from regular expressions:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  val page = &amp;quot;p\\.*\\s*&amp;quot;.r ~ &amp;quot;\\d+&amp;quot;.r&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;ul&gt;  &lt;li&gt;appending &lt;code class="prettyprint"&gt;.r&lt;/code&gt; to a string returns a regular expression (of type &lt;code class="prettyprint"&gt;Regex&lt;/code&gt;)&lt;/li&gt;  &lt;li&gt;there is an implicit conversion method in the &lt;code class="prettyprint"&gt;RegexParsers&lt;/code&gt; trait, called &lt;code class="prettyprint"&gt;regex&lt;/code&gt; from a &lt;code class="prettyprint"&gt;Regex&lt;/code&gt; to a &lt;code class="prettyprint"&gt;Parser[String]&lt;/code&gt;&lt;/li&gt;  &lt;li&gt;I can sequence 2 parsers using the &lt;code class="prettyprint"&gt;~&lt;/code&gt; operator&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;The &lt;code class="prettyprint"&gt;page&lt;/code&gt; parser above can recognize page numbers like &lt;code class="prettyprint"&gt;p.134&lt;/code&gt; or &lt;code class="prettyprint"&gt;p.1&lt;/code&gt; but it will also accept &lt;code class="prettyprint"&gt;p134&lt;/code&gt;. You can argue that this is not very well formatted, and my wife will agree with you. However she certainly doesn't want to see the count of words being wrong or fail just because she forgot a dot! The plan here is to display what was parsed so that she can eventually fix some incorrect references, not written according to the academia standards. We'll see, in part 3 of this series how we can use another parsing library to manage those errors, without breaking the parsing.&lt;/p&gt;&lt;p&gt;One more important thing to mention about the use of the &lt;code class="prettyprint"&gt;RegexParsers&lt;/code&gt; trait is the &lt;code class="prettyprint"&gt;skipWhitespace&lt;/code&gt; method. If it returns &lt;code class="prettyprint"&gt;true&lt;/code&gt; (the default), any &lt;code class="prettyprint"&gt;regex&lt;/code&gt; parser will discard space before any string matching a regular expression. This is convenient most of the time but not here where I need to preserve spaces to be able to count words accurately.&lt;/p&gt;&lt;p&gt;To finish with the subject of Parsing you can have a look at the &lt;a href="http://github.com/etorreborre/wordcount/tree/reactive/src/test/scala/essays/TextParsingSpec"&gt;&lt;code class="prettyprint"&gt;TextParsingSpec&lt;/code&gt; specification&lt;/a&gt;. This specification features a &lt;code class="prettyprint"&gt;ParserMatchers&lt;/code&gt; trait to help with testing your parsers. It also uses the &lt;code class="prettyprint"&gt;Auto-Examples&lt;/code&gt; feature of specs2 to use the text of the example directly as a description:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  &amp;quot;Pages&amp;quot;                                                                           ^&lt;br /&gt;  { page must succeedOn(&amp;quot;p.33&amp;quot;) }                                                   ^&lt;br /&gt;  { page must succeedOn(&amp;quot;p33&amp;quot;) }                                                    ^&lt;br /&gt;  { pages must succeedOn(&amp;quot;pp.215-324&amp;quot;) }                                            ^&lt;br /&gt;  { pages must succeedOn(&amp;quot;pp.215/324&amp;quot;) }                                            ^&lt;br /&gt;  { pages(&amp;quot;pp. 215/324&amp;quot;) must haveSuccessResult(===(Pages(&amp;quot;pp. 215/324&amp;quot;))) }        ^&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;a name="Displaying+the+results"&gt;&lt;/a&gt;&lt;h3&gt;Displaying the results&lt;/h3&gt;&lt;p&gt;The next big chunk of this application is a Swing GUI. The Scala standard distribution provides a &lt;code class="prettyprint"&gt;scala-swing&lt;/code&gt; library adding some syntactic sugar on top of regular Swing components. If you want to read more about Scala and Swing you can have a look at &lt;a href="http://www.slideshare.net/perneto/scalaswing"&gt;this presentation&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;The main components of my application are:&lt;/p&gt;&lt;ul&gt;  &lt;li&gt;a menu bar with 2 buttons to: select a file, do the count&lt;/li&gt;  &lt;li&gt;a field to display the currently selected file&lt;/li&gt;  &lt;li&gt;a results panel showing: the number of counted words and the document references&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;img alt="wordcount application" src="https://lh5.googleusercontent.com/-IFWEyekxBmc/TpNuXQ_wDAI/AAAAAAAAALM/zD7WzHgHZjU/s945/wordscounter.png" /&gt;&lt;br /&gt;&lt;em&gt;count example, note that the parsing is not perfect since the word counts do not add up!&lt;/em&gt;&lt;/p&gt;&lt;p&gt;If you have a look at the code you will see that this translates to:&lt;/p&gt;&lt;ul&gt;  &lt;li&gt;an instance of &lt;code class="prettyprint"&gt;SimpleSwingApplication&lt;/code&gt; defining a &lt;code class="prettyprint"&gt;top&lt;/code&gt; method and including all the embedded components: a menu bar, a panel with the selected file and results&lt;/li&gt;  &lt;li&gt;the subcomponents themselves: the menu items, the count action, the results panel&lt;/li&gt;  &lt;li&gt;the &amp;quot;reactions&amp;quot; which is a &lt;code class="prettyprint"&gt;PartialFunction&lt;/code&gt; listening to the events created by some components, &lt;code class="prettyprint"&gt;SelectionChanged&lt;/code&gt; for example, and triggering the count or displaying the results&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;I was pretty happy to see that much of the verbosity of Swing programming is reduced with Scala:&lt;/p&gt;&lt;ul&gt;  &lt;li&gt;you don't need to create xxxListeners for everything&lt;/li&gt;  &lt;li&gt;there are components providing both a &lt;code class="prettyprint"&gt;Panel&lt;/code&gt; and a &lt;code class="prettyprint"&gt;LayoutManager&lt;/code&gt; with the appropriate syntax to display the components: &lt;code class="prettyprint"&gt;FlowPanel&lt;/code&gt;, &lt;code class="prettyprint"&gt;BoxPanel&lt;/code&gt;, &lt;code class="prettyprint"&gt;BorderPanel&lt;/code&gt;&lt;/li&gt;  &lt;li&gt;thanks to scala syntax you can write &lt;code class="prettyprint"&gt;action = new Action(...)&lt;/code&gt; instead of &lt;code class="prettyprint"&gt;setAction(new Action(...))&lt;/code&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;This is nice but I think that there is a some potential for pushing this way further and create more reusable out-of-the-box components. For example, I've created an &lt;code class="prettyprint"&gt;OpenFileMenuItem&lt;/code&gt; which is a &lt;code class="prettyprint"&gt;MenuItem&lt;/code&gt; with an &lt;code class="prettyprint"&gt;Action&lt;/code&gt; to open a &lt;code class="prettyprint"&gt;FileChooser&lt;/code&gt;. Also, something like a pervasive &lt;code class="prettyprint"&gt;LabeledField&lt;/code&gt; with just a label and some text would very useful to have in a standard library.&lt;/p&gt;&lt;p&gt;I also added a bit of syntactic sugar to have actions executed on a worker thread, instead of the event dispatch thread (to avoid grey screens), using the &lt;code class="prettyprint"&gt;SwingUtilities.invokeLater&lt;/code&gt; method. For example: &lt;code class="prettyprint"&gt;myAction.inBackground&lt;/code&gt; will be executed on a separate thread.&lt;/p&gt;&lt;p&gt;Eventually, I was able to code up the GUI of the application pretty fast. The only thing which I didn't really like was the Publish/React pattern. It felt a bit messy. The next part of this series will show how &lt;a href="http://en.wikipedia.org/wiki/Functional_reactive_programming"&gt;Functional Reactive Programming&lt;/a&gt; with the &lt;a href="http://github.com/nafg/reactive"&gt;reactive&lt;/a&gt; library helped me write cleaner code.&lt;/p&gt;&lt;a name="Reading+the+file"&gt;&lt;/a&gt;&lt;h3&gt;Reading the file&lt;/h3&gt;&lt;p&gt;I anticipated this part to be a tad difficult. My first experiments of text parsing were using a simple text file and I knew that having the user (my wife, remember,...) copy and paste her text to another file just for counting would be a deal-breaker. So I tried to read &lt;code class="prettyprint"&gt;.odt&lt;/code&gt; and &lt;code class="prettyprint"&gt;.docx&lt;/code&gt; files directly. This was actually much &lt;em&gt;easier&lt;/em&gt; than anything I expected!&lt;/p&gt;&lt;p&gt;Both formats are zipped xml files. Getting the content of those files is just a matter of:&lt;/p&gt;&lt;ul&gt;  &lt;li&gt;&lt;p&gt;reading the &lt;code class="prettyprint"&gt;ZipFile&lt;/code&gt; entries and find the file containing the text&lt;/p&gt;  &lt;pre&gt;&lt;code class="prettyprint"&gt;val rootzip = new ZipFile(doc.path)&lt;br /&gt;rootzip.entries.find(_.getName.equals(&amp;quot;word/document.xml&amp;quot;))&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;  &lt;li&gt;&lt;p&gt;loading the xml as a &lt;code class="prettyprint"&gt;NodeSeq&lt;/code&gt;&lt;/p&gt;  &lt;pre&gt;&lt;code class="prettyprint"&gt;XML.load(rootzip.getInputStream(f)))&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;  &lt;li&gt;&lt;p&gt;find the nodes containing the actual text of the document and transform them to text&lt;/p&gt;  &lt;pre&gt;&lt;code class="prettyprint"&gt;// for a Word document text is under &amp;lt;p&amp;gt;&amp;lt;t&amp;gt; tags&lt;br /&gt;(xml \\ &amp;quot;p&amp;quot;) map (p =&amp;gt; (p \\ &amp;quot;t&amp;quot;).map(_.text) mkString &amp;quot;&amp;quot;) mkString &amp;quot;\n&amp;quot;&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;For further details you can read the code &lt;a href="https://github.com/etorreborre/wordcount/blob/master/src/main/scala/docs/DocReader.scala"&gt;here&lt;/a&gt;.&lt;/p&gt;&lt;a name="Recap"&gt;&lt;/a&gt;&lt;h3&gt;Recap&lt;/h3&gt;&lt;p&gt;That's it, parser combinators + scala-swing + xml = a quick app solving a real-world problem. In the next posts we'll try to make this even better!&lt;/p&gt;&lt;/div&gt;&lt;/status&gt;&lt;status class="ok"&gt;&lt;p&gt;&lt;/p&gt;&lt;/status&gt;&lt;status class="ok"&gt;&lt;div class="level0"&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5336273-9093661194415181439?l=etorreborre.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://etorreborre.blogspot.com/feeds/9093661194415181439/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5336273&amp;postID=9093661194415181439' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5336273/posts/default/9093661194415181439'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5336273/posts/default/9093661194415181439'/><link rel='alternate' type='text/html' href='http://etorreborre.blogspot.com/2011/10/counting-words.html' title='Counting words'/><author><name>Eric</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://farm3.static.flickr.com/2203/2118752971_7becaf7476_t.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='https://lh5.googleusercontent.com/-IFWEyekxBmc/TpNuXQ_wDAI/AAAAAAAAALM/zD7WzHgHZjU/s72-c/wordscounter.png' height='72' width='72'/><thr:total>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5336273.post-4815256103882564718</id><published>2011-06-24T16:53:00.018+09:00</published><updated>2011-12-04T08:34:00.251+09:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='functional programming'/><category scheme='http://www.blogger.com/atom/ns#' term='iterator'/><category scheme='http://www.blogger.com/atom/ns#' term='scala'/><title type='text'>The Essence of the Iterator Pattern</title><content type='html'>&lt;p&gt;&lt;a href="http://www.comlab.ox.ac.uk/jeremy.gibbons/publications/iterator.pdf"&gt;"The Essence of the Iterator Pattern"&lt;/a&gt;(EIP) is the paper I liked the most last year. It gave me a brand new look over something which I had be using for years: the &lt;code class="prettyprint"&gt;for&lt;/code&gt; loop.&lt;/p&gt;&lt;p&gt;In this post I'll try to present some ideas of that paper and show how to implement the solution described by the authors using &lt;a href="http://github.com/scalaz/scalaz"&gt;Scalaz&lt;/a&gt;-like code. A minimum previous exposure to functional programming, functors and monads will definitely help!&lt;/p&gt;&lt;a name="What%27s+in+a+for+loop%3F"&gt;&lt;/a&gt;&lt;h2&gt;What's in a for loop?&lt;/h2&gt;&lt;p&gt;That was really what hooked me. What do you mean "what's in a &lt;code class="prettyprint"&gt;for&lt;/code&gt; loop"?. Is there anything magic in that construct I've been using for years?&lt;/p&gt;&lt;p&gt;The introduction of EIP shows an example of a &lt;code class="prettyprint"&gt;for&lt;/code&gt; loop to iterate on elements (not the C-like &lt;code class="prettyprint"&gt;for&lt;/code&gt; used with an index). I'm &lt;a href="http://members.shaw.ca/newsong/calvin.html"&gt;transmogrifying&lt;/a&gt; it here to Scala but the idea remains the same:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  val basket: Basket[Fruit] = Basket(orange, apple)&lt;br /&gt;  var count = 0&lt;br /&gt;&lt;br /&gt;  val juices = Basket[Juice]()&lt;br /&gt;  for (fruit &amp;lt;- basket) {&lt;br /&gt;    count = count + 1&lt;br /&gt;    juices.add(fruit.press)&lt;br /&gt;  }&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;We start from a "container" of fruits: &lt;code class="prettyprint"&gt;Basket&lt;/code&gt;. It could actually be anything, a &lt;code class="prettyprint"&gt;List&lt;/code&gt;, a &lt;code class="prettyprint"&gt;Tree&lt;/code&gt;, a &lt;code class="prettyprint"&gt;Map&lt;/code&gt;... Then the &lt;code class="prettyprint"&gt;for&lt;/code&gt; loop actually does 3 things:&lt;/p&gt;&lt;ol&gt;&lt;li&gt;it returns a container having &lt;em&gt;the same "shape"&lt;/em&gt;; &lt;code class="prettyprint"&gt;juices&lt;/code&gt; is still a &lt;code class="prettyprint"&gt;Basket&lt;/code&gt;&lt;/li&gt;   &lt;li&gt;it &lt;em&gt;accumulates&lt;/em&gt; some kind of measure. Here, the number of fruits in the &lt;code class="prettyprint"&gt;count&lt;/code&gt; variable&lt;/li&gt;   &lt;li&gt;it &lt;em&gt;maps&lt;/em&gt; the elements to other elements: pressing the fruits to get some juice&lt;/li&gt; &lt;/ol&gt;&lt;p&gt;And this &lt;code class="prettyprint"&gt;for&lt;/code&gt; loop is actually not the most complex:&lt;/p&gt; &lt;ul&gt;   &lt;li&gt;the &lt;code class="prettyprint"&gt;count&lt;/code&gt; variable could influence the mapping of elements: &lt;code class="prettyprint"&gt;juices.add(fruit.press(harder=count))&lt;/code&gt;&lt;/li&gt;   &lt;li&gt;we could have several variables depending on each other: &lt;code class="prettyprint"&gt;cumulative = cumulative + count&lt;/code&gt;&lt;/li&gt;   &lt;li&gt;the mapping could also influence a "measure" variable: &lt;code class="prettyprint"&gt;liquid = liquid + fruit.press.quantity&lt;/code&gt;&lt;/li&gt; &lt;/ul&gt;&lt;p&gt;The purpose of EIP is to show that the "essence" of what happens in the &lt;code class="prettyprint"&gt;for&lt;/code&gt; loop above can be abstracted by an &lt;em&gt;Applicative Traversal&lt;/em&gt;. And the authors go on showing that given this &lt;a href="http://github.com/scalaz/scalaz/blob/scalaz7/core/src/main/scala/scalaz/Applicative.scala"&gt;&lt;em&gt;Applicative&lt;/em&gt;&lt;/a&gt; abstraction, we get an incredible modularity for programming.&lt;/p&gt;&lt;a name="The+Applicative+typeclass"&gt;&lt;/a&gt;&lt;h2&gt;The Applicative typeclass&lt;/h2&gt;&lt;p&gt;How can an &lt;em&gt;Applicative traversal&lt;/em&gt; be better than a for loop, and what does that even &lt;em&gt;mean&lt;/em&gt;?? EIP has a lot of sentences and expressions which can be hard to grasp if you don't have a strong functional programming / Haskell background. Let's try to dissect that slowly and start with the formal definitions anyway.&lt;/p&gt;&lt;a name="What+is+a+Functor%3F"&gt;&lt;/a&gt;&lt;h3&gt;What is a Functor?&lt;/h3&gt;&lt;p&gt;The first thing we need to talk about is the &lt;code class="prettyprint"&gt;Functor&lt;/code&gt; typeclass:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  trait Functor[F[_]] {&lt;br /&gt;    def fmap[A, B](f: A =&amp;gt; B): F[A] =&amp;gt; F[B]&lt;br /&gt;  }&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;One way of interpreting a &lt;code class="prettyprint"&gt;Functor&lt;/code&gt; is to describe it as a computation of values of type &lt;code class="prettyprint"&gt;A&lt;/code&gt;. For example &lt;code class="prettyprint"&gt;List[A]&lt;/code&gt; is a computation returning several values of type &lt;code class="prettyprint"&gt;A&lt;/code&gt; (non-deterministic computation), &lt;code class="prettyprint"&gt;Option[A]&lt;/code&gt; is for computations that you may or may not have, &lt;code class="prettyprint"&gt;Future[A]&lt;/code&gt; is a computation of a value of type &lt;code class="prettyprint"&gt;A&lt;/code&gt; that you will get later, and so on. Another way of picturing it is as some kind of "container" for values of type &lt;code class="prettyprint"&gt;A&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;Saying that those computations are &lt;code class="prettyprint"&gt;Functors&lt;/code&gt; is essentially showing that we can have a very useful way of combining them with regular functions. We can apply a function to the value that is being computed. Given a value &lt;code class="prettyprint"&gt;F[A]&lt;/code&gt; and a function &lt;code class="prettyprint"&gt;f&lt;/code&gt;, we can apply that function to the value with &lt;code class="prettyprint"&gt;fmap&lt;/code&gt;. For example, &lt;code class="prettyprint"&gt;fmap&lt;/code&gt; is a simple &lt;code class="prettyprint"&gt;map&lt;/code&gt; for a &lt;code class="prettyprint"&gt;List&lt;/code&gt; or an &lt;code class="prettyprint"&gt;Option&lt;/code&gt;.&lt;/p&gt;&lt;a name="Pointed+Functor"&gt;&lt;/a&gt;&lt;h3&gt;Pointed Functor&lt;/h3&gt;&lt;p&gt;By the way, how do you even &lt;em&gt;create&lt;/em&gt; a value of type &lt;code class="prettyprint"&gt;F[A]&lt;/code&gt;? One way to do that is to say that &lt;code class="prettyprint"&gt;F[_]&lt;/code&gt; is &lt;code class="prettyprint"&gt;Pointed&lt;/code&gt;:&lt;/p&gt; &lt;pre&gt;&lt;code class="prettyprint"&gt;  trait Pointed[F[_]] {&lt;br /&gt;    def point[A](a: =&amp;gt; A): F[A]&lt;br /&gt;  }&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;That is, there is a &lt;code class="prettyprint"&gt;point&lt;/code&gt; function taking a value of type &lt;code class="prettyprint"&gt;A&lt;/code&gt; and returning a &lt;code class="prettyprint"&gt;F[A]&lt;/code&gt;. For example, a regular &lt;code class="prettyprint"&gt;List&lt;/code&gt; is &lt;code class="prettyprint"&gt;Pointed&lt;/code&gt; just by using the constructor for Lists:&lt;/p&gt; &lt;pre&gt;&lt;code class="prettyprint"&gt;  object PointedList extends Pointed[List] {&lt;br /&gt;    def point[A](a: =&amp;gt; A) = List(a)&lt;br /&gt;  }&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Then combining the 2 capabilities, pointed and functor, gives you a &lt;code class="prettyprint"&gt;PointedFunctor&lt;/code&gt;:&lt;/p&gt; &lt;pre&gt;&lt;code class="prettyprint"&gt;  trait PointedFunctor[F[_]] {&lt;br /&gt;    val functor: Functor[F]&lt;br /&gt;    val pointed: Pointed[F]&lt;br /&gt;&lt;br /&gt;    def point[A](a: =&amp;gt; A): F[A] = pointed.point(a)&lt;br /&gt;&lt;br /&gt;    def fmap[A, B](f: A =&amp;gt; B): F[A] =&amp;gt; F[B] = functor.fmap(f)&lt;br /&gt;  }&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The &lt;code class="prettyprint"&gt;PointedFunctor&lt;/code&gt; trait is merely the aggregation of a &lt;code class="prettyprint"&gt;Pointed&lt;/code&gt; and a &lt;code class="prettyprint"&gt;Functor&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;What about &lt;code class="prettyprint"&gt;Applicative&lt;/code&gt; then? We're getting to it, the last missing piece is &lt;code class="prettyprint"&gt;Applic&lt;/code&gt;.&lt;/p&gt;&lt;a name="Applic"&gt;&lt;/a&gt;&lt;h3&gt;Applic&lt;/h3&gt;&lt;p&gt;&lt;code class="prettyprint"&gt;Applic&lt;/code&gt; is another way to combine a "container" with a function.&lt;/p&gt;&lt;p&gt;Instead of using &lt;code class="prettyprint"&gt;fmap&lt;/code&gt; to apply the function to the computed value we suppose that the function is itself a computed value &lt;em&gt;inside the container &lt;code class="prettyprint"&gt;F&lt;/code&gt;&lt;/em&gt; (&lt;code class="prettyprint"&gt;F[A =&amp;gt; B]&lt;/code&gt;) and we provide a method &lt;code class="prettyprint"&gt;applic&lt;/code&gt; to apply that function to a value &lt;code class="prettyprint"&gt;F[A]&lt;/code&gt;:&lt;/p&gt; &lt;pre&gt;&lt;code class="prettyprint"&gt;  trait Applic[F[_]] {&lt;br /&gt;    def applic[A, B](f: F[A =&amp;gt; B]): F[A] =&amp;gt; F[B]&lt;br /&gt;  }&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Let's take an example. Say I have a way to compute the price of a &lt;code class="prettyprint"&gt;Fruit&lt;/code&gt; when the market is open:&lt;/p&gt; &lt;pre&gt;&lt;code class="prettyprint"&gt;  def pricer(market: Market): Option[Fruit =&amp;gt; Double]&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;If the market is closed, &lt;code class="prettyprint"&gt;pricer&lt;/code&gt; returns &lt;code class="prettyprint"&gt;None&lt;/code&gt;, because we don't know what are the prices. Otherwise it returns a pricing function. Now if I have a &lt;code class="prettyprint"&gt;grow&lt;/code&gt; function possibly returning a &lt;code class="prettyprint"&gt;Fruit&lt;/code&gt;:&lt;/p&gt; &lt;pre&gt;&lt;code class="prettyprint"&gt;  def grow: Option[Fruit]&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Then, using the &lt;code class="prettyprint"&gt;Applic&lt;/code&gt; instance, you can price the &lt;code class="prettyprint"&gt;Fruit&lt;/code&gt;:&lt;/p&gt; &lt;pre&gt;&lt;code class="prettyprint"&gt;  val price: Option[Double] = applic(pricer(market)).apply(grow)&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The &lt;code class="prettyprint"&gt;price&lt;/code&gt; will necessarily be an &lt;code class="prettyprint"&gt;Option&lt;/code&gt; because you may not have a pricer nor a &lt;code class="prettyprint"&gt;Fruit&lt;/code&gt; to price. And a bit of renaming and &lt;a href="http://www.artima.com/weblogs/viewpost.jsp?thread=179766"&gt;pimping&lt;/a&gt; reveals why we're using the term "Applicative":&lt;/p&gt; &lt;pre&gt;&lt;code class="prettyprint"&gt;  val pricingFunction = pricer(market)&lt;br /&gt;  val fruit = grow&lt;br /&gt;&lt;br /&gt;  val price: Option[Double] = pricingFunction ⊛ fruit&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;In a way we're just doing a normal function application, but we're just doing it &lt;em&gt;inside&lt;/em&gt; the Applicative container. Now we have all the pieces to build the &lt;code class="prettyprint"&gt;Applicative&lt;/code&gt; functor that EIP is talking about.&lt;/p&gt;&lt;a name="Applicative+Functor"&gt;&lt;/a&gt;&lt;h3&gt;Applicative Functor&lt;/h3&gt;&lt;p&gt;An &lt;code class="prettyprint"&gt;Applicative Functor&lt;/code&gt; is the aggregation of an &lt;code class="prettyprint"&gt;Applic&lt;/code&gt; and a &lt;code class="prettyprint"&gt;PointedFunctor&lt;/code&gt;:&lt;/p&gt; &lt;pre&gt;&lt;code class="prettyprint"&gt;  trait Applicative[F[_]] {&lt;br /&gt;    val pointedFunctor: PointedFunctor[F]&lt;br /&gt;    val applic: Applic[F]&lt;br /&gt;&lt;br /&gt;    def functor: Functor[F] = new Functor[F] { &lt;br /&gt;      def fmap[A, B](f: A =&amp;gt; B) = pointedFunctor fmap f &lt;br /&gt;    }&lt;br /&gt;    def pointed: Pointed[F] = new Pointed[F] { &lt;br /&gt;      def point[A](a: =&amp;gt; A) = pointedFunctor point a &lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    def fmap[A, B](f: A =&amp;gt; B): F[A] =&amp;gt; F[B]     = functor.fmap(f)&lt;br /&gt;    def point[A](a: =&amp;gt; A): F[A]                 = pointed.point(a)&lt;br /&gt;    def apply[A, B](f: F[A =&amp;gt; B]): F[A] =&amp;gt; F[B] = applic.applic(f)&lt;br /&gt;  }&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Let's see how that can be implemented for a &lt;code class="prettyprint"&gt;List&lt;/code&gt;. &lt;code class="prettyprint"&gt;fmap&lt;/code&gt; and &lt;code class="prettyprint"&gt;point&lt;/code&gt; are straightforward:&lt;/p&gt; &lt;pre&gt;&lt;code class="prettyprint"&gt;   def fmap[A, B](f: A =&amp;gt; B): F[A] =&amp;gt; F[B] = (l: List[A]) =&amp;gt; l map f&lt;br /&gt;   def point[A](a: =&amp;gt; A): F[A]             = List(a)&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;code class="prettyprint"&gt;apply&lt;/code&gt; turns out to be more interesting because there are 2 ways to implement it, both of them being useful:&lt;/p&gt; &lt;ol&gt;   &lt;li&gt;&lt;p&gt;apply a list of functions to each element and gather the results in a List:&lt;/p&gt;   &lt;pre&gt;&lt;code class="prettyprint"&gt;def apply[A, B](f: F[A =&amp;gt; B]): F[A] =&amp;gt; F[B] = (l: List[A]) =&amp;gt;&lt;br /&gt; for { a &amp;lt;- l; func &amp;lt;- f } yield func(a)&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;   &lt;li&gt;&lt;p&gt;&lt;code class="prettyprint"&gt;zip&lt;/code&gt; the list of functions to the list of elements to apply each function to each element&lt;/p&gt;   &lt;pre&gt;&lt;code class="prettyprint"&gt;def apply[A, B](f: F[A =&amp;gt; B]): F[A] =&amp;gt; F[B] = (l: List[A]) =&amp;gt;&lt;br /&gt; (l zip f) map (p =&amp;gt; p._2 apply p._1)&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt; &lt;/ol&gt;&lt;p&gt;There is even a third way to use &lt;code class="prettyprint"&gt;List&lt;/code&gt; as an &lt;code class="prettyprint"&gt;Applicative&lt;/code&gt; by using the fact that &lt;code class="prettyprint"&gt;List&lt;/code&gt; is a &lt;code class="prettyprint"&gt;Monoid&lt;/code&gt;. But more on that later, for now we still have to see how all of this relates to the &lt;code class="prettyprint"&gt;for&lt;/code&gt; loop...&lt;/p&gt;&lt;a name="Traversing+the+structure"&gt;&lt;/a&gt;&lt;h2&gt;Traversing the structure&lt;/h2&gt;&lt;p&gt;When we do a &lt;code class="prettyprint"&gt;for&lt;/code&gt; loop, we take a "structure" containing some elements and we "traverse" it to return:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;that same structure containing other elements&lt;/li&gt;   &lt;li&gt;a value computed from the structure elements&lt;/li&gt;   &lt;li&gt;some combination of above&lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;&lt;p&gt;Gibbons &amp;amp; Oliveira argue that &lt;em&gt;any kind of for loop&lt;/em&gt; can be represented as the following &lt;code class="prettyprint"&gt;traverse&lt;/code&gt; operation:&lt;/p&gt; &lt;pre&gt;&lt;code class="prettyprint"&gt;  trait Traversable[T[_]] {&lt;br /&gt;    def traverse[F[_] : Applicative, A, B](f: A =&amp;gt; F[B]): T[A] =&amp;gt; F[T[B]]&lt;br /&gt;  }&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;That is, if the container/structure of type &lt;code class="prettyprint"&gt;T&lt;/code&gt; has this &lt;code class="prettyprint"&gt;traverse&lt;/code&gt; function using an &lt;code class="prettyprint"&gt;Applicative F&lt;/code&gt;, then we can do whatever we would do with a &lt;code class="prettyprint"&gt;for&lt;/code&gt; loop on it.&lt;/p&gt;&lt;p&gt;To get a better feel for this &lt;code class="prettyprint"&gt;traverse&lt;/code&gt; function, we're going to implement the &lt;code class="prettyprint"&gt;Traversable&lt;/code&gt; trait for a binary tree and then we'll see how can we &lt;code class="prettyprint"&gt;loop&lt;/code&gt; on that tree.&lt;/p&gt;&lt;a name="A+Binary+Tree"&gt;&lt;/a&gt;&lt;h3&gt;A Binary Tree&lt;/h3&gt;&lt;p&gt;For all the other examples in this post, we're going to use a very simple binary tree:&lt;/p&gt; &lt;pre&gt;&lt;code class="prettyprint"&gt;  sealed trait BinaryTree[A]&lt;br /&gt;  case class Leaf[A](a: A) extends BinaryTree[A]&lt;br /&gt;  case class Bin[A](left: BinaryTree[A], right: BinaryTree[A]) extends BinaryTree[A]&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;On the other hand, the first shot at the &lt;code class="prettyprint"&gt;Traversable&lt;/code&gt; implementation is barely readable!&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt; def BinaryTreeIsTraversable[A]: Traversable[BinaryTree] = new Traversable[BinaryTree] {&lt;br /&gt;&lt;br /&gt;   def createLeaf[B] = (n: B) =&amp;gt; (Leaf(n): (BinaryTree[B]))&lt;br /&gt;   def createBin[B]  = (nl: BinaryTree[B]) =&amp;gt; &lt;br /&gt;     (nr: BinaryTree[B]) =&amp;gt; (Bin(nl, nr): BinaryTree[B])&lt;br /&gt;&lt;br /&gt;   def traverse[F[_] : Applicative, A, B](f: A =&amp;gt; F[B]): &lt;br /&gt;     BinaryTree[A] =&amp;gt; F[BinaryTree[B]] = (t: BinaryTree[A]) =&amp;gt; {&lt;br /&gt;     val applicative = implicitly[Applicative[F]]&lt;br /&gt;     t match {&lt;br /&gt;       case Leaf(a)   =&amp;gt; applicative.apply(applicative.point(createLeaf[B]))(f(a))&lt;br /&gt;       case Bin(l, r) =&amp;gt;&lt;br /&gt;         applicative.apply(applicative.apply(applicative.point(createBin[B]))(traverse[F, A, B](f).apply(l))).&lt;br /&gt;         apply(traverse[F, A, B](f).apply(r))&lt;br /&gt;     }&lt;br /&gt;   }&lt;br /&gt; }&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This is a shame because the corresponding Haskell code is so concise:&lt;/p&gt; &lt;pre&gt;&lt;code class="prettyprint"&gt;  instance Traversable Tree where&lt;br /&gt;  traverse f (Leaf x)  = pure Leaf ⊛ f x&lt;br /&gt;  traverse f (Bin t u) = pure Bin  ⊛ traverse f t ⊛ traverse f u&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;A bit of &lt;a href="http://www.artima.com/weblogs/viewpost.jsp?thread=179766"&gt;pimping&lt;/a&gt; to the rescue and we can improve the situation:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;def traverse[F[_] : Applicative, A, B](f: A =&amp;gt; F[B]): BinaryTree[A] =&amp;gt; F[BinaryTree[B]] = (t: BinaryTree[A]) =&amp;gt; {&lt;br /&gt;  t match {&lt;br /&gt;    case Leaf(a)   =&amp;gt; createLeaf[B] ∘ f(a)&lt;br /&gt;    case Bin(l, r) =&amp;gt; createBin[B]  ∘ (l traverse f) &amp;lt;*&amp;gt; (r traverse f)&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Informally the &lt;code class="prettyprint"&gt;traverse&lt;/code&gt; method applies the function &lt;code class="prettyprint"&gt;f&lt;/code&gt; to each node and "reconstructs" the tree by using the &lt;code class="prettyprint"&gt;apply&lt;/code&gt; method (&lt;code class="prettyprint"&gt;&amp;lt;*&amp;gt;&lt;/code&gt;) of the &lt;code class="prettyprint"&gt;Applicative&lt;/code&gt; functor.&lt;/p&gt;&lt;p&gt;That's certainly still some Ancient Chinese to you (as it was for me) so we'd better see the &lt;code class="prettyprint"&gt;traverse&lt;/code&gt; method at work now. But we need to take another detour :-)&lt;/p&gt;&lt;a name="Applicative+Monoid"&gt;&lt;/a&gt;&lt;h3&gt;Applicative Monoid&lt;/h3&gt;&lt;p&gt;One simple thing we might want to do, when iterating on a &lt;code class="prettyprint"&gt;BinaryTree&lt;/code&gt;, is to get the content of that tree in a &lt;code class="prettyprint"&gt;List&lt;/code&gt;. To do that, we're going to use the 3rd way to use &lt;code class="prettyprint"&gt;List&lt;/code&gt; as an &lt;code class="prettyprint"&gt;Applicative&lt;/code&gt;, as mentioned earlier. It turns out indeed that any &lt;a href="http://learnyouahaskell.com/functors-applicative-functors-and-monoids"&gt;&lt;code class="prettyprint"&gt;Monoid&lt;/code&gt;&lt;/a&gt; gives rise to an &lt;code class="prettyprint"&gt;Applicative&lt;/code&gt; instance but in a way that's a bit surprising.&lt;/p&gt; &lt;pre&gt;&lt;code class="prettyprint"&gt;  /** Const is a container for values of type A, with a "phantom" type B */&lt;br /&gt; case class Const[A, +B](value: A)&lt;br /&gt;&lt;br /&gt; implicit def ConstIsPointed[M : Monoid] = new Pointed[({type l[A]=Const[M, A]})#l] {&lt;br /&gt;   def point[A](a: =&amp;gt; A) = Const[M, A](implicitly[Monoid[M]].z)&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; implicit def ConstIsFunctor[M : Monoid] = new Functor[({type l[A]=Const[M, A]})#l] {&lt;br /&gt;   def fmap[A, B](f: A =&amp;gt; B) = (c: Const[M, A]) =&amp;gt; Const[M, B](c.value)&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; implicit def ConstIsApplic[M : Monoid] = new Applic[({type l[A]=Const[M, A]})#l] {&lt;br /&gt;   def applic[A, B](f: Const[M, A =&amp;gt; B]) = (c: Const[M, A]) =&amp;gt; Const[M, B](implicitly[Monoid[M]].append(f.value, c.value))&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; implicit def ConstIsPointedFunctor[M : Monoid] = new PointedFunctor[({type l[A]=Const[M, A]})#l] {&lt;br /&gt;   val functor = Functor.ConstIsFunctor&lt;br /&gt;   val pointed = Pointed.ConstIsPointed&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; implicit def ConstIsApplicative[M : Monoid] = new Applicative[({type l[A]=Const[M, A]})#l] {&lt;br /&gt;   val pointedFunctor = PointedFunctor.ConstIsPointedFunctor&lt;br /&gt;   val applic = Applic.ConstIsApplic&lt;br /&gt; }&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;In the code above, &lt;code class="prettyprint"&gt;Const&lt;/code&gt; is the &lt;code class="prettyprint"&gt;Applicative&lt;/code&gt; instance for a given &lt;code class="prettyprint"&gt;Monoid&lt;/code&gt;. &lt;code class="prettyprint"&gt;Const&lt;/code&gt; contains values of type &lt;code class="prettyprint"&gt;T&lt;/code&gt; where &lt;code class="prettyprint"&gt;T&lt;/code&gt; is a &lt;code class="prettyprint"&gt;Monoid&lt;/code&gt; and we progressively establish what are the properties that &lt;code class="prettyprint"&gt;Const&lt;/code&gt; must satisfy to be &lt;code class="prettyprint"&gt;Applicative&lt;/code&gt;.&lt;/p&gt; &lt;ul&gt;   &lt;li&gt;&lt;p&gt;it must first be &lt;code class="prettyprint"&gt;Pointed&lt;/code&gt;. Informally, the &lt;code class="prettyprint"&gt;point&lt;/code&gt; method puts the neutral element of the &lt;code class="prettyprint"&gt;Monoid&lt;/code&gt; in a &lt;code class="prettyprint"&gt;Const&lt;/code&gt; instance&lt;/p&gt;&lt;/li&gt; &lt;li&gt;&lt;p&gt;then it must be a &lt;code class="prettyprint"&gt;Functor&lt;/code&gt;. Here the &lt;code class="prettyprint"&gt;fmap&lt;/code&gt; function doesn't do anything but changing the type of &lt;code class="prettyprint"&gt;Const&lt;/code&gt; from &lt;code class="prettyprint"&gt;Const[M, A]&lt;/code&gt; to &lt;code class="prettyprint"&gt;Const[M, B]&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;   &lt;li&gt;&lt;p&gt;finally it must be an &lt;code class="prettyprint"&gt;Applic&lt;/code&gt; where the &lt;code class="prettyprint"&gt;apply&lt;/code&gt; method of &lt;code class="prettyprint"&gt;Applic&lt;/code&gt; uses the &lt;code class="prettyprint"&gt;append&lt;/code&gt; method of the &lt;code class="prettyprint"&gt;Monoid&lt;/code&gt; to "add" 2 values and return the result in a &lt;code class="prettyprint"&gt;Const&lt;/code&gt; instance.&lt;/p&gt;&lt;/li&gt; &lt;/ul&gt;&lt;p&gt;There is unfortunately a lot of typing vodoo thing here:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;&lt;p&gt;the type declaration for &lt;code class="prettyprint"&gt;Const&lt;/code&gt; is &lt;code class="prettyprint"&gt;Const[A, +B]&lt;/code&gt;. It has a type parameter &lt;code class="prettyprint"&gt;B&lt;/code&gt; which is actually not represented by a value in the &lt;code class="prettyprint"&gt;Const&lt;/code&gt; class! It is a &lt;em&gt;phantom type&lt;/em&gt;. But it is actually indispensable to match the type declarations of the typeclasses&lt;/p&gt;&lt;/li&gt;   &lt;li&gt;&lt;p&gt;the type &lt;code class="prettyprint"&gt;F&lt;/code&gt; that is supposed to be &lt;code class="prettyprint"&gt;Applicative&lt;/code&gt; is... &lt;code class="prettyprint"&gt;({type l[A] = Const[T, A]})#l&lt;/code&gt;. Ouch, this deserves some explanation!&lt;/p&gt;&lt;/li&gt; &lt;/ul&gt;&lt;p&gt;What we want is not so hard. The type &lt;code class="prettyprint"&gt;Const[A, B]&lt;/code&gt; has 2 type parameters. We need a way to &lt;em&gt;fix&lt;/em&gt; &lt;code class="prettyprint"&gt;A&lt;/code&gt; to be &lt;code class="prettyprint"&gt;T&lt;/code&gt; and get the resulting type which will have only one type parameter. The expression above is the most concise way to get this desired type:&lt;/p&gt; &lt;ul&gt;   &lt;li&gt;&lt;p&gt;&lt;code class="prettyprint"&gt;{ type l = SomeType }&lt;/code&gt; is an anonymous type with a type member called &lt;code class="prettyprint"&gt;l&lt;/code&gt;. We can access that type &lt;code class="prettyprint"&gt;l&lt;/code&gt; in Scala by using &lt;code class="prettyprint"&gt;#&lt;/code&gt;: &lt;code class="prettyprint"&gt;{ type l = SomeType }#l&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;   &lt;li&gt;&lt;p&gt;Then, in &lt;code class="prettyprint"&gt;{ type l[A] = SomeType[T, A] }#l&lt;/code&gt;, &lt;code class="prettyprint"&gt;l&lt;/code&gt; is a &lt;em&gt;higher-kinded type&lt;/em&gt;, having a type variable &lt;code class="prettyprint"&gt;A&lt;/code&gt; (actually &lt;code class="prettyprint"&gt;SomeType[T, A]&lt;/code&gt; where &lt;code class="prettyprint"&gt;T&lt;/code&gt; is fixed)&lt;/p&gt;&lt;/li&gt; &lt;/ul&gt;&lt;p&gt;That was a really long detour for a mere &lt;code class="prettyprint"&gt;for&lt;/code&gt; loop, isn't it? Now... profit!&lt;/p&gt;&lt;br /&gt;&lt;a name="Contents+of+a+BinaryTree..."&gt;&lt;/a&gt;&lt;h3&gt;Contents of a BinaryTree...&lt;/h3&gt;&lt;p&gt;We're going to use the &lt;code class="prettyprint"&gt;Traversable&lt;/code&gt; instance for the &lt;code class="prettyprint"&gt;BinaryTree&lt;/code&gt; and the &lt;code class="prettyprint"&gt;List Monoid Applicative&lt;/code&gt; to get the contents of a &lt;code class="prettyprint"&gt;BinaryTree&lt;/code&gt;:&lt;/p&gt; &lt;pre&gt;&lt;code class="prettyprint"&gt;  import Applicative._&lt;br /&gt;&lt;br /&gt;  val f    = (i: Int) =&amp;gt; List(i)&lt;br /&gt;  val tree = Bin(Leaf(1), Leaf(2))&lt;br /&gt;&lt;br /&gt;  (tree.traverse[...](f)).value must_== List(1, 2)&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Simple, for each element of the tree, we put it in a &lt;code class="prettyprint"&gt;List&lt;/code&gt; then we let the &lt;code class="prettyprint"&gt;List Monoid&lt;/code&gt; do its magic and aggregate all the results as we traverse the tree. The only difficulty here is the limits of Scala type inference. The &lt;code class="prettyprint"&gt;...&lt;/code&gt; stands for type annotations that the compiler requires:&lt;/p&gt; &lt;pre&gt;&lt;code class="prettyprint"&gt;  tree.traverse[Int, ({type l[A]=Const[List[Int], A]})#l](f)&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Not pretty :-(&lt;/p&gt;&lt;i&gt;Update&lt;/i&gt;: As pointed out by Ittay Dror in the comments, &lt;code class="prettyprint"&gt;List[Int]&lt;/code&gt; is not an applicative by itself and we need to put this list into a &lt;code class="prettyprint"&gt;Const&lt;/code&gt; value to make it usable by the &lt;code class="prettyprint"&gt;traverse&lt;/code&gt; function. &lt;br /&gt;&lt;br /&gt;This is actually done by an implicit conversion method, &lt;code class="prettyprint"&gt;liftConst&lt;/code&gt;, provided by the &lt;code class="prettyprint"&gt;Applicative object&lt;/code&gt;:&lt;pre&gt;&lt;code class="prettyprint"&gt;  implicit def liftConst[A, B, M : Monoid](f: A =&gt; M): A =&gt; Const[M, B] = &lt;br /&gt;     (a: A) =&gt; Const[M, B](f(a))&lt;/code&gt;&lt;/pre&gt;&lt;a name="Profit+time"&gt;&lt;/a&gt;&lt;h4&gt;Profit time&lt;/h4&gt;&lt;p&gt;Not everything is lost! We can encapsulate a bit the complexity in this case. We can extract part of the code above and create a &lt;code class="prettyprint"&gt;contents&lt;/code&gt; method which will work on &lt;em&gt;any&lt;/em&gt; of &lt;code class="prettyprint"&gt;Traversable&lt;/code&gt; instance (assume I'm pimping the following examples so that I can write &lt;code class="prettyprint"&gt;tree.method&lt;/code&gt; instead of &lt;code class="prettyprint"&gt;method(tree)&lt;/code&gt;):&lt;/p&gt; &lt;pre&gt;&lt;code class="prettyprint"&gt;  val tree: BinaryTree[Int] = Bin(Leaf(1), Leaf(2))&lt;br /&gt;  tree.contents must_== List(1, 2)&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This is based on the following definition:&lt;/p&gt; &lt;pre&gt;&lt;code class="prettyprint"&gt;  def contents[A]: T[A] =&amp;gt; List[A] = {&lt;br /&gt;    val f = (a: A) =&amp;gt; Const[List[A], Any](List(a))&lt;br /&gt;    (ta: T[A]) =&amp;gt; traverse[({type l[U]=Const[List[A], U]})#l, A, Any](f).apply(ta).value&lt;br /&gt;  }&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;It also turns out that the &lt;code class="prettyprint"&gt;contents&lt;/code&gt; function is a specialized version of something even more generic, the &lt;code class="prettyprint"&gt;reduce&lt;/code&gt; function, working with any &lt;code class="prettyprint"&gt;Monoid&lt;/code&gt;:&lt;/p&gt; &lt;pre&gt;&lt;code class="prettyprint"&gt;  def contents[A]: T[A] =&amp;gt; List[A] = reduce((a: A) =&amp;gt; List(a))&lt;br /&gt;&lt;br /&gt;  def reduce[A, M : Monoid](reducer: A =&amp;gt; M): T[A] =&amp;gt; M = {&lt;br /&gt;    val f = (a: A) =&amp;gt; Const[M, Any](reducer(a))&lt;br /&gt;    (ta: T[A]) =&amp;gt; traverse[({type l[A]=Const[M, A]})#l, A, Any](f).apply(ta).value&lt;br /&gt;  }&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The &lt;code class="prettyprint"&gt;reduce&lt;/code&gt; function can traverse any &lt;code class="prettyprint"&gt;Traversable&lt;/code&gt; structure with a function mapping each element to a &lt;code class="prettyprint"&gt;Monoid&lt;/code&gt; element. We've used it to get the contents of the tree but can as easily get the number of elements:&lt;/p&gt; &lt;pre&gt;&lt;code class="prettyprint"&gt;  def count[A]: T[A] =&amp;gt; Int = reduce((a: A) =&amp;gt; 1)&lt;br /&gt;&lt;br /&gt;  tree.count must_== 2&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Can it get simpler than this :-)? Actually in that case it can! Since we don't need &lt;code class="prettyprint"&gt;(a: A)&lt;/code&gt; at all we can use &lt;code class="prettyprint"&gt;reduceConst&lt;/code&gt;:&lt;/p&gt; &lt;pre&gt;&lt;code class="prettyprint"&gt;  def reduceConst[A, M : Monoid](m: M): T[A] =&amp;gt; M = reduce((a: A) =&amp;gt; m)&lt;br /&gt;&lt;br /&gt;  def count[A]: T[A] =&amp;gt; Int = reduceConst(1)&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;It's like a Scala standard &lt;code class="prettyprint"&gt;reduce&lt;/code&gt; on steroids because instead you don't need to provide a binary operation, you just need a &lt;code class="prettyprint"&gt;Monoid&lt;/code&gt; instance.&lt;/p&gt;&lt;a name="....+and+shape+of+a+BinaryTree"&gt;&lt;/a&gt;&lt;h3&gt;.... and shape of a BinaryTree&lt;/h3&gt;&lt;p&gt;We've addressed the question of doing some kind of accumulation based on the elements in the tree, now we're going to "map" them.&lt;/p&gt;&lt;a name="Monads+are+Applicatives+too%21"&gt;&lt;/a&gt;&lt;h4&gt;Monads are Applicatives too!&lt;/h4&gt;&lt;p&gt;The following &lt;code class="prettyprint"&gt;map&lt;/code&gt; method can be derived from the &lt;code class="prettyprint"&gt;traverse&lt;/code&gt; method (note that no type annotations are necessary in that case, yes!):&lt;/p&gt; &lt;pre&gt;&lt;code class="prettyprint"&gt;  def map[A, B](mapper: A =&amp;gt; B) = (ta: T[A]) =&amp;gt; traverse((a: A) =&amp;gt; Ident(mapper(a))).apply(ta).value&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Here we're traversing with an &lt;code class="prettyprint"&gt;Applicative&lt;/code&gt; which is very simple, the &lt;code class="prettyprint"&gt;Ident&lt;/code&gt; class:&lt;/p&gt; &lt;pre&gt;&lt;code class="prettyprint"&gt;  case class Ident[A](value: A)&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The &lt;code class="prettyprint"&gt;Ident&lt;/code&gt; class is a simple wrapper around a value, nothing more. That simple class is an &lt;code class="prettyprint"&gt;Applicative&lt;/code&gt;. But how?&lt;/p&gt;&lt;p&gt;Easy. &lt;code class="prettyprint"&gt;Ident&lt;/code&gt; is actually a &lt;code class="prettyprint"&gt;Monad&lt;/code&gt; and we can construct an &lt;code class="prettyprint"&gt;Applicative&lt;/code&gt; instance from &lt;em&gt;every Monad&lt;/em&gt;. This comes from the fact that a &lt;code class="prettyprint"&gt;Monad&lt;/code&gt; is both a &lt;code class="prettyprint"&gt;PointedFunctor&lt;/code&gt; and an &lt;code class="prettyprint"&gt;Applic&lt;/code&gt;:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  trait Monad[F[_]] {&lt;br /&gt;    val pointed: Pointed[F]&lt;br /&gt;    val bind: Bind[F]&lt;br /&gt;&lt;br /&gt;    def functor: Functor[F] = new Functor[F] {&lt;br /&gt;      def fmap[A, B](f: A =&amp;gt; B): F[A] =&amp;gt; F[B] = (fa: F[A]) =&amp;gt; &lt;br /&gt;        bind.bind((a: A) =&amp;gt; pointed.point(f(a))).apply(fa)&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    def pointedFunctor: PointedFunctor[F] = new PointedFunctor[F] {&lt;br /&gt;      val functor = Monad.this.functor&lt;br /&gt;      val pointed = Monad.this.pointed&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    def applic: Applic[F] = new Applic[F] {&lt;br /&gt;      def applic[A, B](f: F[A =&amp;gt; B]) = a =&amp;gt; &lt;br /&gt;        bind.bind[A =&amp;gt; B, B](ff =&amp;gt; functor.fmap(ff)(a))(f)&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    def applicative: Applicative[F] = new Applicative[F] {&lt;br /&gt;      val pointedFunctor = Monad.this.pointedFunctor&lt;br /&gt;      val applic = Monad.this.applic&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;And the &lt;code class="prettyprint"&gt;Ident&lt;/code&gt; class is trivially a &lt;code class="prettyprint"&gt;Monad&lt;/code&gt; (having a &lt;code class="prettyprint"&gt;pointed&lt;/code&gt; and a &lt;code class="prettyprint"&gt;bind&lt;/code&gt; member):&lt;/p&gt; &lt;pre&gt;&lt;code class="prettyprint"&gt;  implicit def IdentIsMonad = new Monad[Ident] {&lt;br /&gt;&lt;br /&gt;    val pointed = new Pointed[Ident] {&lt;br /&gt;      def point[A](a: =&amp;gt; A): Ident[A] = Ident(a)&lt;br /&gt;    }&lt;br /&gt;    val bind = new Bind[Ident] {&lt;br /&gt;      def bind[A, B](f: A =&amp;gt; Ident[B]): Ident[A] =&amp;gt; Ident[B] = &lt;br /&gt;        (i: Ident[A]) =&amp;gt; f(i.value)&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;  }&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;We can use our brand new &lt;code class="prettyprint"&gt;map&lt;/code&gt; function now:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  tree.map((i: Int) =&amp;gt; i.toString) must_== Bin(Leaf("1"), Leaf("2"))&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;We can even use it to get the "shape" of our container and discard all the elements:&lt;/p&gt; &lt;pre&gt;&lt;code class="prettyprint"&gt;  tree.shape must_== Bin(Leaf(()), Leaf(()))&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The &lt;code class="prettyprint"&gt;shape&lt;/code&gt; method just maps each element to &lt;code class="prettyprint"&gt;()&lt;/code&gt;.&lt;/p&gt;&lt;a name="Decompose+%2F+Compose"&gt;&lt;/a&gt;&lt;h3&gt;Decompose / Compose&lt;/h3&gt;&lt;p&gt;I recap. We implemented a &lt;em&gt;very&lt;/em&gt; generic way to iterate over a structure, &lt;em&gt;any kind of structure&lt;/em&gt; (as long as it's &lt;code class="prettyprint"&gt;Traversable&lt;/code&gt;), containing elements, &lt;em&gt;any kind of element&lt;/em&gt;, with a function which does an "application", &lt;em&gt;any kind of application&lt;/em&gt;. Among the possible "applications", we've seen 2 examples: collecting and mapping which are the essential operations that we usually do in a &lt;code class="prettyprint"&gt;for&lt;/code&gt; loop.&lt;/p&gt;&lt;p&gt;Specifically we were able to get the &lt;code class="prettyprint"&gt;contents&lt;/code&gt; of a tree and its &lt;code class="prettyprint"&gt;shape&lt;/code&gt;. Is there a way to compose those 2 operations into a &lt;code class="prettyprint"&gt;decompose&lt;/code&gt; operation that would get both the content and the shape at once? Our first attempt might be:&lt;/p&gt; &lt;pre&gt;&lt;code class="prettyprint"&gt;  def decompose[A] = (t: T[A]) =&amp;gt; (shape(t), contents(t))&lt;br /&gt;&lt;br /&gt;  tree.decompose must_== (Bin(Leaf(()), Leaf(())), List(1, 2))&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This works but it is pretty naive because this requires 2 traversals of the tree. Is that possible to do just one?&lt;/p&gt;&lt;a name="Applicative+products"&gt;&lt;/a&gt;&lt;h4&gt;Applicative products&lt;/h4&gt;&lt;p&gt;This is indeed possible by noticing the following: &lt;em&gt;the product of 2 Applicatives is still an Applicative&lt;/em&gt;.&lt;/p&gt;&lt;p&gt;Proof, proof. We define &lt;code class="prettyprint"&gt;Product&lt;/code&gt; as:&lt;/p&gt; &lt;pre&gt;&lt;code class="prettyprint"&gt;  case class Product[F1[_], F2[_], A](first: F1[A], second: F2[A]) {&lt;br /&gt;    def tuple = (first, second)&lt;br /&gt;  }&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;I spare you the full definition of &lt;code class="prettyprint"&gt;Product&lt;/code&gt; as an &lt;code class="prettyprint"&gt;Applicative&lt;/code&gt; to just focus on the &lt;code class="prettyprint"&gt;Applic&lt;/code&gt; instance:&lt;/p&gt; &lt;pre&gt;&lt;code class="prettyprint"&gt;  implicit def ProductIsApplic[F1[_] : Applic, F2[_] : Applic] =&lt;br /&gt;    new Applic[({type l[A]=Product[F1, F2, A]})#l] {&lt;br /&gt;      val f1 = implicitly[Applic[F1]]&lt;br /&gt;      val f2 = implicitly[Applic[F2]]&lt;br /&gt;&lt;br /&gt;      def applic[A, B](f: Product[F1, F2, A =&amp;gt; B]) = (c: Product[F1, F2, A]) =&amp;gt;&lt;br /&gt;        Product[F1, F2, B](f1.applic(f.first).apply(c.first), &lt;br /&gt;                           f2.applic(f.second).apply(c.second))&lt;br /&gt;   }&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;That's not too complicated, you just have to follow the types. What's more troubling is the amount of type annotations which are necessary to implement &lt;code class="prettyprint"&gt;decompose&lt;/code&gt;. Ideally we would like to write:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  def decompose[A] = traverse((t: T[A]) =&amp;gt; shape(t) ⊗ contents(t))&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Where &lt;code class="prettyprint"&gt;⊗&lt;/code&gt; is an operation taking 2 &lt;code class="prettyprint"&gt;Applicative&lt;/code&gt;s and returning their product. Again the lack of partial type application for &lt;code class="prettyprint"&gt;Const&lt;/code&gt; muddies the whole (upvote &lt;a href="https://issues.scala-lang.org/browse/SI-2712"&gt;SI-2712&lt;/a&gt; please!):&lt;/p&gt; &lt;pre&gt;&lt;code class="prettyprint"&gt;val shape   = (a: A) =&amp;gt; Ident(())&lt;br /&gt;val content = (a: A) =&amp;gt; Const[List[A], Unit](List(a))&lt;br /&gt;&lt;br /&gt;val product = (a: A) =&amp;gt; (shape(a).⊗[({type l[T] = Const[List[A], T]})#l](content(a)))&lt;br /&gt;&lt;br /&gt;implicit val productApplicative = &lt;br /&gt;  ProductIsApplicative[Ident, ({type l1[U] = Const[List[A], U]})#l1]&lt;br /&gt;&lt;br /&gt;(ta: T[A]) =&amp;gt; { val (Ident(s), Const(c)) = &lt;br /&gt;  traverse[({type l[V] = Product[Ident, ({type l1[U] = Const[List[A], U]})#l1, V]})#l, A, Unit](product).&lt;br /&gt;   apply(ta).tuple&lt;br /&gt;  (s, c)&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;We can improve the code sligthly by moving the implicit definition for &lt;code class="prettyprint"&gt;productApplicative&lt;/code&gt; inside the &lt;code class="prettyprint"&gt;Applicative&lt;/code&gt; companion object:&lt;/p&gt; &lt;pre&gt;&lt;code class="prettyprint"&gt;  object Applicative {&lt;br /&gt;   ...&lt;br /&gt;   implicit def ProductWithListIsApplicative[A[_] : Applicative, B] = &lt;br /&gt;     ProductIsApplicative[A, ({type l1[U] = Const[List[B], U]})#l1]&lt;br /&gt; }&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Then no &lt;code class="prettyprint"&gt;implicit val productApplicative&lt;/code&gt; is necessary and the &lt;code class="prettyprint"&gt;Applicative&lt;/code&gt; imports will be all we need.&lt;/p&gt;&lt;a name="Collection+and+dispersal"&gt;&lt;/a&gt;&lt;h4&gt;Collection and dispersal&lt;/h4&gt;&lt;p&gt;There is another way to do things "in parallel" while traversing the structure. The &lt;code class="prettyprint"&gt;collect&lt;/code&gt; method that we're going to build will do 2 things:&lt;/p&gt;&lt;ul&gt;   &lt;li&gt;it will &lt;em&gt;accumulate&lt;/em&gt; some kind of state, based on the elements that we meet&lt;/li&gt;&lt;br /&gt; &lt;li&gt;it will &lt;em&gt;map&lt;/em&gt; each element to another kind of element&lt;/li&gt; &lt;/ul&gt;&lt;p&gt;So, as we're iterating, we can do a regular mapping while computing some kind of measure. But before that, we need to take a little detour (again?? Yes, again) with the &lt;code class="prettyprint"&gt;State&lt;/code&gt; monad.&lt;/p&gt;&lt;a name="The+State+monad"&gt;&lt;/a&gt;&lt;h5&gt;The &lt;code class="prettyprint"&gt;State&lt;/code&gt; monad&lt;/h5&gt;&lt;p&gt;The &lt;code class="prettyprint"&gt;State&lt;/code&gt; Monad is defined by:&lt;/p&gt; &lt;pre&gt;&lt;code class="prettyprint"&gt;  trait State[S, +A] {&lt;br /&gt;    def apply(s: S): (S, A)&lt;br /&gt;  }&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;It is basically:&lt;/p&gt; &lt;ul&gt;   &lt;li&gt;an object keeping some previous "state", of type &lt;code class="prettyprint"&gt;S&lt;/code&gt;&lt;/li&gt;   &lt;li&gt;a method to extract a meaningful value from this "state", of type &lt;code class="prettyprint"&gt;A&lt;/code&gt;&lt;/li&gt;&lt;br /&gt; &lt;li&gt;this method computes a new "state", of type &lt;code class="prettyprint"&gt;S&lt;/code&gt;&lt;/li&gt; &lt;/ul&gt;&lt;p&gt;For example, a simple counter for the number of elements in a &lt;code class="prettyprint"&gt;List[Int]&lt;/code&gt; can be implemented by:&lt;/p&gt; &lt;pre&gt;&lt;code class="prettyprint"&gt;  val count = state((n: Int) =&amp;gt; (n+1, ()))&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;It takes the previous "count" number &lt;code class="prettyprint"&gt;n&lt;/code&gt; and returns the new state &lt;code class="prettyprint"&gt;n+1&lt;/code&gt; and the extracted value (&lt;code class="prettyprint"&gt;()&lt;/code&gt; here, because we don't need to extract anything special).&lt;/p&gt;&lt;p&gt;The &lt;code class="prettyprint"&gt;State&lt;/code&gt; type above is a &lt;code class="prettyprint"&gt;Monad&lt;/code&gt;. I encourage you to read &lt;a href="http://learnyouahaskell.com/for-a-few-monads-more#state"&gt;"Learn You a Haskell"&lt;/a&gt; to get a better understanding on the subject. I will just show here that the &lt;code class="prettyprint"&gt;flatMap&lt;/code&gt; (or &lt;code class="prettyprint"&gt;bind&lt;/code&gt;) method of the &lt;code class="prettyprint"&gt;Monad&lt;/code&gt; typeclass is central in putting that &lt;code class="prettyprint"&gt;State&lt;/code&gt; to work:&lt;/p&gt; &lt;pre&gt;&lt;code class="prettyprint"&gt;  val count = (s: String) =&amp;gt; state((n: Int) =&amp;gt; (n+1, s + n))&lt;br /&gt;&lt;br /&gt;  (count("a-") flatMap count flatMap count).apply(0) must_== (3, "a-012")&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The &lt;code class="prettyprint"&gt;count&lt;/code&gt; function takes the latest computed string and returns a &lt;code class="prettyprint"&gt;State&lt;/code&gt; where we increment the current "state" by 1 and we have a new String as the result, where the current count is appended. So when we start with the string &lt;code class="prettyprint"&gt;"a-"&lt;/code&gt; and we &lt;code class="prettyprint"&gt;flatMap&lt;/code&gt; &lt;code class="prettyprint"&gt;count&lt;/code&gt; 2 times, we get &lt;code class="prettyprint"&gt;(3, "a-012")&lt;/code&gt; where 3 is the number of times we've applied the &lt;code class="prettyprint"&gt;n+1&lt;/code&gt; function and &lt;code class="prettyprint"&gt;"a-012"&lt;/code&gt; the result of appending to the current string.&lt;/p&gt;&lt;p&gt;By the way, why do we need to &lt;code class="prettyprint"&gt;apply(0)&lt;/code&gt;?&lt;/p&gt;&lt;p&gt;When we do all the &lt;code class="prettyprint"&gt;flatMap&lt;/code&gt;s, we actually store "stateful computations". And they are executed only once we provide the initial state: &lt;code class="prettyprint"&gt;0&lt;/code&gt;!&lt;/p&gt;&lt;a name="Collecting+elements"&gt;&lt;/a&gt;&lt;h5&gt;Collecting elements&lt;/h5&gt;&lt;p&gt;Let's now define a &lt;code class="prettyprint"&gt;collect&lt;/code&gt; operation on &lt;code class="prettyprint"&gt;Traversable&lt;/code&gt; which will help us to count:&lt;/p&gt; &lt;pre&gt;&lt;code class="prettyprint"&gt;  def collect[F[_] : Applicative, A, B](f: A =&amp;gt; F[Unit], g: A =&amp;gt; B) = {&lt;br /&gt;    val applicative = implicitly[Applicative[F]]&lt;br /&gt;    import applicative._&lt;br /&gt;&lt;br /&gt;    val application = (a: A) =&amp;gt; point((u: Unit) =&amp;gt; g(a)) &amp;lt;*&amp;gt; f(a)&lt;br /&gt;    traverse(application)&lt;br /&gt;  }&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This &lt;code class="prettyprint"&gt;collect&lt;/code&gt; operation, defined in EIP, is different from the &lt;code class="prettyprint"&gt;collect&lt;/code&gt; operation on Scala collections which is the equivalent of &lt;code class="prettyprint"&gt;filter + map&lt;/code&gt;. The &lt;code class="prettyprint"&gt;collect&lt;/code&gt; of EIP is using 2 functions:&lt;/p&gt; &lt;ul&gt;   &lt;li&gt;&lt;code class="prettyprint"&gt;f: A =&amp;gt; F[Unit]&lt;/code&gt; which collects data from each element "effectfully" (that is, possibly keeping state)&lt;/li&gt;&lt;br /&gt; &lt;li&gt;&lt;code class="prettyprint"&gt;g: A =&amp;gt; B&lt;/code&gt; which maps each element to something else&lt;/li&gt; &lt;/ul&gt;&lt;p&gt;So we could say that the EIP &lt;code class="prettyprint"&gt;collect&lt;/code&gt; is a bit like &lt;code class="prettyprint"&gt;fold + map&lt;/code&gt;. Knowing this, we can use &lt;code class="prettyprint"&gt;collect&lt;/code&gt; to count elements &lt;em&gt;and&lt;/em&gt; do some mapping:&lt;/p&gt; &lt;pre&gt;&lt;code class="prettyprint"&gt;  val count = (i: Int) =&amp;gt; state((n: Int) =&amp;gt; (n+1, ()))&lt;br /&gt;  val map   = (i: Int) =&amp;gt; i.toString&lt;br /&gt;&lt;br /&gt;  tree.collect[({type l[A]=State[Int, A]})#l, String](count, map).apply(0) must_== &lt;br /&gt;  (2, Bin(Leaf("1"), Leaf("2")))&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Here again the type annotations are obscuring the intent a bit and if type inference was perfect we would just read:&lt;/p&gt; &lt;pre&gt;&lt;code class="prettyprint"&gt;  val count = (i: Int) =&amp;gt; state((n: Int) =&amp;gt; (n+1, ()))&lt;br /&gt;  val map   = (i: Int) =&amp;gt; i.toString&lt;br /&gt;&lt;br /&gt;  tree.collect(count, map).apply(0) must_== (2, Bin(Leaf("1"), Leaf("2")))&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;I don't know about you, but I find this a bit magical. With the &lt;code class="prettyprint"&gt;Applicative&lt;/code&gt; and &lt;code class="prettyprint"&gt;Traversable&lt;/code&gt; abstractions, we can assemble our program based on 2 independent functions possibly developed and tested elsewhere.&lt;/p&gt;&lt;a name="Dispersing+elements"&gt;&lt;/a&gt;&lt;h5&gt;Dispersing elements&lt;/h5&gt;&lt;p&gt;The next utility function proposed by EIP is the &lt;code class="prettyprint"&gt;disperse&lt;/code&gt; function. Its signature is:&lt;/p&gt; &lt;pre&gt;&lt;code class="prettyprint"&gt;  def disperse[F[_] : Applicative, A, B, C](f: F[B], g: A =&amp;gt; B =&amp;gt; C): F[A] =&amp;gt; F[T[C]]&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;What does it do?&lt;/p&gt; &lt;ul&gt;   &lt;li&gt;&lt;code class="prettyprint"&gt;f&lt;/code&gt; is the &lt;code class="prettyprint"&gt;Applicative&lt;/code&gt; context that's going to evolve when we traverse the structure, but regardless of what the elements of type &lt;code class="prettyprint"&gt;A&lt;/code&gt; are&lt;/li&gt;   &lt;li&gt;&lt;code class="prettyprint"&gt;g&lt;/code&gt; is a function which, for each element of type &lt;code class="prettyprint"&gt;A&lt;/code&gt; says what to do with the current context value, &lt;code class="prettyprint"&gt;B&lt;/code&gt;, and map that element back to the structure&lt;/li&gt; &lt;/ul&gt;&lt;p&gt;Please, please, a concrete example!&lt;/p&gt;&lt;p&gt;Say I want to mark each element of a &lt;code class="prettyprint"&gt;BinaryTree&lt;/code&gt; with its "number" in the Traversal (the "label"). Moreover I want to use the element name to be able to qualify this label:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  // a BinaryTree of Doubles&lt;br /&gt; val tree: BinaryTree[Double] = Bin(Leaf(1.1), Bin(Leaf(2.2), Leaf(3.3)))&lt;br /&gt;&lt;br /&gt; // the "label" state returning integers in sequence&lt;br /&gt; val labelling: State[Int, Int] = state((n: Int) =&amp;gt; (n+1, n+1))&lt;br /&gt;&lt;br /&gt; // for each element in the tree, and its label, &lt;br /&gt; // produce a String with the name and label&lt;br /&gt; val naming: Double =&amp;gt; Int =&amp;gt; String = (p1: Double) =&amp;gt; (p2: Int) =&amp;gt; p1+" node is "+p2&lt;br /&gt;&lt;br /&gt; // testing by applying an initial state (label `0`) and&lt;br /&gt; // taking the second element of the pair `(last label, resulting tree)`&lt;br /&gt; tree.disperse[elided for sanity](labelling, naming).apply(0)._2 must_==&lt;br /&gt;   Bin(Leaf("1.1 node is 1"), Bin(Leaf("2.2 node is 2"), Leaf("3.3 node is 3")))&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Note that the &lt;code class="prettyprint"&gt;naming&lt;/code&gt; function above is curried. A more familiar way to write it would be:&lt;/p&gt; &lt;pre&gt;&lt;code class="prettyprint"&gt;  val naming: (Double, Int) =&amp;gt; String = (p1: Double, p2: Int) =&amp;gt; p1+" node is "+p2&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;But then you would have to curry that function to be able to use it with the &lt;code class="prettyprint"&gt;disperse&lt;/code&gt; function:&lt;/p&gt; &lt;pre&gt;&lt;code class="prettyprint"&gt;  tree.disperse[...](labelling, naming.curried)&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The implementation of &lt;code class="prettyprint"&gt;disperse&lt;/code&gt; is:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  def disperse[F[_] : Applicative, A, B, C](f: F[B], g: A =&amp;gt; B =&amp;gt; C) = {&lt;br /&gt;    val applicative = implicitly[Applicative[F]]&lt;br /&gt;    import applicative._&lt;br /&gt;&lt;br /&gt;    val application = (a: A) =&amp;gt; point(g(a)) &amp;lt;*&amp;gt; f&lt;br /&gt;    traverse(application)&lt;br /&gt;  }&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;It is using the very capabilities of the applicative functor, the &lt;code class="prettyprint"&gt;point&lt;/code&gt; method and the &lt;code class="prettyprint"&gt;&amp;lt;*&amp;gt;&lt;/code&gt; application.&lt;/p&gt;&lt;a name="An+overview+of+traversals"&gt;&lt;/a&gt;&lt;h3&gt;An overview of traversals&lt;/h3&gt;&lt;p&gt;We've seen in the 2 examples above that we get different, specialized, versions of the &lt;code class="prettyprint"&gt;traverse&lt;/code&gt; function by constraining how mapping and &lt;code class="prettyprint"&gt;Applicative&lt;/code&gt; effects occur. Here's a tentative table for classifying other specialized versions of the &lt;code class="prettyprint"&gt;traverse&lt;/code&gt; function:&lt;/p&gt;&lt;table&gt;   &lt;thead&gt;&lt;tr&gt;  &lt;th&gt;function &lt;/th&gt;       &lt;th&gt;map element &lt;/th&gt;       &lt;th&gt;create state &lt;/th&gt;       &lt;th&gt;mapped depend on state &lt;/th&gt;       &lt;th&gt;state depend on element &lt;/th&gt;  &lt;/tr&gt;   &lt;/thead&gt;  &lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;&lt;code class="prettyprint"&gt;collect&lt;/code&gt; &lt;/td&gt;       &lt;td&gt;X &lt;/td&gt;       &lt;td&gt;X &lt;/td&gt;       &lt;td&gt; &lt;/td&gt;       &lt;td&gt;X &lt;/td&gt;     &lt;/tr&gt;     &lt;tr&gt;       &lt;td&gt;&lt;code class="prettyprint"&gt;disperse&lt;/code&gt; &lt;/td&gt;       &lt;td&gt;X &lt;/td&gt;       &lt;td&gt;X &lt;/td&gt;       &lt;td&gt;X &lt;/td&gt;       &lt;td&gt; &lt;/td&gt;     &lt;/tr&gt;     &lt;tr&gt;       &lt;td&gt;&lt;code class="prettyprint"&gt;measure&lt;/code&gt; &lt;/td&gt;  &lt;td&gt;X &lt;/td&gt;       &lt;td&gt;X &lt;/td&gt;      &lt;td&gt; &lt;/td&gt;       &lt;td&gt; &lt;/td&gt;     &lt;/tr&gt;     &lt;tr&gt;  &lt;td&gt;&lt;code class="prettyprint"&gt;traverse&lt;/code&gt; &lt;/td&gt;       &lt;td&gt;X &lt;/td&gt;       &lt;td&gt;X &lt;/td&gt;       &lt;td&gt;X &lt;/td&gt;       &lt;td&gt;X &lt;/td&gt;     &lt;/tr&gt;     &lt;tr&gt;       &lt;td&gt;&lt;code class="prettyprint"&gt;reduce&lt;/code&gt; &lt;/td&gt;       &lt;td&gt; &lt;/td&gt;       &lt;td&gt;X &lt;/td&gt;    &lt;td&gt; &lt;/td&gt;       &lt;td&gt;X &lt;/td&gt;     &lt;/tr&gt;     &lt;tr&gt;       &lt;td&gt;&lt;code class="prettyprint"&gt;reduceConst&lt;/code&gt; &lt;/td&gt;       &lt;td&gt; &lt;/td&gt;       &lt;td&gt;X &lt;/td&gt;       &lt;td&gt; &lt;/td&gt;       &lt;td&gt; &lt;/td&gt;     &lt;/tr&gt;     &lt;tr&gt;       &lt;td&gt;&lt;code class="prettyprint"&gt;map&lt;/code&gt; &lt;/td&gt;       &lt;td&gt;X &lt;/td&gt;       &lt;td&gt; &lt;/td&gt;      &lt;td&gt; &lt;/td&gt;       &lt;td&gt; &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt; &lt;/table&gt;&lt;p&gt;The only function we haven't shown before is &lt;code class="prettyprint"&gt;measure&lt;/code&gt;. It is mapping and accumulating state but this accumulation does not depend on the current element. Here's an example:&lt;/p&gt; &lt;pre&gt;&lt;code class="prettyprint"&gt;  val crosses = state((s: String) =&amp;gt; (s+"x", ()))&lt;br /&gt;  val map     = (i: Int) =&amp;gt; i.toString&lt;br /&gt;&lt;br /&gt;  tree.measure(crosses, map).apply("") must_==&lt;br /&gt;  ("xxx", Bin(Leaf("1"), Bin(Leaf("2"), Leaf("3"))))&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Other than not looking very useful, the code above is also lying! It is not possible to have a &lt;code class="prettyprint"&gt;measure&lt;/code&gt; function accepting a &lt;code class="prettyprint"&gt;State&lt;/code&gt; monad without having to provide the usual ugly type annotations. So the actual example is:&lt;/p&gt; &lt;pre&gt;&lt;code class="prettyprint"&gt;  tree.measureState(crosses, map).apply("") must_== &lt;br /&gt;  ("xxx", Bin(Leaf("1"), Bin(Leaf("2"), Leaf("3"))))&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;where &lt;code class="prettyprint"&gt;measureState&lt;/code&gt; is a specialization of the &lt;code class="prettyprint"&gt;measure&lt;/code&gt; method to &lt;code class="prettyprint"&gt;States&lt;/code&gt;. I think that one take-away of this post is that it might be beneficial to specialize a few generic functions in Scala , like &lt;code class="prettyprint"&gt;traverse&lt;/code&gt;, &lt;code class="prettyprint"&gt;collect&lt;/code&gt;... for &lt;code class="prettyprint"&gt;Const&lt;/code&gt; and &lt;code class="prettyprint"&gt;State&lt;/code&gt; in order to avoid type annotations.&lt;/p&gt;&lt;a name="Composing+traversals"&gt;&lt;/a&gt;&lt;h2&gt;Composing traversals&lt;/h2&gt;&lt;p&gt;There's another axis of composition that we haven't exploited yet.&lt;/p&gt;&lt;p&gt;In a &lt;code class="prettyprint"&gt;for&lt;/code&gt; loop, without thinking about it, you may write:&lt;/p&gt; &lt;pre&gt;&lt;code class="prettyprint"&gt;  for (a &amp;lt;- as) {&lt;br /&gt;    val currentSize = a.size&lt;br /&gt;    total += currentSize&lt;br /&gt;    result.add(total)&lt;br /&gt;  }&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;In the body of that &lt;code class="prettyprint"&gt;for&lt;/code&gt; loop, you have statements with dependency on each other. In an &lt;code class="prettyprint"&gt;Applicative&lt;/code&gt; traversal, this translates to the &lt;em&gt;Sequential composition&lt;/em&gt; of Applicatives. From 2 &lt;code class="prettyprint"&gt;Applicatives&lt;/code&gt;, we can create a third one which is their &lt;em&gt;Sequential composition&lt;/em&gt;. More precisely, this means that if &lt;code class="prettyprint"&gt;F1[_]&lt;/code&gt; and &lt;code class="prettyprint"&gt;F2[_]&lt;/code&gt; are &lt;code class="prettyprint"&gt;Applicatives&lt;/code&gt; then &lt;code class="prettyprint"&gt;F1[F2[_]]&lt;/code&gt; is an &lt;code class="prettyprint"&gt;Applicative&lt;/code&gt; as well. You want the demonstration? Ok, go.&lt;/p&gt;&lt;p&gt;First, we introduce a utility function on &lt;code class="prettyprint"&gt;ApplicFunctor&lt;/code&gt;s:&lt;/p&gt; &lt;pre&gt;&lt;code class="prettyprint"&gt;  def liftA2[A, B, C](function: A =&amp;gt; B =&amp;gt; C): F[A] =&amp;gt; F[B] =&amp;gt; F[C] = &lt;br /&gt;    fa =&amp;gt; applic.applic(functor.fmap(function)(fa))&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;code class="prettyprint"&gt;liftA2&lt;/code&gt; allows to &lt;code class="prettyprint"&gt;lift&lt;/code&gt; a regular function of 2 arguments to a function working on the &lt;code class="prettyprint"&gt;Applicative&lt;/code&gt; arguments. This is using the fact that an &lt;code class="prettyprint"&gt;ApplicFunctor&lt;/code&gt; is a &lt;code class="prettyprint"&gt;Functor&lt;/code&gt; so we can apply &lt;code class="prettyprint"&gt;function: A =&amp;gt; B =&amp;gt; C&lt;/code&gt; to the "&lt;code class="prettyprint"&gt;a&lt;/code&gt; in the box", to get a &lt;code class="prettyprint"&gt;F[B =&amp;gt; C]&lt;/code&gt; "in the box". And then, an &lt;code class="prettyprint"&gt;ApplicFunctor&lt;/code&gt; is an &lt;code class="prettyprint"&gt;Applic&lt;/code&gt;, so we can "apply" &lt;code class="prettyprint"&gt;F[B]&lt;/code&gt; to get a &lt;code class="prettyprint"&gt;F[C]&lt;/code&gt;&lt;/p&gt;&lt;p&gt;Armed with this function, we can write the &lt;code class="prettyprint"&gt;applic&lt;/code&gt; method for &lt;code class="prettyprint"&gt;F1[F2[_]]&lt;/code&gt;:&lt;/p&gt; &lt;pre&gt;&lt;code class="prettyprint"&gt;  implicit val f1ApplicFunctor = implicitly[ApplicFunctor[F1]]&lt;br /&gt;  implicit val f2ApplicFunctor = implicitly[ApplicFunctor[F2]]&lt;br /&gt;&lt;br /&gt;  val applic = new Applic[({type l[A]=F1[F2[A]]})#l] {&lt;br /&gt;    def applic[A, B](f: F1[F2[A =&amp;gt; B]]) = (c: F1[F2[A]]) =&amp;gt; {&lt;br /&gt;      f1ApplicFunctor.liftA2((ff: F2[A =&amp;gt; B]) =&amp;gt; f2ApplicFunctor.apply(ff))(f).apply(c)&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;It's not so easy to get an intuition for what the code above is doing except that saying that we're using previous definitions to allow a &lt;code class="prettyprint"&gt;F1[F2[A =&amp;gt; B]]&lt;/code&gt; to be applied to &lt;code class="prettyprint"&gt;F1[F2[A]]&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;In mere mortal terms, this means that if we do an &lt;code class="prettyprint"&gt;Applicative&lt;/code&gt; computation inside a loop and if we reuse that computation in another &lt;code class="prettyprint"&gt;Applicative&lt;/code&gt; computation, we still get an &lt;code class="prettyprint"&gt;Applicative&lt;/code&gt; computation. The EIP illustration of this principle is a crazy function, the &lt;code class="prettyprint"&gt;assemble&lt;/code&gt; function.&lt;/p&gt;&lt;a name="The+assemble+function"&gt;&lt;/a&gt;&lt;h3&gt;The &lt;code class="prettyprint"&gt;assemble&lt;/code&gt; function&lt;/h3&gt;&lt;p&gt;The &lt;code class="prettyprint"&gt;assemble&lt;/code&gt; function takes the &lt;em&gt;shape&lt;/em&gt; of a &lt;code class="prettyprint"&gt;Traversable&lt;/code&gt; and a list of elements. If there are enough elements it returns &lt;code class="prettyprint"&gt;Some[Traversable]&lt;/code&gt; filled with all the elements (+ the reminder), otherwise it returns &lt;code class="prettyprint"&gt;None&lt;/code&gt; (and an empty list). Let's see it in action:&lt;/p&gt; &lt;pre&gt;&lt;code class="prettyprint"&gt;        // the "shape" to fill&lt;br /&gt;       val shape: BinaryTree[Unit] = Bin(Leaf(()), Leaf(()))&lt;br /&gt;&lt;br /&gt;       // we assemble the tree with an exact list of elements&lt;br /&gt;       shape.assemble(List(1, 2)) must_== (List(), Some(Bin(Leaf(1), Leaf(2))))&lt;br /&gt;&lt;br /&gt;       // we assemble the tree with more elements&lt;br /&gt;       shape.assemble(List(1, 2, 3)) must_== (List(3), Some(Bin(Leaf(1), Leaf(2))))&lt;br /&gt;&lt;br /&gt;       // we assemble the tree with not enough elements&lt;br /&gt;       shape.assemble(List(1)) must_== (List(), None)&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;What's the implementation of the &lt;code class="prettyprint"&gt;assemble&lt;/code&gt; function? The implementation uses 2 &lt;code class="prettyprint"&gt;Monads&lt;/code&gt; (which are also &lt;code class="prettyprint"&gt;Applicatives&lt;/code&gt; as we know now):&lt;/p&gt; &lt;ul&gt;   &lt;li&gt;the &lt;code class="prettyprint"&gt;State[List[Int], _] Monad&lt;/code&gt; is going to keep track of what we've already consumed&lt;/li&gt;   &lt;li&gt;the &lt;code class="prettyprint"&gt;Option[_] Monad&lt;/code&gt; is going to provide, or not, an element to put in the structure&lt;/li&gt;   &lt;li&gt;the composition of those 2 monads is &lt;code class="prettyprint"&gt;State[List[Int], Option[_]]&lt;/code&gt; (our &lt;code class="prettyprint"&gt;F1[F2[_]]&lt;/code&gt; in the &lt;code class="prettyprint"&gt;ApplicFunctor&lt;/code&gt; definitions above&lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;&lt;p&gt;So we just need to traverse the &lt;code class="prettyprint"&gt;BinaryTree&lt;/code&gt; with one function:&lt;/p&gt; &lt;pre&gt;&lt;code class="prettyprint"&gt;def takeHead: State[List[B], Option[B]] = state { s: List[B] =&amp;gt;&lt;br /&gt;  s match {&lt;br /&gt;    case Nil     =&amp;gt; (Nil, None)&lt;br /&gt;    case x :: xs =&amp;gt; (xs, Some(x))&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The &lt;code class="prettyprint"&gt;takeHead&lt;/code&gt; function is a &lt;code class="prettyprint"&gt;State&lt;/code&gt; instance where each state application removes the first element of the list of elements if possible, and returns it in an Option.&lt;br /&gt;This is why the result of the &lt;code class="prettyprint"&gt;assemble&lt;/code&gt; function, once we apply it to a list of elements, is of type &lt;code class="prettyprint"&gt;(List[Int], Option[BinaryTree[Int]])&lt;/code&gt;.&lt;/p&gt;&lt;a name="A+recursive+implementation"&gt;&lt;/a&gt;&lt;h4&gt;A recursive implementation&lt;/h4&gt;&lt;p&gt;Just for the fun of comparison, I'm going to write a recursive version doing the same thing:&lt;/p&gt; &lt;pre&gt;&lt;code class="prettyprint"&gt;  def assemble(es: List[Int], s: BinaryTree[Unit]) : (List[Int], Option[BinaryTree[Int]]) = {&lt;br /&gt;    (es, s) match {&lt;br /&gt;      case (Nil, _)                      =&amp;gt; (es, None)&lt;br /&gt;      case (e :: rest, Leaf(()))         =&amp;gt; (rest, Some(Leaf(e)))&lt;br /&gt;      case (_, Bin(left, right))         =&amp;gt; {&lt;br /&gt;        assemble(es, left) match {&lt;br /&gt;          case (l, None)       =&amp;gt; (l, None)&lt;br /&gt;          case (Nil, Some(l))  =&amp;gt; (Nil, None)&lt;br /&gt;          case (rest, Some(l)) =&amp;gt; assemble(rest, right) match {&lt;br /&gt;            case (r, None)            =&amp;gt; (r, None)&lt;br /&gt;            case (finalRest, Some(r)) =&amp;gt; (finalRest, Some(Bin(l, r)))&lt;br /&gt;          }&lt;br /&gt;        }&lt;br /&gt;      }&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;  assemble(List(1, 2, 3), shape) must_== (List(3), Some(Bin(Leaf(1), Leaf(2))))&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;It works, but it makes my head spin!&lt;/p&gt;&lt;a name="A+classical+for-loop+implementation"&gt;&lt;/a&gt;&lt;h4&gt;A classical for-loop implementation&lt;/h4&gt;&lt;p&gt;By the way, what would be the &lt;em&gt;real&lt;/em&gt; &lt;code class="prettyprint"&gt;for&lt;/code&gt; loop version of that functionality? That one is not so easy to come up with because AFAIK there's no easy way to iterate on a &lt;code class="prettyprint"&gt;BinaryTree&lt;/code&gt; to get a similar &lt;code class="prettyprint"&gt;BinaryTree&lt;/code&gt; with just a &lt;code class="prettyprint"&gt;for&lt;/code&gt; loop! So, for the sake of the argument, we're going to do something similar with just a &lt;code class="prettyprint"&gt;List&lt;/code&gt; structure:&lt;/p&gt; &lt;pre&gt;&lt;code class="prettyprint"&gt;  def assemble[T](es: List[T], shape: List[Unit]) = {&lt;br /&gt;    var elements = es&lt;br /&gt;    var list: Option[List[T]] = None&lt;br /&gt;    for (u &amp;lt;- shape) {&lt;br /&gt;      if (!elements.isEmpty) {&lt;br /&gt;        list match {&lt;br /&gt;          case None    =&amp;gt; list = Some(List(elements.first))&lt;br /&gt;          case Some(l) =&amp;gt; list = Some(l :+ elements.first)&lt;br /&gt;        }&lt;br /&gt;        elements = elements.drop(1)&lt;br /&gt;      } else {&lt;br /&gt;        list = None&lt;br /&gt;      }&lt;br /&gt;    }&lt;br /&gt;    (elements, list)&lt;br /&gt;  }&lt;br /&gt;  assemble(List(1, 2, 3), List((), ())) must_== (List(3), Some(List(1, 2)))&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Contrast and compare with:&lt;/p&gt; &lt;pre&gt;&lt;code class="prettyprint"&gt;  List((), ()).assemble(List(1, 2, 3)) must_== (List(3), Some(List(1, 2)))&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;where you just define &lt;code class="prettyprint"&gt;List&lt;/code&gt; as a &lt;code class="prettyprint"&gt;Traversable&lt;/code&gt;:&lt;/p&gt; &lt;pre&gt;&lt;code class="prettyprint"&gt;  implicit def ListIsTraversable[A]: Traversable[List] = new Traversable[List] {&lt;br /&gt;&lt;br /&gt;    def traverse[F[_] : Applicative, A, B](f: A =&amp;gt; F[B]): List[A] =&amp;gt; F[List[B]] = &lt;br /&gt;      (l: List[A]) =&amp;gt; {&lt;br /&gt;        val applicative = implicitly[Applicative[F]]&lt;br /&gt;        l match {&lt;br /&gt;          case Nil       =&amp;gt; applicative.point(List[B]())&lt;br /&gt;          case a :: rest =&amp;gt;&lt;br /&gt;            ((_:B) :: (_: List[B])).curried ∘ f(a) &amp;lt;*&amp;gt; (rest traverse f)&lt;br /&gt;      }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;  }&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The Applicative composition is indeed very powerful, but we're going to see that there are other ways to compose functions and use them with &lt;code class="prettyprint"&gt;Traversables&lt;/code&gt;.&lt;/p&gt;&lt;a name="Monadic+composition"&gt;&lt;/a&gt;&lt;h3&gt;Monadic composition&lt;/h3&gt;&lt;p&gt;This paragraph is exploring the fine relationships between applicative composition and monadic composition when doing traversals. We've seen that &lt;code class="prettyprint"&gt;Applicative&lt;/code&gt; instances can be composed and that &lt;code class="prettyprint"&gt;Monads&lt;/code&gt; can be &lt;code class="prettyprint"&gt;Applicative&lt;/code&gt;. But &lt;code class="prettyprint"&gt;Monads&lt;/code&gt; can also be composed using the so-called &lt;em&gt;Kleisli composition&lt;/em&gt;. If we have:&lt;/p&gt; &lt;pre&gt;&lt;code class="prettyprint"&gt;  val f: B =&amp;gt; M[C]&lt;br /&gt;  val g: A =&amp;gt; M[B]&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Then&lt;/p&gt; &lt;pre&gt;&lt;code class="prettyprint"&gt;  val h: A =&amp;gt; M[C] = f ∎ g // is also a function from a value to a Monad&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;If we have 2 "monadic" functions &lt;code class="prettyprint"&gt;f&lt;/code&gt; and &lt;code class="prettyprint"&gt;g&lt;/code&gt;, we can then compose them, in the Kleisli sense, and use the composed version for a traversal. Indeed we can, but does this traversal have "nice properties"? Specifically, do we have:&lt;/p&gt; &lt;pre&gt;&lt;code class="prettyprint"&gt;  traverse(f ∎ g) == traverse(f) ∎ traverse(g)&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The answer is... it depends.&lt;/p&gt;&lt;a name="Monad+commutativity"&gt;&lt;/a&gt;&lt;h4&gt;Monad commutativity&lt;/h4&gt;&lt;p&gt;EIP shows that, if the &lt;code class="prettyprint"&gt;Monad&lt;/code&gt; is &lt;em&gt;commutative&lt;/em&gt;, then this will always be true. What is a &lt;em&gt;commutative&lt;/em&gt; &lt;code class="prettyprint"&gt;Monad&lt;/code&gt; you ask?&lt;/p&gt;&lt;p&gt;A &lt;code class="prettyprint"&gt;Monad&lt;/code&gt; is commutative if for all &lt;code class="prettyprint"&gt;mx: M[X]&lt;/code&gt; and &lt;code class="prettyprint"&gt;my: M[Y]&lt;/code&gt; we have:&lt;/p&gt; &lt;pre&gt;&lt;code class="prettyprint"&gt;    val xy = for {&lt;br /&gt;     x &amp;lt;- mx&lt;br /&gt;     y &amp;lt;- my&lt;br /&gt;   } yield (x, y)&lt;br /&gt;&lt;br /&gt;   val yx = for {&lt;br /&gt;     y &amp;lt;- my&lt;br /&gt;     x &amp;lt;- mx&lt;br /&gt;   } yield (x, y)&lt;br /&gt;&lt;br /&gt;   xy == yx&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This is &lt;em&gt;not&lt;/em&gt; the case with the &lt;code class="prettyprint"&gt;State Monad&lt;/code&gt; for example:&lt;/p&gt; &lt;pre&gt;&lt;code class="prettyprint"&gt;   val mx = state((n: Int) =&amp;gt; (n+1, n+1))&lt;br /&gt;   val my = state((n: Int) =&amp;gt; (n+1, n+1))&lt;br /&gt;&lt;br /&gt;   xy.apply(0) must_== (2, (1, 2))&lt;br /&gt;   yx.apply(0) must_== (2, (2, 1))&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;a name="Monadic+functions+commutativity"&gt;&lt;/a&gt;&lt;h4&gt;Monadic functions commutativity&lt;/h4&gt;&lt;p&gt;Another slightly different situation is when we have a non-commutative &lt;code class="prettyprint"&gt;Monad&lt;/code&gt; but commutative functions:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  val plus1  = (a: A) =&amp;gt; state((n: Int) =&amp;gt; (n+1, a))&lt;br /&gt;  val plus2  = (a: A) =&amp;gt; state((n: Int) =&amp;gt; (n+2, a))&lt;br /&gt;  val times2 = (a: A) =&amp;gt; state((n: Int) =&amp;gt; (n*2, a))&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Here &lt;code class="prettyprint"&gt;plus1&lt;/code&gt; and &lt;code class="prettyprint"&gt;times2&lt;/code&gt; are not commutative:&lt;/p&gt; &lt;pre&gt;&lt;code class="prettyprint"&gt;  (0 + 1) * 2 != (0 * 2) + 1&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;However it is obvious that &lt;code class="prettyprint"&gt;plus1&lt;/code&gt; and &lt;code class="prettyprint"&gt;plus2&lt;/code&gt; are commutative. What does that mean when we do a traversal?&lt;/p&gt;&lt;p&gt;If we traverse a simple List of elements using monadic composition we get:&lt;/p&gt; &lt;ul&gt;   &lt;li&gt;&lt;code class="prettyprint"&gt;List(1, 2, 3).traverse(times2 ∎ plus1)                         === 22&lt;/code&gt;&lt;/li&gt;   &lt;li&gt;&lt;code class="prettyprint"&gt;List(1, 2, 3).traverse(times2) ∎ List(1, 2, 3).traverse(plus1) === 32&lt;/code&gt;&lt;/li&gt; &lt;/ul&gt;&lt;p&gt;We get different results. However, when &lt;code class="prettyprint"&gt;f&lt;/code&gt; and &lt;code class="prettyprint"&gt;g&lt;/code&gt; commute we get the same result:&lt;/p&gt; &lt;ul&gt;   &lt;li&gt;&lt;code class="prettyprint"&gt;List(1, 2, 3).traverse(plus2 ∎ plus1)                         === 10&lt;/code&gt;&lt;/li&gt;   &lt;li&gt;&lt;code class="prettyprint"&gt;List(1, 2, 3).traverse(plus2) ∎ List(1, 2, 3).traverse(plus1) === 10&lt;/code&gt;&lt;/li&gt; &lt;/ul&gt;&lt;a name="Applicative+composition+vs+Monadic+composition"&gt;&lt;/a&gt;&lt;h4&gt;Applicative composition vs Monadic composition&lt;/h4&gt;&lt;p&gt;Another question we can ask ourselves is: if we consider the &lt;em&gt;monadic&lt;/em&gt; functions as &lt;em&gt;applicative&lt;/em&gt; functions (because each &lt;code class="prettyprint"&gt;Monad&lt;/code&gt; is &lt;code class="prettyprint"&gt;Applicative&lt;/code&gt;), do we get the nice "distribution" property we're after? The answer is yes, even when the functions are not commutative:&lt;/p&gt; &lt;ul&gt;   &lt;li&gt;&lt;code class="prettyprint"&gt;List(1, 2, 3).traverse(times2 ⊡ plus1)                         === 4&lt;/code&gt;&lt;/li&gt;   &lt;li&gt;&lt;code class="prettyprint"&gt;List(1, 2, 3).traverse(times2) ⊡ List(1, 2, 3).traverse(plus1) === 4&lt;/code&gt;&lt;/li&gt; &lt;/ul&gt;&lt;p&gt;Well... more or less. The real situation is a bit more complex. &lt;code class="prettyprint"&gt;List(1, 2, 3).traverse(times2 ⊡ plus1)&lt;/code&gt; returns a &lt;code class="prettyprint"&gt;State[Int, State[Int, List[Int]]]&lt;/code&gt; while the second expression returns a &lt;code class="prettyprint"&gt;State[Int, List[State[Int, Int]]&lt;/code&gt; so what I'm hiding here is some more manipulations to be able to query the final result with some kind of &lt;code class="prettyprint"&gt;join&lt;/code&gt;.&lt;/p&gt;&lt;a name="Conclusion"&gt;&lt;/a&gt;&lt;h2&gt;Conclusion&lt;/h2&gt;&lt;p&gt;You wouldn't believe it but I've only shown here half of the ideas presented in EIP!&lt;/p&gt;&lt;p&gt;To finish off this post here's 3 take-away points that I've learned while writing it:&lt;/p&gt; &lt;ul&gt;   &lt;li&gt;&lt;p&gt;functional programming is also about mastering some of these higher-level control structures like &lt;code class="prettyprint"&gt;Applicative&lt;/code&gt;. Once you master them, your toolbox expands considerably in power (just consider the &lt;code class="prettyprint"&gt;assemble&lt;/code&gt; example)&lt;/p&gt;&lt;/li&gt;   &lt;li&gt;&lt;p&gt;&lt;a href="http://github.com/scalaz/scalaz"&gt;Scalaz&lt;/a&gt; is an incredible library but somewhat obscure to the beginner. For this post I've rewritten all the typeclasses I needed to have, and lots of examples (using &lt;a href="http://specs2.org/"&gt;specs2&lt;/a&gt; of course). That gave me a much better understanding of the Scalaz functionality. You may consider doing the same to learn Scalaz (my code is available &lt;a href="https://github.com/etorreborre/iterator-essence"&gt;on github&lt;/a&gt;)&lt;/p&gt;&lt;/li&gt;   &lt;li&gt;&lt;p&gt;Scala is lacking behind Haskell in terms of type inference and it's a real pain for higher-order, generic programming. This can be sometimes encapsulated away by specializing generic functions to very common types (like &lt;code class="prettyprint"&gt;traverseState&lt;/code&gt; instead of &lt;code class="prettyprint"&gt;traverse&lt;/code&gt;). Again, please upvote &lt;a href="https://issues.scala-lang.org/browse/SI-2712"&gt;SI-2712&lt;/a&gt;!&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Finally, I want to mention that there are many other Haskell &lt;a href="http://www.haskell.org/haskellwiki/Research_papers/Functional_pearls"&gt;functional pearls&lt;/a&gt; waiting to be transliterated to Scala. I mean, it's a shame that we don't have yet any equivalent for &lt;a href="http://learnyouahaskell.com/"&gt;"Learn you a Haskell"&lt;/a&gt; or &lt;a href="http://www.haskell.org/haskellwiki/Typeclassopedia"&gt;"Typeclassopedia"&lt;/a&gt; in the Scala world. I hope that my post, like &lt;a href="http://debasishg.blogspot.com/2011/01/iteration-in-scala-effectful-yet.html"&gt;this other one&lt;/a&gt; by Debasish Ghosh, will also contribute to bridge the gap.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5336273-4815256103882564718?l=etorreborre.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://etorreborre.blogspot.com/feeds/4815256103882564718/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5336273&amp;postID=4815256103882564718' title='24 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5336273/posts/default/4815256103882564718'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5336273/posts/default/4815256103882564718'/><link rel='alternate' type='text/html' href='http://etorreborre.blogspot.com/2011/06/essence-of-iterator-pattern.html' title='The Essence of the Iterator Pattern'/><author><name>Eric</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://farm3.static.flickr.com/2203/2118752971_7becaf7476_t.jpg'/></author><thr:total>24</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5336273.post-3419215371915546335</id><published>2011-05-21T10:18:00.027+09:00</published><updated>2011-06-04T11:52:24.890+09:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='migration'/><category scheme='http://www.blogger.com/atom/ns#' term='specs2'/><category scheme='http://www.blogger.com/atom/ns#' term='specs'/><title type='text'>specs2 migration guide</title><content type='html'>&lt;i&gt;&lt;b&gt;&lt;span class="Apple-style-span"&gt;&lt;h4&gt;Rewrite is rarely the right option&lt;/h4&gt;&lt;/span&gt;&lt;/b&gt;&lt;/i&gt;&lt;p&gt;Well, except when that's the only one :-).&lt;/p&gt;&lt;p&gt;Before starting &lt;a href="http://specs2.org/"&gt;specs2&lt;/a&gt;, I did try to refactor &lt;a href="http://code.google.com/p/specs/"&gt;specs&lt;/a&gt;. It turned out that my first design was clearly not adapted to my goals. So I eventually decided to start from a blank page, as explained &lt;a href="http://etorreborre.github.com/specs2/guide/org.specs2.guide.Philosophy.html"&gt;here&lt;/a&gt;. While I wanted to keep most features I didn't try to go for a 100% backward compatibility. I tried to think to &lt;em&gt;features as part of the design space&lt;/em&gt; and wanted to be sure I had enough design freedom (a complementary view is that &lt;em&gt;implementation is part of the features space&lt;/em&gt;).&lt;/p&gt;&lt;p&gt;That being said, I know that migrating to a new API is not something that people just do for the sheer fun of it. They have to have compelling reasons for doing so.&lt;/p&gt;&lt;p&gt;What are the good reasons why one would like to use &lt;em&gt;&lt;strong&gt;specs2&lt;/strong&gt;&lt;/em&gt; instead of &lt;strong&gt;&lt;em&gt;specs&lt;/em&gt;&lt;/strong&gt;?&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;p&gt;concurrent execution of examples: that's one major thing that is enabled by specs2 new design. Easy and reliable&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;acceptance specifications: something which was really experimental in &lt;strong&gt;&lt;em&gt;specs&lt;/em&gt;&lt;/strong&gt; and which is now completely integrated. You can use it to create an executable &lt;a href="http://etorreborre.github.com/specs2/guide/org.specs2.UserGuide.html"&gt;User Guide&lt;/a&gt; for example&lt;/p&gt;&lt;/li&gt;  &lt;li&gt;&lt;p&gt;nifty features such as &lt;a href="http://etorreborre.github.com/specs2/guide/org.specs2.guide.SpecStructure.html#Auto-Examples"&gt;Auto-Examples&lt;/a&gt; (an example &lt;a href="https://github.com/etorreborre/specs2/blob/1.3/src/test/scala/org/specs2/matcher/AnyMatchersSpec.scala"&gt;here&lt;/a&gt;), implicit &lt;a href="http://etorreborre.github.com/specs2/guide/org.specs2.guide.Matchers.html#Matchers+creation"&gt;Matchers creation&lt;/a&gt; or &lt;a href="http://etorreborre.github.com/specs2/guide/org.specs2.guide.Matchers.html#Json+matchers"&gt;Json matchers&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;  &lt;li&gt;&lt;p&gt;lots of small fixes and consistency changes so that writing tests/specifications is &lt;a href="http://twitter.com/#!/rit/status/69552988569608192"&gt;just a pleasure&lt;/a&gt;!&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Now that you've decided to take the ride with &lt;strong&gt;&lt;em&gt;specs2&lt;/em&gt;&lt;/strong&gt;, what are the steps for a successful migration?&lt;/p&gt;&lt;a name="Just+replace+org.specs._+with+org.specs2.mutable._"&gt;&lt;/a&gt;&lt;i&gt;&lt;b&gt;&lt;span class="Apple-style-span"&gt;Just replace &lt;code class="prettyprint"&gt;org.specs._&lt;/code&gt; with &lt;code class="prettyprint"&gt;org.specs2.mutable._&lt;/code&gt;&lt;/span&gt;&lt;/b&gt;&lt;/i&gt;&lt;p&gt;The first thing to do is to switch the base import from &lt;code class="prettyprint"&gt;org.specs._&lt;/code&gt; to &lt;code class="prettyprint"&gt;org.specs2.mutable._&lt;/code&gt;. For simple specifications, with no context setup and simple equality matchers, nothing else is required.&lt;/p&gt;&lt;p&gt;However, depending on the &lt;strong&gt;&lt;em&gt;specs&lt;/em&gt;&lt;/strong&gt; features you've been using you'll have to change a few more things:&lt;/p&gt;&lt;ol&gt;  &lt;li&gt;matchers&lt;/li&gt;  &lt;li&gt;context setup&lt;/li&gt;  &lt;li&gt;ScalaCheck&lt;/li&gt;  &lt;li&gt;miscellaneous: arguments, specification title, specification inclusions, tags, ...&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;Most of those changes have specific motivations which I leave out of this post to keep it short. If you have questions on any given change please ask on the &lt;a href="http://groups.google.com/group/specs2-users"&gt;mailing-list&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;You may also want to watch &lt;a href="http://vimeo.com/24269137"&gt;this presentation&lt;/a&gt; by @prasinous to learn how she did her own migration of the &lt;a href="http://github.com/novus/salat"&gt;Salat project&lt;/a&gt;.&lt;/p&gt;&lt;a name="Matchers"&gt;&lt;/a&gt;&lt;i&gt;&lt;b&gt;&lt;span class="Apple-style-span"&gt;&lt;h4&gt;Matchers&lt;/h4&gt;&lt;/span&gt;&lt;/b&gt;&lt;/i&gt;&lt;p&gt;Most of the matchers in &lt;em&gt;&lt;strong&gt;specs2&lt;/strong&gt;&lt;/em&gt; have simply been copied over from the same matchers in &lt;strong&gt;&lt;em&gt;specs&lt;/em&gt;&lt;/strong&gt;. There are however some differences:&lt;/p&gt;&lt;ul&gt;  &lt;li&gt;&lt;p&gt;&lt;code class="prettyprint"&gt;mustBe&lt;/code&gt;, &lt;code class="prettyprint"&gt;mustNotBe&lt;/code&gt; and other &lt;code class="prettyprint"&gt;mustXXX&lt;/code&gt; variants don't exist anymore. Only &lt;code class="prettyprint"&gt;must_==&lt;/code&gt;, &lt;code class="prettyprint"&gt;must_!=&lt;/code&gt;, &lt;code class="prettyprint"&gt;mustEqual&lt;/code&gt;, &lt;code class="prettyprint"&gt;mustNotEqual &lt;/code&gt;are left. Otherwise you have to write &lt;code class="prettyprint"&gt;must not be(x)&lt;/code&gt; instead of &lt;code class="prettyprint"&gt;mustNotBe&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;  &lt;li&gt;&lt;p&gt;the matchers having &lt;code class="prettyprint"&gt;not&lt;/code&gt; as a prefix have been removed too. You're encouraged to write &lt;code class="prettyprint"&gt;must not beEmpty&lt;/code&gt; instead of &lt;code class="prettyprint"&gt;must notBeEmpty&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;  &lt;li&gt;&lt;p&gt;the &lt;code class="prettyprint"&gt;verify&lt;/code&gt; matcher has been removed, so you should write &lt;code class="prettyprint"&gt;f(a) must beTrue&lt;/code&gt; instead of &lt;code class="prettyprint"&gt;a must verify(f)&lt;/code&gt; (or better, use &lt;a href="http://www.blogger.com/post-edit.g?blogID=5336273&amp;amp;postID=3419215371915546335"&gt;ScalaCheck&lt;/a&gt; properties!)&lt;/p&gt;&lt;/li&gt;  &lt;li&gt;&lt;p&gt;&lt;code class="prettyprint"&gt;beLike&lt;/code&gt; used to take a &lt;code class="prettyprint"&gt;PartialFunction[T, Boolean]&lt;/code&gt; as an argument. It is now &lt;code class="prettyprint"&gt;PartialFunction[T, MatchResult[_]]&lt;/code&gt; which allows better failure messages: &lt;code class="prettyprint"&gt;a must beLike { case ThisThing(b) =&amp;gt; b must be_&amp;gt;(0) }&lt;/code&gt;. If you have nothing special to assert, just return &lt;code class="prettyprint"&gt;ok&lt;/code&gt;: &lt;code class="prettyprint"&gt;a must beLike { case ThisThing(_) =&amp;gt; ok }&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;  &lt;li&gt;&lt;p&gt;the &lt;code class="prettyprint"&gt;fail()&lt;/code&gt; method has been removed in favor of the simple return of a &lt;code class="prettyprint"&gt;Failure&lt;/code&gt; object: &lt;code class="prettyprint"&gt;aFailure&lt;/code&gt; or &lt;code class="prettyprint"&gt;failure(message)&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;  &lt;li&gt;&lt;p&gt;String matchers: &lt;code class="prettyprint"&gt;ignoreCase&lt;/code&gt; and &lt;code class="prettyprint"&gt;ignoreSpace&lt;/code&gt; matchers for string equality are now constraints which can be added to the &lt;code class="prettyprint"&gt;beEqualTo &lt;/code&gt;matcher: &lt;code class="prettyprint"&gt;eEqualTo(b).ignoreSpace.ignoreCase&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;  &lt;li&gt;&lt;p&gt;Iterable matchers: &lt;code class="prettyprint"&gt;only&lt;/code&gt; and &lt;code class="prettyprint"&gt;inOrder&lt;/code&gt; are now constraints with can be applied to the &lt;code class="prettyprint"&gt;contain&lt;/code&gt; matcher instead of having dedicated matchers like &lt;code class="prettyprint"&gt;containInOrder&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;  &lt;li&gt;&lt;p&gt;Option matchers: "beSomething" is now just &lt;code class="prettyprint"&gt;beSome&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;  &lt;li&gt;&lt;p&gt;xUnit assertions have been removed&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;There are certainly other differences, I'll keep the list updated as I'll see them.&lt;/p&gt;&lt;a name="Contexts"&gt;&lt;/a&gt;&lt;i&gt;&lt;b&gt;&lt;span class="Apple-style-span"&gt;&lt;h4&gt;Contexts&lt;/h4&gt;&lt;/span&gt;&lt;/b&gt;&lt;/i&gt;&lt;p&gt;That's the tough part! There are 3 ways to manage contexts in &lt;strong&gt;&lt;em&gt;specs&lt;/em&gt;&lt;/strong&gt;:&lt;/p&gt;&lt;ul&gt;  &lt;li&gt;"automagic" variables&lt;/li&gt;  &lt;li&gt;before / after methods&lt;/li&gt;  &lt;li&gt;system / specification contexts&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;All of this has actually been reduced to the direct use of Scala natural features: the easy creation of traits and case classes. And a few support traits to avoid duplication of code. You definitely should read the &lt;a href="http://etorreborre.github.com/specs2/guide/org.specs2.guide.SpecStructure.html#Contexts"&gt;User Guide section on Contexts&lt;/a&gt; before starting your migration.&lt;/p&gt;&lt;a name="Automagic+variables"&gt;&lt;/a&gt;&lt;h5&gt;&lt;i&gt;&lt;span class="Apple-style-span"&gt;Automagic variables&lt;/span&gt;&lt;/i&gt;&lt;/h5&gt;&lt;p&gt;This was a very cool functionality of &lt;em&gt;&lt;strong&gt;specs&lt;/strong&gt;&lt;/em&gt; but also the greatest source of bugs! In &lt;strong&gt;&lt;em&gt;specs&lt;/em&gt;&lt;/strong&gt; you can nest examples, declare variables in each scope and have those variables being automatically reset when executing each example:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  "this system" should {&lt;br /&gt;   val var1 = ... &lt;br /&gt;   "example 1" in {&lt;br /&gt;     val var2 = ...&lt;br /&gt;     "subexample 1" in { ... }&lt;br /&gt;     "subexample 2" in { ... }&lt;br /&gt;   }&lt;br /&gt;   "example 2" in { ... }&lt;br /&gt; }&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Handling those variables is a lot less magic in &lt;strong&gt;&lt;em&gt;specs2&lt;/em&gt;&lt;/strong&gt;. What's the simplest way to get new variables in Scala? Simply open a new scope by placing some code into a new object! That's it, nothing more to say:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  "this system" should {&lt;br /&gt;   "example 1" in new c1 {&lt;br /&gt;     // do something with var1&lt;br /&gt;   }&lt;br /&gt;   "example 2" in new c1 {&lt;br /&gt;     // do something else with a new var1&lt;br /&gt;   }&lt;br /&gt; }&lt;br /&gt; trait c1 {&lt;br /&gt;   val var1 = ...&lt;br /&gt; }&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Well, almost :-). It turns out that the body of an &lt;code class="prettyprint"&gt;Example&lt;/code&gt; has to be something akin to a &lt;code class="prettyprint"&gt;Result&lt;/code&gt;. In order to allow our context, and everything inside, to be a &lt;code class="prettyprint"&gt;Result&lt;/code&gt;, we need to have our &lt;code class="prettyprint"&gt;c1&lt;/code&gt; trait extend the &lt;code class="prettyprint"&gt;Scope&lt;/code&gt; trait and benefit from an implicit conversion from &lt;code class="prettyprint"&gt;Scope&lt;/code&gt; to &lt;code class="prettyprint"&gt;Result&lt;/code&gt;:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  import org.specs2.specification._&lt;br /&gt;&lt;br /&gt; trait c1 extends Scope {&lt;br /&gt;   val var1 = ...&lt;br /&gt; }&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;From there, having nested contexts, like the ones in the first &lt;strong&gt;&lt;em&gt;specs&lt;/em&gt;&lt;/strong&gt; example is easy. We use inheritance to create them:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  "this system" should {&lt;br /&gt;   "example 1" in {&lt;br /&gt;     "subexample 1" in new c2 { ... }&lt;br /&gt;     "subexample 2" in new c2 { ... }&lt;br /&gt;   }&lt;br /&gt;   "example 2" in new c1 { ... }&lt;br /&gt; }&lt;br /&gt; trait c1 extends Scope { val var1 = ... }&lt;br /&gt; trait c2 extends c1 { val var2 = ... }&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;a name="Before+%2F+After"&gt;&lt;/a&gt;&lt;h6&gt;&lt;span class="Apple-style-span"&gt;&lt;i&gt;Before / After&lt;/i&gt;&lt;/span&gt;&lt;/h6&gt;&lt;p&gt;In &lt;strong&gt;&lt;em&gt;specs&lt;/em&gt;&lt;/strong&gt;, you can run setup code as you would do with any kind of JUnit code. This is done by declaring a doBefore block inside a sus:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  "this system" should {&lt;br /&gt;   doBefore(cleanAll)&lt;br /&gt;   "example 1" { ... }&lt;br /&gt;   "example 2" { ... }&lt;br /&gt; }&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;In &lt;strong&gt;&lt;em&gt;specs2&lt;/em&gt;&lt;/strong&gt;, there are several ways to do that. The first one is as simple as running that code into the Scope trait:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  "this system" should {&lt;br /&gt;   "example 1" in new c1 { ... }&lt;br /&gt;   "example 2" in new c1 { ... }&lt;br /&gt; }&lt;br /&gt; trait c1 extends Scope {&lt;br /&gt;   val var1 = ...&lt;br /&gt;   cleanAll&lt;br /&gt; }&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This works well for "before" setup but we can't easily setup any "after" behavior because we need additional machinery to make sure that the teardown code is executed even if there is a failure. This is where you can use the &lt;code class="prettyprint"&gt;After&lt;/code&gt; trait and define the &lt;code class="prettyprint"&gt;after&lt;/code&gt; method:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  "this system" should {&lt;br /&gt;   "example 1" in new c1 { ... }&lt;br /&gt;   "example 2" in new c1 { ... }&lt;br /&gt; }&lt;br /&gt; trait c1 extends Scope with After {&lt;br /&gt;   def after = // teardown code goes here&lt;br /&gt; }&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;For good measure, even if it's not necessary, there is a corresponding &lt;code class="prettyprint"&gt;Before&lt;/code&gt; trait and &lt;code class="prettyprint"&gt;before&lt;/code&gt; method for the setup code.&lt;/p&gt;&lt;a name="Remove+duplication"&gt;&lt;/a&gt;&lt;h6&gt;&lt;span class="Apple-style-span"&gt;&lt;i&gt;Remove duplication&lt;/i&gt;&lt;/span&gt;&lt;/h6&gt;&lt;p&gt;If you don't need any "local" variable in your contexts, but only before/after behavior, you can reduce the amount of code in the example above with the &lt;code class="prettyprint"&gt;AfterExample&lt;/code&gt; trait (or &lt;code class="prettyprint"&gt;BeforeExample&lt;/code&gt; for before behavior):&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  class MySpec extends Specification with AfterExample {&lt;br /&gt;&lt;br /&gt;   def after = // teardown code goes here&lt;br /&gt;&lt;br /&gt;  "this system" should {&lt;br /&gt;    "example 1" in { ... }&lt;br /&gt;    "example 2" in { ... }&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Yet another alternative is to use an &lt;em&gt;implicit context&lt;/em&gt;:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  implicit val context = new Scope with After {&lt;br /&gt;   def after = // teardown code goes here&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; "this system" should {&lt;br /&gt;   "example 1" in { ... }&lt;br /&gt;   "example 2" in { ... }&lt;br /&gt; }&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;a name="BeforeSus+%2F+AfterSus+%2F+BeforeSpec+%2F+AfterSpec"&gt;&lt;/a&gt;&lt;h6&gt;&lt;i&gt;&lt;span class="Apple-style-span"&gt;BeforeSus / AfterSus / BeforeSpec / AfterSpec&lt;/span&gt;&lt;/i&gt;&lt;/h6&gt;&lt;p&gt;Some other declarations in &lt;em&gt;&lt;strong&gt;specs&lt;/strong&gt;&lt;/em&gt;, like &lt;code class="prettyprint"&gt;beforeSpec&lt;/code&gt;, allow to specify some setup code to be executed before all the specification examples. In &lt;strong&gt;&lt;em&gt;specs2&lt;/em&gt;&lt;/strong&gt;, the Specification is seen as a sequence of Fragments and you have to insert a &lt;code class="prettyprint"&gt;Step&lt;/code&gt; Fragment at the appropriate place:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  step(cleanDB)&lt;/code&gt;&lt;/pre&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  "first example" in { ... }&lt;/code&gt;&lt;/pre&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;&lt;/code&gt;  "second example" in { ... }  &lt;/pre&gt;&lt;pre&gt;  step(println("finished!")) &lt;/pre&gt;&lt;a name="Specification+%2F+sus+contexts"&gt;&lt;/a&gt;&lt;h5&gt;&lt;span class="Apple-style-span"&gt;&lt;i&gt;Specification / sus contexts&lt;/i&gt;&lt;/span&gt;&lt;/h5&gt;&lt;p&gt;The purpose of Contexts in &lt;em&gt;&lt;strong&gt;specs&lt;/strong&gt;&lt;/em&gt; is to be able to define and reuse a given setup/teardown procedure. As seen above, in &lt;strong&gt;&lt;em&gt;specs2&lt;/em&gt;&lt;/strong&gt;, traits extending &lt;code class="prettyprint"&gt;Before&lt;/code&gt; or &lt;code class="prettyprint"&gt;After&lt;/code&gt; play exactly the same role.&lt;/p&gt;&lt;a name="ScalaCheck"&gt;&lt;/a&gt;&lt;i&gt;&lt;span class="Apple-style-span"&gt;&lt;b&gt;&lt;/b&gt;&lt;h4&gt;&lt;b&gt;ScalaCheck&lt;/b&gt;&lt;/h4&gt;&lt;/span&gt;&lt;/i&gt;&lt;p&gt;With &lt;strong&gt;&lt;em&gt;specs&lt;/em&gt;&lt;/strong&gt; there is a special matcher to check ScalaCheck properties. It comes in 4 forms:&lt;/p&gt;&lt;ol&gt;  &lt;li&gt;&lt;code class="prettyprint"&gt;property must pass&lt;/code&gt;&lt;/li&gt; &lt;li&gt;&lt;code class="prettyprint"&gt;generator must pass(function)&lt;/code&gt;&lt;/li&gt;  &lt;li&gt;&lt;code class="prettyprint"&gt;function must pass(generator)&lt;/code&gt;&lt;/li&gt;  &lt;li&gt;&lt;code class="prettyprint"&gt;generator must validate(partialFunction)&lt;/code&gt;&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;In &lt;strong&gt;&lt;em&gt;specs2&lt;/em&gt;&lt;/strong&gt; we're using the fact that the body of an Example expects anything that can be converted to a &lt;code class="prettyprint"&gt;Result&lt;/code&gt;, so there are implicit conversions transforming ScalaCheck properties and Scala functions to &lt;code class="prettyprint"&gt;Result&lt;/code&gt;s and the examples above become:&lt;/p&gt;&lt;p&gt;&lt;em&gt;(we suppose that the function to test has 2 parameters of type &lt;code class="prettyprint"&gt;T1&lt;/code&gt; and &lt;code class="prettyprint"&gt;T2&lt;/code&gt;, and 2 implicit &lt;code class="prettyprint"&gt;Arbitrary[T1]&lt;/code&gt; and &lt;code class="prettyprint"&gt;Arbitrary[T2]&lt;/code&gt; instances in scope)&lt;/em&gt;&lt;/p&gt;&lt;ol&gt;  &lt;li&gt;&lt;code class="prettyprint"&gt;"ex" in check { property }&lt;/code&gt;&lt;/li&gt;  &lt;li&gt;&lt;code class="prettyprint"&gt;"ex" in check { function }&lt;/code&gt;&lt;br /&gt;or &lt;code class="prettyprint"&gt;"ex" in check (arbitrary1, arbitrary2) { function }&lt;/code&gt; to be explicit about the &lt;code class="prettyprint"&gt;Arbitrary&lt;/code&gt; instances to use&lt;/li&gt; &lt;li&gt;no equivalent&lt;/li&gt; &lt;li&gt;no equivalent&lt;/li&gt; &lt;/ol&gt;&lt;p&gt;You can also notice that &lt;strong&gt;&lt;em&gt;specs2&lt;/em&gt;&lt;/strong&gt; uses implicit &lt;code class="prettyprint"&gt;Arbitrary&lt;/code&gt; instances instead of &lt;code class="prettyprint"&gt;Gen&lt;/code&gt; instances directly but creating an &lt;code class="prettyprint"&gt;Arbitrary&lt;/code&gt; from a &lt;code class="prettyprint"&gt;Gen&lt;/code&gt; is easy:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  import org.scalacheck._&lt;br /&gt;&lt;br /&gt; val arbitrary: Arbitrary[T] = Arbitrary(generator)&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h4&gt;Miscellaneous&lt;/h4&gt;&lt;a name="Arguments"&gt;&lt;/a&gt;&lt;i&gt;&lt;b&gt;&lt;span class="Apple-style-span"&gt;Arguments&lt;/span&gt;&lt;/b&gt;&lt;/i&gt;&lt;p&gt;This is also a part of your specifications which is likely to necessitate a change. In &lt;strong&gt;&lt;em&gt;specs&lt;/em&gt;&lt;/strong&gt; there were several ways to modify the behavior of the execution or the reporting:&lt;/p&gt;&lt;ul&gt;  &lt;li&gt;&lt;code class="prettyprint"&gt;detailedDiffs()&lt;/code&gt;&lt;/li&gt;  &lt;li&gt;&lt;code class="prettyprint"&gt;shareVariables()&lt;/code&gt;&lt;/li&gt;  &lt;li&gt;&lt;code class="prettyprint"&gt;setSequential()&lt;/code&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="http://code.google.com/p/specs/wiki/RunningSpecs#Run_your_specification_in_the_Console"&gt;Command-line options&lt;/a&gt;: &lt;code class="prettyprint"&gt;-sus&lt;/code&gt;, &lt;code class="prettyprint"&gt;-ex&lt;/code&gt;, &lt;code class="prettyprint"&gt;--color&lt;/code&gt;, &lt;code class="prettyprint"&gt;-finalstats&lt;/code&gt;,...&lt;/li&gt;  &lt;li&gt;&lt;a href="http://code.google.com/p/specs/wiki/RunningSpecs#Override_specs_default_behavior"&gt;Configuration objects&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt;&lt;p&gt;All of this has been completely redesigned in &lt;strong&gt;&lt;em&gt;specs2&lt;/em&gt;&lt;/strong&gt;:&lt;/p&gt;&lt;ul&gt;  &lt;li&gt;all the arguments can be specified &lt;a href="http://etorreborre.github.com/specs2/guide/org.specs2.guide.Runners.html#Arguments"&gt;from inside the Specification&lt;/a&gt;&lt;/li&gt;  &lt;li&gt;most of them can be passed &lt;a href="http://etorreborre.github.com/specs2/guide/org.specs2.guide.Runners.html#On+the+command+line"&gt;on the command-line&lt;/a&gt; as Strings&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;And just to be specific about what's not going to compile during your &lt;strong&gt;&lt;em&gt;specs2&lt;/em&gt;&lt;/strong&gt; migration:&lt;/p&gt;&lt;ul&gt;  &lt;li&gt;&lt;code class="prettyprint"&gt;detailedDiffs()&lt;/code&gt; needs to be replaced by a &lt;code class="prettyprint"&gt;diffs(...)&lt;/code&gt; or your own &lt;a href="http://etorreborre.github.com/specs2/guide/org.specs2.guide.Runners.html#Diffs"&gt;&lt;code class="prettyprint"&gt;Diffs&lt;/code&gt; object&lt;/a&gt;&lt;/li&gt;  &lt;li&gt;&lt;code class="prettyprint"&gt;shareVariables()&lt;/code&gt; makes no sense because all variables are shared in &lt;strong&gt;&lt;em&gt;specs2&lt;/em&gt;&lt;/strong&gt; unless you isolate them in Contexts&lt;/li&gt;  &lt;li&gt;&lt;code class="prettyprint"&gt;setSequential()&lt;/code&gt; is replaced with the addition of a &lt;code class="prettyprint"&gt;sequential&lt;/code&gt; argument&lt;/li&gt;&lt;/ul&gt;&lt;a name="DataTables"&gt;&lt;/a&gt;&lt;i&gt;&lt;b&gt;&lt;span class="Apple-style-span"&gt;DataTables&lt;/span&gt;&lt;/b&gt;&lt;/i&gt;&lt;p&gt;&lt;a href="http://code.google.com/p/specs/wiki/AdvancedSpecifications#How_to_use_Data_Tables"&gt;DataTables&lt;/a&gt; have not really changed, except for their package being &lt;code class="prettyprint"&gt;org.specs2.matcher&lt;/code&gt; instead of &lt;code class="prettyprint"&gt;org.specs.util&lt;/code&gt;. You may however get a few compilation errors because of the &lt;code class="prettyprint"&gt;!&lt;/code&gt; operator as explained &lt;a href="http://etorreborre.github.com/specs2/guide/org.specs2.guide.Matchers.html#DataTables"&gt;here&lt;/a&gt;. Just replace it with &lt;code class="prettyprint"&gt;!!&lt;/code&gt; in that case.&lt;/p&gt;&lt;a name="Specification+title"&gt;&lt;/a&gt;&lt;i&gt;&lt;b&gt;Specification title&lt;/b&gt;&lt;/i&gt;&lt;p&gt;In &lt;em&gt;&lt;strong&gt;specs&lt;/strong&gt;&lt;/em&gt; the specification title is a member of the &lt;code class="prettyprint"&gt;Specification&lt;/code&gt; &lt;em&gt;class&lt;/em&gt; whereas &lt;code class="prettyprint"&gt;Specifications&lt;/code&gt; in &lt;em&gt;&lt;strong&gt;specs2&lt;/strong&gt;&lt;/em&gt; are traits. If you want to specify a Specification title in &lt;strong&gt;&lt;em&gt;specs2&lt;/em&gt;&lt;/strong&gt; you can insert it as a Fragment:&lt;/p&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;  class MySpec extends Specification { def is =&lt;br /&gt;   "Specification title".title ^&lt;br /&gt;   ...&lt;br /&gt;                              ^ end&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; class MySpec extends mutable.Specification {&lt;br /&gt;   "Specification title".title&lt;br /&gt;   ...&lt;br /&gt; }&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;a name="Include+a+specification+in+another+one"&gt;&lt;/a&gt;&lt;i&gt;&lt;b&gt;&lt;span class="Apple-style-span"&gt;Include a specification in another one&lt;/span&gt;&lt;/b&gt;&lt;/i&gt;&lt;p&gt;This used to be done with &lt;code class="prettyprint"&gt;include&lt;/code&gt; or &lt;code class="prettyprint"&gt;isSpecifiedBy&lt;/code&gt;/&lt;code class="prettyprint"&gt;areSpecifiedBy&lt;/code&gt;. In &lt;strong&gt;&lt;em&gt;specs2&lt;/em&gt;&lt;/strong&gt; there are 3 ways to "include" other specifications with different behaviours which mostly make sense with the &lt;code class="prettyprint"&gt;HmtlRunner&lt;/code&gt;:&lt;/p&gt;&lt;p&gt;If you have a "parent" specification &lt;code class="prettyprint"&gt;spec1&lt;/code&gt;&lt;/p&gt;&lt;ol&gt;  &lt;li&gt;&lt;code class="prettyprint"&gt;include(spec2)&lt;/code&gt; will include all the Fragments of spec2 into spec1 as if they were part of it&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;code class="prettyprint"&gt;link(spec2)&lt;/code&gt; will include all the Fragments of spec2 into spec1. When executing spec1, spec2 will be executed and the html runner will create a link to a separate page for spec2&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;code class="prettyprint"&gt;see(spec2)&lt;/code&gt; will include all the Fragments of spec2 into spec1. When executing spec1, spec2 will &lt;em&gt;not&lt;/em&gt; be executed but the html runner will create a link to a separate page for spec2.&lt;/li&gt; &lt;/ol&gt;&lt;i&gt;&lt;b&gt;&lt;span class="Apple-style-span"&gt;Tags&lt;/span&gt;&lt;/b&gt;&lt;/i&gt;&lt;p&gt;The &lt;a href="http://code.google.com/p/specs/wiki/RunningSpecs#Include_or_exclude_examples"&gt;tagging system&lt;/a&gt; in &lt;strong&gt;&lt;em&gt;specs2&lt;/em&gt;&lt;/strong&gt; has been completely changed but not in way drastic way for users of the API. The major difference is that &lt;a href="http://etorreborre.github.com/specs2/guide/org.specs2.guide.SpecStructure.html#Tags"&gt;tags are positional&lt;/a&gt; which opens new possibilities for tagging like creating &lt;code class="prettyprint"&gt;Section&lt;/code&gt;s.&lt;/p&gt;&lt;a name="Conclusion"&gt;&lt;/a&gt;&lt;i&gt;&lt;b&gt;&lt;span class="Apple-style-span"&gt;Conclusion&lt;/span&gt;&lt;/b&gt;&lt;/i&gt;&lt;p&gt;There are certainly a million other small differences between your specification written with &lt;em&gt;&lt;strong&gt;specs&lt;/strong&gt;&lt;/em&gt; and what it will look like with &lt;em&gt;&lt;strong&gt;specs2&lt;/strong&gt;&lt;/em&gt;. I can only apologize in advance for the additional work, offer my best support, and hope that you'll be able to rip out more benefits and more fun of writing specifications with &lt;strong&gt;&lt;em&gt;specs2&lt;/em&gt;&lt;/strong&gt;!&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5336273-3419215371915546335?l=etorreborre.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://etorreborre.blogspot.com/feeds/3419215371915546335/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5336273&amp;postID=3419215371915546335' title='7 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5336273/posts/default/3419215371915546335'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5336273/posts/default/3419215371915546335'/><link rel='alternate' type='text/html' href='http://etorreborre.blogspot.com/2011/05/specs2-migration-guide.html' title='specs2 migration guide'/><author><name>Eric</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://farm3.static.flickr.com/2203/2118752971_7becaf7476_t.jpg'/></author><thr:total>7</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5336273.post-2251055722903072459</id><published>2011-02-18T05:58:00.011+09:00</published><updated>2011-06-03T17:49:27.135+09:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='scalacheck'/><category scheme='http://www.blogger.com/atom/ns#' term='specs'/><category scheme='http://www.blogger.com/atom/ns#' term='json'/><category scheme='http://www.blogger.com/atom/ns#' term='scala'/><title type='text'>Scalacheck generators for JSON</title><content type='html'>More than 6 months without a single post, because I've been focusing on the creation of specs2, which should be out in a few weeks (if you want to access the preview drop me a line).&lt;br /&gt;&lt;br /&gt;Among the new features of specs2 there will be JSON matchers to help those of you handling JSON data. When developing those matchers I used &lt;a href="http://code.google.com/p/scalacheck/"&gt;ScalaCheck&lt;/a&gt; to test some of the utility functions I was using. I want to show here that writing custom data generators with ScalaCheck is really easy and almost follows the grammar for the data.&lt;br /&gt;&lt;br /&gt;Here's the code:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;code class="prettyprint"&gt;&lt;br /&gt;/**&lt;br /&gt; * Generator of JSONType objects with a given tree depth&lt;br /&gt; */&lt;br /&gt;import util.parsing.json._&lt;br /&gt;import org.scalacheck._&lt;br /&gt;import Gen._&lt;br /&gt;&lt;br /&gt;trait JsonGen {&lt;br /&gt;&lt;br /&gt;  implicit def arbitraryJsonType: Arbitrary[JSONType] = &lt;br /&gt;    Arbitrary { sized(depth =&gt; jsonType(depth)) }&lt;br /&gt;   &lt;br /&gt;  /** generate either a JSONArray or a JSONObject */&lt;br /&gt;  def jsonType(depth: Int): Gen[JSONType] = oneOf(jsonArray(depth), jsonObject(depth))&lt;br /&gt;&lt;br /&gt;  /** generate a JSONArray */&lt;br /&gt;  def jsonArray(depth: Int): Gen[JSONArray] = for {&lt;br /&gt;    n    &lt;- choose(1, 4)&lt;br /&gt;    vals &lt;- values(n, depth)&lt;br /&gt;  } yield JSONArray(vals)&lt;br /&gt;&lt;br /&gt;  /** generate a JSONObject */&lt;br /&gt;  def jsonObject(depth: Int): Gen[JSONObject] = for {&lt;br /&gt;    n    &lt;- choose(1, 4)&lt;br /&gt;    ks   &lt;- keys(n)&lt;br /&gt;    vals &lt;- values(n, depth)&lt;br /&gt;  } yield JSONObject(Map((ks zip vals):_*))&lt;br /&gt;&lt;br /&gt;  /** generate a list of keys to be used in the map of a JSONObject */&lt;br /&gt;  def keys(n: Int) = listOfN(n, oneOf("a", "b", "c"))&lt;br /&gt;&lt;br /&gt;  /** &lt;br /&gt;   * generate a list of values to be used in the map of a JSONObject or in the list&lt;br /&gt;   * of a JSONArray.&lt;br /&gt;   */&lt;br /&gt;  def values(n: Int, depth: Int) = listOfN(n, value(depth))&lt;br /&gt;&lt;br /&gt;  /** &lt;br /&gt;   * generate a value to be used in the map of a JSONObject or in the list&lt;br /&gt;   * of a JSONArray.&lt;br /&gt;   */&lt;br /&gt;  def value(depth: Int) = &lt;br /&gt;    if (depth == 0) &lt;br /&gt;      terminalType &lt;br /&gt;    else &lt;br /&gt;      oneOf(jsonType(depth - 1), terminalType)&lt;br /&gt;  &lt;br /&gt;  /** generate a terminal value type */&lt;br /&gt;  def terminalType = oneOf(1, 2, "m", "n", "o")&lt;br /&gt;}&lt;br /&gt;/** import the members of that object to use the implicit arbitrary[JSONType] */&lt;br /&gt;object JsonGen extends JsonGen&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Two things to notice in the code above:&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;&lt;p&gt;The generators are recursively defined, which makes sense because the JSON data format is recursive. For example a &lt;code&gt;jsonArray&lt;/code&gt; contains values which can be a &lt;code&gt;terminalType&lt;/code&gt; or a &lt;code&gt;jsonType&lt;/code&gt;. But &lt;code&gt;jsonType&lt;/code&gt; can itself be a &lt;code&gt;jsonArray&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;The top generator used in the definition of the &lt;code&gt;Arbitrary[JSONType]&lt;/code&gt; is using a "sized" generator. This means that we can tweak the ScalaCheck parameters to use a specific "size" for our generated data. Here I've choosen to define "size" as being the depth of the generated JSON trees. This depth parameter is propagated to all generators until the &lt;code&gt;value&lt;/code&gt; generator. If depth is 0 when using that generator, this means that we reached the bottom of the Tree so we need a "terminal" value. Otherwise we generate another JSON object with a decremented depth.&lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;You can certainly tweak the code above using other ScalaCheck generators to obtain more random trees (the one above is too balanced), with a broader range of values but this should get you started. It was definitely good enough for me as I spotted a bug in my code on the first run!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5336273-2251055722903072459?l=etorreborre.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://etorreborre.blogspot.com/feeds/2251055722903072459/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5336273&amp;postID=2251055722903072459' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5336273/posts/default/2251055722903072459'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5336273/posts/default/2251055722903072459'/><link rel='alternate' type='text/html' href='http://etorreborre.blogspot.com/2011/02/scalacheck-generator-for-json.html' title='Scalacheck generators for JSON'/><author><name>Eric</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://farm3.static.flickr.com/2203/2118752971_7becaf7476_t.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5336273.post-7875293871001085015</id><published>2010-06-09T21:17:00.002+09:00</published><updated>2010-06-09T23:27:56.059+09:00</updated><title type='text'>Random Hacks of Kindness</title><content type='html'>&lt;b&gt;&lt;i&gt;Pure kindness&lt;/i&gt;&lt;/b&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;My last coding week-end is worth a few notes. I participated to the &lt;a href="http://www.rhok.org/"&gt;Random Hacks of Kindness&lt;/a&gt; event in Sydney. The idea is to gather techies and make them code during the week-end on software projects that help the organisations working on humanitarian crises, like the earthquake in Haiti.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;I joined the event thinking that, for once, the code I would create would have a direct impact on people lives! Not that working in banking, telecom, or testing tools has no benefit, for no one, but &lt;i&gt;this&lt;/i&gt; is guaranteed to help.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;First of all I want to say that the organization was flawless, with a very nice room at the University of New South Wales and all the network equipment you would expect: a proper wifi network, video and audio facilities, an additional meeting room, a nice terrace to appreciate the view around and get fresh inspiration/air.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;It is also worth mentioning that food and (non-alcoholic) drinks were provided in abundance (maybe because the crowd was less that expected :-)).&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Then I must say that one big success factor to the event was the incredible energy conveyed by &lt;a href="http://ca.linkedin.com/in/heatherleson"&gt;Heather&lt;/a&gt;, a (completely jet-lagged :-)) Canadian girl who animated and coordinated the teams locally and across the continents. A special thank also to Martin Bliemel (from the University) who co-organized and Tolmie for the videos and pictures! (including a video of me if you crawl the links below well enough, you should find it!)&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;&lt;i&gt;"Crises are chaos, so expect chaos"&lt;/i&gt;&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;&lt;i&gt;&lt;br /&gt;&lt;/i&gt;&lt;/b&gt;&lt;/div&gt;&lt;div&gt;One day before the beginning of the week-end the list of projects was published. I had a quick look, the list was &lt;a href="http://wiki.rhok.org/Projects"&gt;huge&lt;/a&gt;. My first thought was: "wow, a lot of Python over there. How can I be effective in 2 days only?".&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;I also realized that most projects had very vague specifications, some looked more like brainstormings indeed. &lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Eventually, when I arrived on Saturday morning, I had really no idea where I could bring any meaningful contribution, so I just followed someone who had chosen one project and seemed really motivated by it: the &lt;a href="http://wiki.rhok.org/Person-finder"&gt;PersonFinder&lt;/a&gt; project. It turned out that he eventually went on to something else, leaving me and 2 others continuing the project. &lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;But that's the rules of the game. As an introduction for the week-end, Heather reminded us that crises situations generate a lot of chaos, so that's only normal that we experience part of that during the week-end.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;For me, that chaos took the form of a long "What am I supposed to do for God's sake?!!" during most of the Saturday (you can read that &lt;a href="http://wiki.rhok.org/Person_Finder_Sydney_team#Saturday"&gt;here&lt;/a&gt;). &lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;We did a lot of research: what is already there? what's valuable? How could it be done? It was like a mini business plan for the day. Of course that's a bit frustrating for a software developer who wants to save the world by coding like a madman, but I kept thinking to myself that laying down the foundations of what could be a productive coding session for others, if not for me, was definitely worthwhile. So that become my objective for the week-end: &lt;/div&gt;&lt;div&gt;&lt;ol&gt;&lt;li&gt;define exactly what needs to be done&lt;/li&gt;&lt;li&gt;prepare the environment (development, test)&lt;/li&gt;&lt;li&gt;code at least a single, well-defined, small feature&lt;/li&gt;&lt;/ol&gt;&lt;div&gt;Another way of saying it: reduce the chaos,... Actually most of that became clear to me after I had a discussion with Alice, from the DC team, who had a well-defined idea of where we should go with the project. If only I had that discussion to start with,...&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;b&gt;&lt;i&gt;I learned a lot&lt;/i&gt;&lt;/b&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;&lt;i&gt;&lt;br /&gt;&lt;/i&gt;&lt;/b&gt;&lt;/div&gt;&lt;div&gt;Let's say it straight, I failed to deliver even a single small feature. But I learned a lot! On the "functional" side, I learned that:&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;there's a whole ecosystem of "crises software" with platforms like &lt;a href="http://www.sahanafoundation.org/"&gt;sahana&lt;/a&gt; (PHP, Python) or &lt;a href="http://www.ushahidi.com/"&gt;usahidi&lt;/a&gt; (PHP)&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;the integration of social web services is only at its infancy. Website like &lt;a href="http://pipl.com/"&gt;pipl&lt;/a&gt; do not offer a public API for example. There a &lt;a href="http://code.google.com/apis/socialgraph/"&gt;Google Social Graph API&lt;/a&gt;, but it's still in the Labs.&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;there is available data on the web which is incredibly difficult to grab. For example, knowing my full name, you can easily get my personal address. But for that, you have to know in which country I live and which white page directory website to use. There's no generic query for that kind of search. Hmm, I'd be interesting in knowing which "semantic web" initiative or magical &lt;a href="http://www-staff.it.uts.edu.au/~cbj/patterns/"&gt;pattern language&lt;/a&gt; addresses that in a near future&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;Web 2.0-this, Social-that, this is a bit of chaos too. Quizz question: what's Google Wave? An email client, a wiki, a code-review tool, a forum? Not only the frontiers and definitions blur, but the amount of possibly related data grows exponentially. Managing all that, including in "crisis" environments will require a lot of "social" heuristics, mixing all kind of CS and social knowledge&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;The technical side was very illuminating too, as I'm way behind the curve in terms of web technologies:&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;I learned how to install and deploy a Google Web App on the &lt;a href="http://code.google.com/appengine/"&gt;Google App Engine&lt;/a&gt; (GAE)&lt;/li&gt;&lt;li&gt;I realized that my Java skills were useful after all, with the GAE&lt;/li&gt;&lt;li&gt;I read about the&lt;a href="http://apiwiki.twitter.com/Twitter-API-Documentation"&gt; Twitter search API&lt;/a&gt; (kudos, effective and well-documented)&lt;/li&gt;&lt;li&gt;I had a look at &lt;a href="http://www.json.org/"&gt;JSON&lt;/a&gt; as a data format&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;That may seem silly but for the first time, the whole notion of webservice started to make sense to me!&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;&lt;i&gt;&lt;br /&gt;&lt;/i&gt;&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;&lt;i&gt;&lt;span class="Apple-style-span" style="font-style: normal; font-weight: normal; "&gt;&lt;b&gt;&lt;i&gt;And now?&lt;/i&gt;&lt;/b&gt;&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;&lt;i&gt;&lt;br /&gt;&lt;/i&gt;&lt;/b&gt;&lt;/div&gt;&lt;div&gt;Well, the features are still not fully delivered. Thanks to the heroic work of the DC team, we've made some &lt;a href="http://github.com/feordin/twitter-to-pfif"&gt;progress&lt;/a&gt; but nothing has been yet approved and reused. Can we do better next time? Sure we can:&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;we need to have "Product Owners". People who know what they *want* to be done and are able to specify it. Then they can validate the deliverables and can be a stable point of contact after the event is finished&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;we need more preparation on the projects: for example, what to install for development and testing, some hints about what could be used in the solution. I'm killing a bit the fun and brainstorming aspect of the event here, but the next point is&lt;/li&gt;&lt;/ul&gt;     make a clearer separation between:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;"proof of concept" projects: show me this could work&lt;/li&gt;&lt;li&gt;"brainstorming" projects: what could be cool&lt;/li&gt;&lt;li&gt;"prototype" projects: cool + some code&lt;/li&gt;&lt;li&gt;"feature" projects: add a new functionality&lt;/li&gt;&lt;li&gt;"problem-solving" projects: fix something by using a brilliant algorithm for example&lt;/li&gt;&lt;li&gt;"specification" projects: prepare the problem statement for a "feature" project&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;I think that by making clear what are the expectations and deliverables on each type of project we give more chances to the community to converge towards something eventually useful and durable.&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;My personal dilemma is that I would love to contribute more to what was started but I have a few open-source commitments that are taking priority over this at the moment. On the other hand I will be really happy to be involved next year in the preparation of the event so that everyone can get the maximum out of it. And that's because, as one of my colleagues said at work: &lt;/div&gt;&lt;div&gt;&lt;blockquote&gt;These projects have a spiritual meaning&lt;/blockquote&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5336273-7875293871001085015?l=etorreborre.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://etorreborre.blogspot.com/feeds/7875293871001085015/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5336273&amp;postID=7875293871001085015' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5336273/posts/default/7875293871001085015'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5336273/posts/default/7875293871001085015'/><link rel='alternate' type='text/html' href='http://etorreborre.blogspot.com/2010/06/random-hacks-of-kindness.html' title='Random Hacks of Kindness'/><author><name>Eric</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://farm3.static.flickr.com/2203/2118752971_7becaf7476_t.jpg'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5336273.post-3481158681725589129</id><published>2010-05-28T07:04:00.005+09:00</published><updated>2010-07-22T03:50:05.145+09:00</updated><title type='text'>OSCON 2010</title><content type='html'>&lt;b&gt;&lt;i&gt;The slides&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Those are the slides I used to present my talk:&lt;br /&gt;&lt;br /&gt;&lt;a href="http://github.com/etorreborre/oscon-2010/tree/master/src/main/resources/oscon_2010_specs.pptx"&gt;http://github.com/etorreborre/oscon-2010/raw/master/src/main/resources/oscon_2010_specs.pptx&lt;/a&gt;&lt;br /&gt;&lt;a href="http://github.com/etorreborre/oscon-2010/tree/master/src/main/resources/oscon_2010_specs.pptx"&gt;http://github.com/etorreborre/oscon-2010/raw/master/src/main/resources/oscon_2010_specs.pdf&lt;/a&gt;&lt;br /&gt;&lt;div&gt;&lt;br /&gt;&lt;b&gt;&lt;i&gt;Thank you&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;I want to add a thank to:&lt;div&gt;&lt;ul&gt;&lt;li&gt;Dean Wampler and Alex Payne for thinking about me to make a presentation&lt;/li&gt;&lt;li&gt;Edd Dumbill and Shirley Bailes for the organization&lt;/li&gt;&lt;li&gt;Scott Berkun (&lt;a href="http://www.scottberkun.com/"&gt;www.scottberkun.com&lt;/a&gt;) for giving useful tips to make my talk "suck less"&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5336273-3481158681725589129?l=etorreborre.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://etorreborre.blogspot.com/feeds/3481158681725589129/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5336273&amp;postID=3481158681725589129' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5336273/posts/default/3481158681725589129'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5336273/posts/default/3481158681725589129'/><link rel='alternate' type='text/html' href='http://etorreborre.blogspot.com/2010/05/oscon-2010.html' title='OSCON 2010'/><author><name>Eric</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://farm3.static.flickr.com/2203/2118752971_7becaf7476_t.jpg'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5336273.post-8861327367688538999</id><published>2010-05-13T16:11:00.013+09:00</published><updated>2010-05-21T11:44:51.601+09:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='testcomponent'/><category scheme='http://www.blogger.com/atom/ns#' term='guice'/><category scheme='http://www.blogger.com/atom/ns#' term='test'/><category scheme='http://www.blogger.com/atom/ns#' term='junit'/><title type='text'>GuiceModules and TestComponents, a powerful combination</title><content type='html'>&lt;br/&gt;&lt;br /&gt;&lt;b&gt;&lt;i&gt;Important update!! [21/05/2010, see at the bottom]&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;&lt;br/&gt;&lt;br /&gt;&lt;i&gt;&lt;b&gt;Sorry everyone, that doesn't work&lt;/b&gt;&lt;/i&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;In this &lt;a href="http://etorreborre.blogspot.com/2010/03/we-need-algebra-for-guice-modules.html"&gt;previous post&lt;/a&gt;, I was mentioning a way to inject the dependencies of a TestComponent without having to inject the component itself.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;The objective was, if I pick up the example I was using, to have a TestComponent called "CustomerOrder". That TestComponent is responsible for setting up a proper Customer and its Order, so that they both can be injecting into the TestCases needing them. But the additional requirement was not to have to inject the CustomerOrder component itself.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;It turns out that what I was advising to do, create a binding of the CustomerOrder in the CustomerOrderModule doesn't work at all! &lt;br /&gt;&lt;br /&gt;And all my other attempts at solving this issue usually ended up in initialization errors where a TestComponent was relying on another for his setup, but that other one was not initialized.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;i&gt;&lt;b&gt;The proper trick&lt;/b&gt;&lt;/i&gt;&lt;/div&gt;&lt;div&gt;&lt;i&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/i&gt;&lt;/div&gt;&lt;div&gt;&lt;i&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-style: normal; font-weight: normal; "&gt;It is actually simple and I don't understand why I haven't been able to come up with it in the first place.&lt;/span&gt;&lt;/b&gt;&lt;/i&gt;&lt;/div&gt; The idea is to:&lt;ul&gt;&lt;li&gt;get a set of all the modules, including their transitive dependencies&lt;/li&gt;&lt;li&gt;create an injector for this set of modules&lt;/li&gt;&lt;li&gt;&lt;b&gt;inject the modules themselves!&lt;/b&gt;&lt;/li&gt;&lt;/ul&gt;&lt;code class="prettyprint"&gt;&lt;br /&gt;   /**&lt;br /&gt;    * Use all the bindings of this module, including the additional ones&lt;br /&gt;    * to inject dependencies&lt;br /&gt;    * &lt;br /&gt;    */&lt;br /&gt;   public &lt;T&gt; T inject(final T object) {&lt;br /&gt;     injector().injectMembers(object);&lt;br /&gt;     return object;&lt;br /&gt;   }&lt;br /&gt;   /**&lt;br /&gt;    * @return an injector initialized with all the transitive bindings of this module&lt;br /&gt;    */&lt;br /&gt;   private Injector injector() {&lt;br /&gt;     if (injector == null) {&lt;br /&gt;        injector = Guice.createInjector(getModules());&lt;br /&gt;        injectModules();&lt;br /&gt;     }&lt;br /&gt;     return injector;&lt;br /&gt;   }&lt;br /&gt;   /**&lt;br /&gt;    * modules are injected one after the other following their dependencies order:&lt;br /&gt;    * If module A depends on module B, then module B must be injected first &lt;br /&gt;    */&lt;br /&gt;   private void injectModules() {&lt;br /&gt;     final List&lt;GuiceModule&gt; sorted = new ArrayList&lt;GuiceModule&gt;(getModules());&lt;br /&gt;     Collections.sort(sorted, new GuiceModuleComparator());&lt;br /&gt;     for (final GuiceModule m : sorted)&lt;br /&gt;       injector.injectMembers(m);&lt;br /&gt;   }&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Because the modules are now injected, if you add a dependency &lt;i&gt;inside the module&lt;/i&gt;, that dependency will be initialized when the Module is injected. So nothing stops me to inject the CustomerOrder TestComponent into its module so that &lt;i&gt;anytime someone uses the module&lt;/i&gt;, the CustomerOrder TestComponent will be fully set-up.&lt;br /&gt;&lt;br /&gt;Now, how does the code above solves the initialization issues I've been mentioning?&lt;br /&gt;&lt;br /&gt;Well, I guess that you noticed something in that code, &lt;i&gt;the modules are sorted before they're injected&lt;/i&gt;. Indeed they are sorted following the "natural" pre-order of their transitive dependencies with the following Comparator:&lt;br /&gt;&lt;code class="prettyprint"&gt;&lt;br /&gt;  private static class GuiceModuleComparator implements Comparator&lt;GuiceModule&gt; {&lt;br /&gt;    public int compare(final GuiceModule o1, final GuiceModule o2) {&lt;br /&gt;      if (o1.getModules().contains(o2))&lt;br /&gt;        return 1;&lt;br /&gt;      else if (o2.getModules().contains(o1))&lt;br /&gt;        return -1;&lt;br /&gt;      return 0;&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;This sorting ensures that "low-level" modules will be injected first, then modules depending on them will be injected, respecting the actual dependencies.&lt;br /&gt;&lt;br /&gt;So now I can write:&lt;br /&gt;&lt;code class="prettyprint"&gt;&lt;br /&gt;  /** &lt;br /&gt;   * The bindings definitions&lt;br /&gt;   */&lt;br /&gt;  public class CustomerOrderModule extends GuiceModule {&lt;br /&gt;    // this injection will setup the Customer/Order before they're used anywhere&lt;br /&gt;    @Inject CustomerOrder customerOrder;&lt;br /&gt;    @Override protected void configure() { &lt;br /&gt;      bind(Customer.class).toInstance(new Customer());&lt;br /&gt;      bind(Order.class).toInstance(new Order());&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;  /** &lt;br /&gt;   * The CustomerOrder TestComponent setting up a "typical" &lt;br /&gt;   * Customer and its Order&lt;br /&gt;   */&lt;br /&gt;  public class CustomerOrder extends TestComponent {&lt;br /&gt;    @Inject Customer customer;&lt;br /&gt;    @Inject Order order;&lt;br /&gt;    @Override protected void before() { &lt;br /&gt;      // set up the Customer and order instances here&lt;br /&gt;      order.setCustomer(customer);&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;  /** &lt;br /&gt;   * A test case using the Customer and its Order&lt;br /&gt;   */&lt;br /&gt;  public class OrderingTestCaseextends TestComponent {&lt;br /&gt;    // the customer and order can be injected without having to&lt;br /&gt;    // inject the CustomerOrder TestComponent!!&lt;br /&gt;    @Inject Customer customer;&lt;br /&gt;    @Inject Order order;&lt;br /&gt;&lt;br /&gt;    @Test public void anOrderMustHaveACustomer() {&lt;br /&gt;      assertEquals(customer, order.getCustomer());&lt;br /&gt;    }&lt;br /&gt;    @Override public GuiceModule module() { &lt;br /&gt;      return new CustomerOrderModule();&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;i&gt;&lt;b&gt;Please ask&lt;/b&gt;&lt;/i&gt;&lt;br /&gt;&lt;br /&gt;That's all there is to it. I understand that this may be a bit difficult to grasp at first, but then you realize that you only have a few concepts:&lt;ul&gt;&lt;li&gt;a GuiceModule specifies bindings&lt;/li&gt;&lt;li&gt;GuiceModules can to have dependencies to other GuiceModules&lt;/li&gt;&lt;li&gt;a TestComponent is either setting data (aka TestFixture) or is a TestCase&lt;/li&gt;&lt;li&gt;a TestComponent specifies its GuiceModule to define its required bindings&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;Give it a go, I've been using that for a while now on my project, and the flexibility we got for defining and reusing TestComponents has been really great. And if you have any trouble don't hesitate to ask, I'll be happy to provide some more complete code samples and explanations.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;i&gt;Important update!! [21/05/2010]&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Live and learn, they say. Well the sorting of modules proposed above doesn't work either. What I really want is a &lt;a href="http://en.wikipedia.org/wiki/Topological_sorting"&gt;topological sort&lt;/a&gt;. I'm glad that Cedric Beust posted &lt;a href="http://beust.com/weblog2/archives/000525.html"&gt;this article&lt;/a&gt; once, explaining how he was dealing with the dependencies in TestNG, because that's essentially the same problem here.&lt;br /&gt;&lt;br /&gt;So, for the courageous who want to follow the GuiceModules/TestComponent way, here is the sorting code you will need:&lt;br /&gt;&lt;code class="prettyprint"&gt;&lt;br /&gt;/**&lt;br /&gt; * This sort algorithm is taken from http://en.wikipedia.org/wiki/Topological_sorting&lt;br /&gt; * @return the list of sorted modules according to the Topological order&lt;br /&gt; */&lt;br /&gt;public List&lt;GuiceModule&gt; sortedModules() {&lt;br /&gt; final List&lt;GuiceModule&gt; result = new ArrayList&lt;GuiceModule&gt;();&lt;br /&gt; final Map&lt;GuiceModule, Boolean&gt; visited = new HashMap&lt;GuiceModule, Boolean&gt;();&lt;br /&gt; for (final GuiceModule m : getModules())&lt;br /&gt;   visited.put(m, false);&lt;br /&gt; for (final GuiceModule m : getModules())&lt;br /&gt;   visit(m, result, visited);&lt;br /&gt; return new ArrayList&lt;GuiceModule&gt;(result);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/**&lt;br /&gt; * visit a module and add it only if it comes after other modules&lt;br /&gt; */&lt;br /&gt;private void visit(final GuiceModule m, final List&lt;GuiceModule&gt; result, final Map&lt;GuiceModule, Boolean&gt; visited) {&lt;br /&gt;  if (!visited.get(m)) {&lt;br /&gt;    visited.put(m, true);&lt;br /&gt;    for (final GuiceModule other : getModules()) {&lt;br /&gt;      if (!m.equals(other) &amp;&amp; !visited.get(other) &amp;&amp; GuiceModuleComparator.compare(other, m) &lt; 0)&lt;br /&gt;        visit(other, result, visited);&lt;br /&gt;      result.add(m);&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;This is exactly the time when I don't regret reading blog posts as a morning routine in the morning!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5336273-8861327367688538999?l=etorreborre.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://etorreborre.blogspot.com/feeds/8861327367688538999/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5336273&amp;postID=8861327367688538999' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5336273/posts/default/8861327367688538999'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5336273/posts/default/8861327367688538999'/><link rel='alternate' type='text/html' href='http://etorreborre.blogspot.com/2010/05/guicemodules-and-testcomponents.html' title='GuiceModules and TestComponents, a powerful combination'/><author><name>Eric</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://farm3.static.flickr.com/2203/2118752971_7becaf7476_t.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5336273.post-7735802123472438238</id><published>2010-05-06T20:30:00.016+09:00</published><updated>2010-05-07T13:52:15.139+09:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='parsercombinator'/><category scheme='http://www.blogger.com/atom/ns#' term='specs'/><category scheme='http://www.blogger.com/atom/ns#' term='scala'/><title type='text'>Mini-Parsers to the rescue</title><content type='html'>&lt;b&gt;&lt;i&gt;An example of curryfication&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;I'm implementing a reasonably complex algorithm at the moment with different transformation phases. One transformation is a "curryfication" of some terms to be able to transform some expressions like:&lt;br /&gt;&lt;br /&gt;&lt;code class="prettyprint"&gt;f(a, b, c)&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;to&lt;br /&gt;&lt;br /&gt;&lt;code class="prettyprint"&gt;.(.(.(f, a), b), c)&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Being test-compulsive, I want to be able to test that my transformation works. Unfortunately the data structures I'm dealing with are pretty verbose in my tests.&lt;br /&gt;&lt;br /&gt;One of my &lt;a href="http://code.google.com/p/specs/"&gt;specs&lt;/a&gt; examples was this:&lt;br /&gt;&lt;code class="prettyprint"&gt;&lt;br /&gt;"A composed expression, when curried" should {&lt;br /&gt;  "be 2 successive applications of one parameter" + &lt;br /&gt;  "when there are 2 parameters to the method expression" in {&lt;br /&gt;    ComposedExp(MethodEx(method), const :: arb :: Nil).curryfy must_==&lt;br /&gt;      Apply(Apply(Curry(method), Curry(const)), Curry(arb))&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;But  &lt;code class="prettyprint"&gt;Apply(Apply(Curry(method), Curry(const)), Curry(arb)))&lt;/code&gt; is really less readable than &lt;code class="prettyprint"&gt;.(.(method, const), arb)&lt;/code&gt;. And this can only get worse on a more complex example.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;i&gt;With the help of a mini-parser&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;So I thought that I could just write a parser to recreate a "Curried" expression from a String.&lt;br /&gt;&lt;br /&gt;A first look at the &lt;a href="http://www.scala-lang.org/archives/downloads/distrib/files/nightly/docs/library/index.html"&gt;Scala 2.8.0 Scaladoc&lt;/a&gt;(filter with "parser") was a bit scary. Especially because my last parser exercise was really &lt;a href="http://etorreborre.blogspot.com/2008/07/my-first-icfp-contest-part-2-parser.html"&gt;a long time ago&lt;/a&gt; now.&lt;br /&gt;&lt;br /&gt;But no, parser combinators and especially small, specific parsers like the one I wrote, are really straightforward:&lt;br /&gt;&lt;code class="prettyprint"&gt;&lt;br /&gt;object CurriedParser extends JavaTokenParsers {&lt;br /&gt;  val parser: Parser[Curried] = application | constant&lt;br /&gt;  val constant = ident ^^ { s =&gt; Curry(s) }&lt;br /&gt;  val application = (".(" ~&gt; parser) ~ (", " ~&gt; constant &lt;~ ")") ^^ { case a ~ b =&gt; &lt;br /&gt;    Apply(a, b) &lt;br /&gt;  }    &lt;br /&gt;  def fromString(s: String): Curried = parser.apply(new CharSequenceReader(s)).get&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;In the snippet above I declare that:&lt;ul&gt;&lt;br /&gt;&lt;li&gt;my parser returns a Curried object&lt;/li&gt;&lt;br /&gt;&lt;li&gt;my parser is either an application or a constant (with the &lt;code&gt;|&lt;/code&gt; operator)&lt;/li&gt;&lt;br /&gt;&lt;li&gt;a constant is a Java identifier (&lt;code&gt;ident&lt;/code&gt; is a parser inherited from the JavaTokenParsers trait)&lt;/li&gt;&lt;br /&gt;&lt;li&gt;a constant should be transformed to a &lt;code&gt;Curry object&lt;/code&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;an application is composed of two parts (separated by the central &lt;code&gt;~&lt;/code&gt; operator). The first part is: some syntax (&lt;code&gt;"("&lt;/code&gt;) and  the result of something to parse recursively with the parser (i.e. an application or a constant). The second part is a constant, surrounded by some syntax (&lt;code&gt;", "&lt;/code&gt; and &lt;code&gt;")"&lt;/code&gt;). Note that the syntactic elements are being discarded by using &lt;code&gt;~&gt;&lt;/code&gt; and &lt;code&gt;&lt;~&lt;/code&gt; instead of &lt;code&gt;~&lt;/code&gt;.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;once an application is parsed, part 1 and part 2 are accessible as matchable object of the form &lt;code&gt;a ~ b&lt;/code&gt; and this object can be used to build an &lt;code&gt;Apply object&lt;/code&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;the &lt;code&gt;fromString&lt;/code&gt; method simply passes a String to the parser and gets the result&lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;I have to say that this parser is really rudimentary. It doesn't handle errors in the input text, there absolutely needs to be a space after the comma, and so on.&lt;br /&gt;&lt;br /&gt;Yet it really fits my testing purpose for a minimum amount of development time. &lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;i&gt;Open question&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;I hope that this post can serve as an example to anyone new to Scala wanting to play with parsers and I leave an open question for senior Scala developers:&lt;br /&gt;&lt;br /&gt;Is there a way to extends the case classes representing algebraic datatypes in Scala so that:&lt;ul&gt;&lt;li&gt;each case class has a proper toString representation (that's already the case and that's one benefit of case classes), but that representation can be overriden (for example to replace &lt;code&gt;Apply(x, y)&lt;/code&gt; with &lt;code&gt;.(x, y)&lt;/code&gt;)&lt;/li&gt;&lt;br /&gt;&lt;li&gt;there is an implicit parser that is able to reconstruct the hierarchy of objects from its string representation&lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5336273-7735802123472438238?l=etorreborre.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://etorreborre.blogspot.com/feeds/7735802123472438238/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5336273&amp;postID=7735802123472438238' title='9 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5336273/posts/default/7735802123472438238'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5336273/posts/default/7735802123472438238'/><link rel='alternate' type='text/html' href='http://etorreborre.blogspot.com/2010/05/mini-parsers-to-rescue.html' title='Mini-Parsers to the rescue'/><author><name>Eric</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://farm3.static.flickr.com/2203/2118752971_7becaf7476_t.jpg'/></author><thr:total>9</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5336273.post-2933112511054399689</id><published>2010-03-10T16:39:00.021+09:00</published><updated>2010-03-11T22:33:54.941+09:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='guice'/><category scheme='http://www.blogger.com/atom/ns#' term='test'/><category scheme='http://www.blogger.com/atom/ns#' term='mockito'/><title type='text'>We need an algebra for Guice modules</title><content type='html'>Or,... I missed something (which is still highly probable).&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Anyway, I want to share today the small library I've created to ease the life of my co-workers when faced with the tedious task of writing unit tests.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;b&gt;&lt;i&gt;I won't do that again and again (and again)&lt;/i&gt;&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;I love writing unit tests. Ah, the smell of the CPU burning cycles to pass all those little creatures to green! Yet, starting from a blank page can be really tedious. &lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;If you're like me, working with some kind of major Client-Server system, with tons of business objects and a fair deal of interfaces, you may find that just starting writing the first line of test code is no piece of cake. You're likely to need:&lt;/div&gt;&lt;ul&gt;&lt;li&gt;some kind of "reference" data in your system, like a list of customers or a list of products&lt;/li&gt;&lt;li&gt;some live-like data, like current prices&lt;/li&gt;&lt;li&gt;some elaborate building of your "object under test", like a ConfirmationProcessor for orders confirmation&lt;/li&gt;&lt;li&gt;some infrastructure to make sure that you don't need to go to a real database (think "mocks" here) &lt;/li&gt;&lt;li&gt;some infrastructure to make sure that you don't need to access external webservices (think "mock" one more time)&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;Phew! I can pretty much guarantee that if you've gone through the trouble of coding all this machinery once, for your first test class, you will desperately want to reuse it a for your next test class!&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;&lt;i&gt;Injection is for everyone: interfaces, objects, mocks and, yes, tests&lt;/i&gt;&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;If I rephrase the requirements above a bit differently, what I really, really, want is the ability to create &lt;i&gt;Test Components&lt;/i&gt; where:&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;one Test Component represents my Server, with all interfaces being mocks (those are RMI interfaces in my case)&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;one Test Component provides a set of factories to create business objects easily and make as if they were saved on the Server (this uses the previous Test Component, right?).  Once initialized, this component maken sure that a mininum of default "reference" objects are already saved (a customer and a product for example)&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;one Test Component named CustomerOrder represents the "typical" customer order, with all necessary customer data, product selection,... This component also provides easy methods to change the product or the payment details, possibly using a DSL to do that concisely&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;one Test Component is for the ConfirmationProcessor, what you really want to test, an object which creates and sends confirmations to the customer for their orders. This component, for example, is using a mock for the &lt;i&gt;DiscountCalculator&lt;/i&gt; because it is not relevant in the context of sending confirmations&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div&gt;It must be clear, from the description above, that those Test Components may have lots of dependencies between them. Instantiating all those components properly can rapidly become a nightmare, but it happens that we know exactly &lt;a href="http://en.wikipedia.org/wiki/Dependency_injection"&gt;how to deal with this nowadays&lt;/a&gt;. &lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Different shapes and flavors are available for Dependency Injection and I went with my favorite: &lt;a href="http://en.wikipedia.org/wiki/Google_Guice"&gt;Google Guice&lt;/a&gt;. Guice is going to "inject" all the required objects for my test:&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;a mock Server&lt;/li&gt;&lt;li&gt;a set of factories&lt;/li&gt;&lt;li&gt;a typical customer order&lt;/li&gt;&lt;li&gt;the context of my test&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;b&gt;&lt;i&gt;Show me the code&lt;/i&gt;&lt;/b&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;First of all, I need to show what I mean by &lt;i&gt;TestComponent&lt;/i&gt;:&lt;br /&gt;&lt;pre class="prettyprint"&gt;&lt;br /&gt;/** something with a Guice module */&lt;br /&gt;public interface HasGuiceModule {&lt;br /&gt;  GuiceModule module();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/**&lt;br /&gt; * A simple test component using a Guice Module to inject its members&lt;br /&gt; * when invoked by JUnit through the @Before annotation.&lt;br /&gt; *&lt;br /&gt; * If necessary, after the members injection, the subclass can use the localSetup method&lt;br /&gt; * to add more initialization (like creating an initial customer) or more&lt;br /&gt; * expectations (for mock objects)&lt;br /&gt; */&lt;br /&gt;public abstract class TestComponent implements HasGuiceModule {&lt;br /&gt;  @Before&lt;br /&gt;  public void setup() {&lt;br /&gt;    inject();&lt;br /&gt;    localSetup();&lt;br /&gt;    expectations();&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  private void inject() {&lt;br /&gt;    module().inject(this);&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  protected void localSetup() {}&lt;br /&gt;  protected void expectations() {}&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;div&gt;The component above is really JUnit-oriented but you could as easily add a main method which would call the setup.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;Armed with this, I can create my first concrete Test Components, leaving out the &lt;code class="prettyprint"&gt;module()&lt;/code&gt; method definition for now:&lt;/div&gt;&lt;div&gt;&lt;pre class="prettyprint"&gt;public class MockServer extends TestComponent {&lt;br /&gt;  @Inject&lt;br /&gt;  Connection connection;&lt;br /&gt;&lt;br /&gt;  @Inject&lt;br /&gt;  CustomerInterface customers;&lt;br /&gt;&lt;br /&gt;  @Inject&lt;br /&gt;  ProductInterface products;&lt;br /&gt;&lt;br /&gt;  @Inject&lt;br /&gt;  OrderInterface orders;&lt;br /&gt;&lt;br /&gt;  // do all the wiring when the members have been injected with Guice&lt;br /&gt;  @Override&lt;br /&gt;  protected void expectations() {&lt;br /&gt;    when(connection.getCustomersInterface()).thenReturn(customers);&lt;br /&gt;    when(connection.getProductsInterface()).thenReturn(products);&lt;br /&gt;    when(connection.getOrdersInterface()).thenReturn(orders);&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  protected GuiceModule module() { /** to be defined later */ }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public class Factories extends TestComponent {&lt;br /&gt;  // this server interface provides methods to individual objects: a customer&lt;br /&gt;  // a payment method, a customer address,...&lt;br /&gt;  @Inject CustomerInterface customers;&lt;br /&gt;&lt;br /&gt;  // this factory provide easy to use methods to create&lt;br /&gt;  // fully setup customers with delivery address, payment method,...&lt;br /&gt;  // it uses the CustomerInterface interface to do so&lt;br /&gt;  @Inject CustomerFactory customerFactory;&lt;br /&gt;&lt;br /&gt;  protected GuiceModule module() { /** to be defined later */ }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public class ConfirmationProcessorTest extends TestComponent {&lt;br /&gt;&lt;br /&gt;  // the class under test&lt;br /&gt;  @Inject ConfirmationProcessor processor;&lt;br /&gt;&lt;br /&gt;  // the standard order, it uses the OrderFactory and CustomerFactory to save and&lt;br /&gt;  // update the customer and order&lt;br /&gt;  @Inject CustomerOrder customerOrder;&lt;br /&gt;&lt;br /&gt;  @Test public void aConfirmationForAGoldCustomerMustDisplayStars() {&lt;br /&gt;    // a DSL for describing customer types&lt;br /&gt;    customerOrder.setCustomerType("Gold 5Y 4*");&lt;br /&gt;    confirmationMustContain("*******");&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  private void confirmationMustContain(String content) {&lt;br /&gt;    assertTrue(processor.&lt;br /&gt;               confirmationFor(customerOrder.customer()).contains(content));&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  protected GuiceModule module() { /** to be defined later */ }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;The code above shows a classic JUnit4 class, with one method annotated with &lt;code class="prettyprint"&gt;@Test&lt;/code&gt;. The nice things to notice are:&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;the test data is injected in the form of a Test Component with a ready-to-use initial state&lt;/li&gt;&lt;li&gt;that Test Component provides convenient ways to refine the initial state with data relevant to the test objective&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div&gt;&lt;i&gt;&lt;b&gt;So what's Guicy here?&lt;/b&gt;&lt;/i&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;So far, so good, but you must definitely feel that I left out part of the actual magic here. What about this &lt;code class="prettyprint"&gt;module()&lt;/code&gt; method? How are &lt;code class="prettyprint"&gt;GuiceModule&lt;/code&gt;s defined? And why a &lt;code class="prettyprint"&gt;GuiceModule&lt;/code&gt; and not a Guice,...,&lt;code class="prettyprint"&gt;Module&lt;/code&gt; (i.e. &lt;code class="prettyprint"&gt;com.google.inject.Module&lt;/code&gt;)? &lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;The &lt;code class="prettyprint"&gt;module()&lt;/code&gt; method is very straightforward. It just returns a GuiceModule which describes &lt;/div&gt;&lt;div&gt;the bindings for the TestComponent:&lt;/div&gt;&lt;div&gt;&lt;pre class="prettyprint"&gt;public ConfirmationProcessor extends TestComponent {&lt;br /&gt;  protected void module() {&lt;br /&gt;    return new ConfirmationProcessorModule();&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;// and&lt;br /&gt;public ConfirmationProcessorModule extends GuiceModule {&lt;br /&gt;  // bindings go here&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;So, what is a &lt;code class="prettyprint"&gt;GuiceModule?&lt;/code&gt; Actually, a &lt;code class="prettyprint"&gt;GuiceModule&lt;/code&gt; is mostly a regular &lt;code class="prettyprint"&gt;com.google.inject.AbstractModule&lt;/code&gt;. It allows you to describe the bindings for a specific TestComponent. However, it is &lt;i&gt;refinable&lt;/i&gt;, &lt;i&gt;test-friendly&lt;/i&gt; and &lt;i&gt;composable &lt;/i&gt;with other modules. &lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Let's see how it works with several examples:&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;A simple module&lt;pre class="prettyprint"&gt;public MockServerModule extends GuiceModule {&lt;br /&gt;  @Override protected void configure() {&lt;br /&gt;    // equivalent to bind(Customers.class).toInstance(mock(Customers.class))&lt;br /&gt;    // this binding declares that in every TestComponent using this GuiceModule&lt;br /&gt;    // the Customers interface will be a mock&lt;br /&gt;    mockAndBind(CustomerInterface.class);&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;A module dependent on another&lt;pre class="prettyprint"&gt;public MockFactoriesModule extends GuiceModule {&lt;br /&gt;  @Override protected void configure() {&lt;br /&gt;    // in these bindings, we declare that the factories are&lt;br /&gt;    // going to use an in-memory representation of the database&lt;br /&gt;    // the job of a Mock database is to use the mock server interfaces&lt;br /&gt;    // to make as if a saved object was always returned when required&lt;br /&gt;    bind(CustomersDatabase.class).to(MockCustomersDatabase.class);&lt;br /&gt;  }&lt;br /&gt;  @Override protected Set&amp;lt;GuiceModule&amp;gt; modules() {&lt;br /&gt;    // add a MockServerModule to the list of dependent modules&lt;br /&gt;    return module(MockServerModule.class);&lt;br /&gt;  }&lt;br /&gt;}&lt;/pre&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;A module with dependencies and local mocks&lt;pre class="prettyprint"&gt;public ConfirmationProcessorModule extends GuiceModule {&lt;br /&gt;  @Override protected List&amp;lt;Class&amp;lt;?&amp;gt;&amp;gt; mocks() {&lt;br /&gt;    return classes(DiscountCalculator.class);&lt;br /&gt;  }&lt;br /&gt;  @Override protected Set&amp;lt;GuiceModule&amp;gt; modules() {&lt;br /&gt;    return modules(CustomerOrderModule.class);&lt;br /&gt;  }&lt;br /&gt;}&lt;/pre&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;A module refining another one and composed with other modules for a complex setup&lt;/li&gt;&lt;/ul&gt;&lt;pre class="prettyprint"&gt;new ConfirmationModule() {&lt;br /&gt;  @Override protected List&amp;lt;Class&amp;lt;?&amp;gt;&amp;gt; spies() { &lt;br /&gt;    return classes(ConfirmationProcessor.class); &lt;br /&gt;  }&lt;br /&gt;}.remove(MockPrinterModule.class).add(RealPrinterModule.class)&lt;/pre&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;As you can see on those examples, there is a lot of flexibility to allow you to reuse your existing modules as much as possible:&lt;/div&gt;&lt;ul&gt;&lt;li&gt;by refining them with additional mocks / spies (subclassing the mocks method)&lt;/li&gt;&lt;li&gt;by adding another module to form a larger one&lt;/li&gt;&lt;li&gt;by removing / replacing another module &lt;/li&gt;&lt;/ul&gt;&lt;div&gt;One question however remains unanswered. The dependencies among modules could be rather hairy and knowing that you can't declare bindings more than once with Guice, how do you manage to avoid conflicts? You can't afford to add modules referencing one another and face the nightmare of digging out why a given module has been referenced twice.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;i&gt;&lt;b&gt;Transitive dependencies and Set of GuiceModules&lt;/b&gt;&lt;/i&gt;&lt;/div&gt;&lt;br /&gt;The answer to this issue is simple. It is coded in the &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;GuiceModule#getModules()&lt;/span&gt; method:&lt;pre class="prettyprint"&gt;public Set&amp;lt;GuiceModule&amp;gt; getModules() {&lt;br /&gt;  Set&amp;lt;GuiceModule&amp;gt; result= new HashSet&amp;lt;GuiceModule&amp;gt;();&lt;br /&gt;  result.addAll(dependentModules); // a private list of added modules&lt;br /&gt;  result.addAll(modules()); // the ones declared by the subclass&lt;br /&gt;&lt;br /&gt;  for(GuiceModule m: modules()) {&lt;br /&gt;    result.addAll(m.getModules());&lt;br /&gt;  }&lt;br /&gt;  for(GuiceModule m: modulesToRemove()) {&lt;br /&gt;    result.removeAll(m.getModules());&lt;br /&gt;  }&lt;br /&gt;  result.add(this);&lt;br /&gt;  return result;&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;The &lt;code class="prettyprint"&gt;GuiceModule#getModules()&lt;/code&gt; method adds or removes modules following all dependencies transitively (and recursively). The design is also kept voluntarily simple here. A Set makes sure that 2 "same" modules are never returned based on them being equal. And 2 modules are considered to be equal if they have the same class. &lt;br /&gt;&lt;br /&gt;This forbids the possibility of parameterizing modules but I haven't found the need yet to do that. My guess is also that evolving the design to accommodate that situation shouldn't be too difficult (add a smarter equal method).&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;Now my real question to the knowledgeable people around here is: is there a better way to do so with Guice? &lt;br /&gt;&lt;br /&gt;I've found a way to override bindings from a module with bindings from another module. I've seen that you can create a hierarchy of injectors. I've seen that you can use scopes to specify the applicability of your bindings. But I haven't found anything like a simple &lt;i&gt;"algebra"&lt;/i&gt; for modules so that they can be added, subtracted (or unioned / intersected if you prefer). Maybe this post title is provocative enough that I can get an enlightened answer! &lt;br /&gt;&lt;br /&gt;Anyway, the supporting code I'm presenting here is just a few lines. All the heavylifting is done in Guice for dependencies resolution as well as in the Factories I've created to mock out the Server and create smart test data.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;i&gt;Last.tips.com&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;A Test Component is declaring lots of bindings but and in order to get the corresponding members injected, you either need to:&lt;ul&gt;&lt;li&gt;inject the component itself to your test&lt;/li&gt;&lt;li&gt;inherit from it&lt;/li&gt;&lt;/ul&gt;Let's take an example. You want to use the "standard" order built by the CustomerOrder &lt;i&gt;TestComponent&lt;/i&gt;. You could write: &lt;pre class="prettyprint"&gt;public class MyTestWithAnOrder extends TestComponent {&lt;br /&gt;  // injecting the CustomerOrder builds an order which itself is injected&lt;br /&gt;  // forgetting that line would end up with an "empty" order &lt;br /&gt;  // (order.equals(new Order()))&lt;br /&gt;  @Inject CustomerOrder customerOrder&lt;br /&gt;  @Inject Order Order&lt;br /&gt;  public GuiceModule module() { return new CustomerOrderModule(); }&lt;br /&gt;}&lt;/pre&gt;You can also write: &lt;pre class="prettyprint"&gt;public class MyTestWithAnOrder extends CustomerOrder {&lt;br /&gt;  // the order member can be inherited and properly setup by the superclass&lt;br /&gt;  public GuiceModule module() { return new CustomerOrderModule(); }&lt;br /&gt;}&lt;/pre&gt;But here's a trick. If you declare the following binding in the CustomerOrderModule:&lt;pre class="prettyprint"&gt;public class CustomerOrderModule extends GuiceModule {&lt;br /&gt;  @Override protected void configure() { &lt;br /&gt;    ...&lt;br /&gt;    bind(Order.class).toInstance(new Order());&lt;br /&gt;    bind(CustomerOrder.class).toInstance(new CustomerOrder);&lt;br /&gt;    ... &lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;// given that CustomerOrder looks like&lt;br /&gt;public class CustomerOrder extends TestComponent {&lt;br /&gt;  @Inject Customer customer;&lt;br /&gt;  @Inject Order order;&lt;br /&gt;  @Inject OrderFactory orderFactory;&lt;br /&gt;&lt;br /&gt;  // this is where, as a post-construct step, the order object gets fully setup  &lt;br /&gt;  @Inject &lt;br /&gt;  protected void localInit() {&lt;br /&gt;    orderFactory.saveAsStandardOrder(order);&lt;br /&gt;  }&lt;br /&gt;  public void GuiceModule module() { return new CustomerOrderModule(); }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;Then, you can just access the &lt;code class="prettyprint"&gt;order&lt;/code&gt; dependency in your test without having to reference the &lt;code class="prettyprint"&gt;CustomerOrder&lt;/code&gt; &lt;i&gt;TestComponent&lt;/i&gt;: &lt;pre class="prettyprint"&gt;public class MyTestWithAnOrder extends TestComponent {&lt;br /&gt;  // it just works (tm)&lt;br /&gt;  @Inject Order Order&lt;br /&gt;  public GuiceModule module() { return new CustomerOrderModule(); }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;The other tip I want to share is the addition on the Factories objects of &lt;code class="prettyprint"&gt;autoGet()&lt;/code&gt; methods. &lt;br /&gt;&lt;br /&gt;Let's say you need, for your test, to make as if a customer existed in the database. The usual thing to do is to use the test factory to create a new &lt;code class="prettyprint"&gt;Customer&lt;/code&gt; object and use mocks to have it returned when someone calls &lt;code class="prettyprint"&gt;getCustomer(id)&lt;/code&gt;. But you may actually not really care about which customer it really is. So why not go one step further with an &lt;code class="prettyprint"&gt;autoGet()&lt;/code&gt; method? &lt;br /&gt;&lt;br /&gt;Calling &lt;code class="prettyprint"&gt;autoGet()&lt;/code&gt; on the factory will just set-up the mock object representing the &lt;code class="prettyprint"&gt;CustomerInterface&lt;/code&gt; on the Server so that every time a customer is requested with an id, you simply create one on the fly and return it! (see &lt;a href="http://mockito.googlecode.com/svn/branches/1.5/javadoc/org/mockito/Mockito.html#doAnswer(org.mockito.stubbing.Answer)"&gt;here&lt;/a&gt; to see how to do it with Mockito).&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;i&gt;Conclusion&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;I think the take-away points from this post are:&lt;ul&gt;&lt;li&gt;Test components are worth investing in&lt;/li&gt;&lt;li&gt;But then they have to be highly reusable&lt;/li&gt;&lt;li&gt;Guice is a great tool to manage dependencies&lt;/li&gt;&lt;li&gt;Dependencies are better off with some operations to combine them&lt;/li&gt;&lt;/ul&gt;And the last, last, important thing about having TestComponents and dependencies carefully outlined is that it forces you to think about your design. If you find yourself having too many components and objects to inject for a given test, it may very well be sign of a potent &lt;a href="http://en.wikipedia.org/wiki/Code_smell"&gt;code smell&lt;/a&gt;.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5336273-2933112511054399689?l=etorreborre.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://etorreborre.blogspot.com/feeds/2933112511054399689/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5336273&amp;postID=2933112511054399689' title='7 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5336273/posts/default/2933112511054399689'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5336273/posts/default/2933112511054399689'/><link rel='alternate' type='text/html' href='http://etorreborre.blogspot.com/2010/03/we-need-algebra-for-guice-modules.html' title='We need an algebra for Guice modules'/><author><name>Eric</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://farm3.static.flickr.com/2203/2118752971_7becaf7476_t.jpg'/></author><thr:total>7</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5336273.post-4753888601887588835</id><published>2009-04-22T10:59:00.021+09:00</published><updated>2009-05-07T23:13:39.657+09:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='fit'/><category scheme='http://www.blogger.com/atom/ns#' term='scala'/><title type='text'>A FIT-like library for Scala</title><content type='html'>Ever since I started the &lt;a href="http://code.google.com/p/spec"&gt;specs&lt;/a&gt; project, I saw the possibility to provide a better &lt;a href="http://sourceforge.net/projects/fitlibrary"&gt;FIT&lt;/a&gt;-like library for Scala developers.&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Having been a &lt;a href="http://fitnesse.org/"&gt;Fitnesse&lt;/a&gt; user in the past, I was dissatisfied with the following:&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;having interpreted tables with no possibility for automated refactoring&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;being limited to tables and not being able to add simple expectations close to the specification text&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;being limited to tables while my domain data could be too complex to fit in a tabular format, hence supplementary work when trying to map business concepts to FIT tables&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;A tool for developers&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;I first started with the following assumption: most "business users", "subject matter experts" (SME) can't write and maintain FIT specifications. This point of view is &lt;a href="http://twitter.com/keithb_b/status/1272858561"&gt;not shared by everyone&lt;/a&gt;, but my experience shows me that the most workable FIT process is:&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;ol&gt;&lt;li&gt;A SME gives examples of what is expected, on the blackboard, on a paper, on an excel spreadsheet, whatever&lt;/li&gt;&lt;li&gt;A developer walks through the examples with the SME, asking questions, negociating features,... &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;This is the most important part: communicating clearly the specifications through examples&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;A developer writes and implements the examples with the FIT library&lt;/li&gt;&lt;li&gt;The developer comes back to the SME to clarify hidden assumptions and preconditions as writing things down precisely often shows under-specified parts&lt;/li&gt;&lt;li&gt;The developer completes the executable specification and &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;refactors it&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;The SME validates the final result which should be as readable as possible for her. Possibly, she adds some variations on the first examples, iterating on the whole process&lt;/li&gt;&lt;/ol&gt;&lt;div&gt;The main conclusion of the small process depicted above is that:&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="text-decoration: underline;"&gt;We can use developer tools&lt;/span&gt; because developers are the primary users for the FIT-like specification tools&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;Of course, this isn't the end of the story.  SME are indeed secondary users of those tools and we should take care of them in the future.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;But let's focus for now on our primary users. If we create a tool for developers:&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;An IDE can be used to edit, refactor and navigate the specifications (think Eclipse, IntelliJ)&lt;/li&gt;&lt;li&gt;A high-level language can be used to produce them (think Scala of course!)&lt;/li&gt;&lt;li&gt;The specifications can be stored in a Source configuration and control system (think Subversion, git,...)&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;Basic specifications&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;I first started with this vision: a specification should be some free text, explaining what the system is supposed to do. Among that text there are sentences or text fragments which represents examples of what the system should really be doing. I want to be able to "tag" those fragments and "link" them to a piece of code implementing the actions on the system and my expectations.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Free text mixed with executable code = ...? Scala XML literals of course!&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Having XML literals in Scala allows to write any kind of text and where necessary, insert some code in curly braces {} to be executed. So my first idea was to allow the developer to:&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;write some specification text in a Scala file, with minimal decorum to create a Specification class:&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;span class="Apple-style-span"   style="  ;font-family:arial;font-size:13px;"&gt;&lt;pre class="prettyprint"  style=" margin-left: 2em; padding-top: 0.5em; padding-right: 0.5em; padding-bottom: 0.5em; padding-left: 0.5em; border-left-width: 3px; border-left-style: solid; border-left-color: rgb(204, 204, 204); font-size:110%;"&gt;&lt;span class="kwd" style="color: rgb(0, 0, 136); "&gt;class&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="typ" style="color: rgb(102, 0, 102); "&gt;HelloWorldSpecification&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="kwd" style="color: rgb(0, 0, 136); "&gt;extends&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="typ" style="color: rgb(102, 0, 102); "&gt;HtmlSpecification&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;{&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="str" style="color: rgb(0, 136, 0); "&gt;"The greeting application"&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="kwd" style="color: rgb(0, 0, 136); "&gt;is&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;&lt;&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;textile&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;&gt;&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;&lt;br /&gt;&lt;br /&gt;h3&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;.&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="typ" style="color: rgb(102, 0, 102); "&gt;Presentation&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="typ" style="color: rgb(102, 0, 102); "&gt;This&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="kwd" style="color: rgb(0, 0, 136); "&gt;new&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; application should say &lt;/span&gt;&lt;span class="str" style="color: rgb(0, 136, 0); "&gt;"hello"&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="kwd" style="color: rgb(0, 0, 136); "&gt;in&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; different languages&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;.&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="str" style="color: rgb(0, 136, 0); "&gt;&amp;lt/&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;textile&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;&gt;&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/div&gt;&lt;ul&gt;&lt;li&gt;"tag" part of that text that I consider being an example which should be executed&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;span class="Apple-style-span"   style="  ;font-family:arial;font-size:13px;"&gt;&lt;pre class="prettyprint"  style=" margin-left: 2em; padding-top: 0.5em; padding-right: 0.5em; padding-bottom: 0.5em; padding-left: 0.5em; border-left-width: 3px; border-left-style: solid; border-left-color: rgb(204, 204, 204); font-size:110%;"&gt;&lt;span class="typ" style="color: rgb(102, 0, 102); "&gt;For&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; example&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;,&lt;&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;ex&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;&gt;&lt;/span&gt;&lt;span class="kwd" style="color: rgb(0, 0, 136); "&gt;by&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="kwd" style="color: rgb(0, 0, 136); "&gt;default&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;,&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; saying hello &lt;/span&gt;&lt;span class="kwd" style="color: rgb(0, 0, 136); "&gt;by&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="kwd" style="color: rgb(0, 0, 136); "&gt;default&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; should &lt;/span&gt;&lt;span class="kwd" style="color: rgb(0, 0, 136); "&gt;use&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="typ" style="color: rgb(102, 0, 102); "&gt;English&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;&lt;&lt;/span&gt;&lt;span class="str" style="color: rgb(0, 136, 0); "&gt;/ex&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/div&gt;&lt;ul&gt;&lt;li&gt;add a piece of code implementing the example and setting expectations&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;span class="Apple-style-span"   style="  ;font-family:arial;font-size:13px;"&gt;&lt;pre class="prettyprint"  style=" margin-left: 2em; padding-top: 0.5em; padding-right: 0.5em; padding-bottom: 0.5em; padding-left: 0.5em; border-left-width: 3px; border-left-style: solid; border-left-color: rgb(204, 204, 204); font-size:110%;"&gt;&lt;span class="typ" style="color: rgb(102, 0, 102); "&gt;For&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; example&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;,&lt;&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;ex&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;&gt;&lt;/span&gt;&lt;span class="kwd" style="color: rgb(0, 0, 136); "&gt;by&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="kwd" style="color: rgb(0, 0, 136); "&gt;default&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;,&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; saying hello &lt;/span&gt;&lt;span class="kwd" style="color: rgb(0, 0, 136); "&gt;by&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="kwd" style="color: rgb(0, 0, 136); "&gt;default&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; should &lt;/span&gt;&lt;span class="kwd" style="color: rgb(0, 0, 136); "&gt;use&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="typ" style="color: rgb(102, 0, 102); "&gt;English&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;&lt;&lt;/span&gt;&lt;span class="str" style="color: rgb(0, 136, 0); "&gt;/ex&gt;&lt;br /&gt;{ greet must_== "hello" }&lt;/span&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;I also liked the idea of using wiki markup to introduce simple formatting features to the Specification. Hence the use of &lt;textile&gt; tags showing that the Specification text should be interpreted as &lt;a href="http://en.wikipedia.org/wiki/Textile_(markup_language)"&gt;Textile&lt;/a&gt; marked-up text (the other available markup language is &lt;a href="http://en.wikipedia.org/wiki/Markdown"&gt;Markdown&lt;/a&gt;).&lt;/textile&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;The result of this specification can be seen &lt;a href="http://specs.googlecode.com/svn/samples/LiterateSpecifications/org.specs.samples.helloWorld.html"&gt;here&lt;/a&gt;. The first example executes without any issue and is highlighted as green text:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"   style="  line-height: 16px; font-family:Verdana;font-size:13px;"&gt;For example,&lt;ex onmouseout="hideToolTip();" onmouseover="showExampleDesc('12170552',event)" class="success" style="background-color: rgb(204, 255, 204); "&gt;by default, saying hello by default should use English&lt;/ex&gt;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;You can note that this constrast a lot with other Specification libraries approaches, like &lt;a href="http://cukes.info/"&gt;Cucumber&lt;/a&gt;, where:&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;Text is interpreted and mapped to methods. If I write "Given I have entered 50 in the calculator", there should be a method like &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;I_have_entered_x_in_the_calculator&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;The specification flow is imposed through the use of the "Given, when, then" format. My personal feeling is that this is detrimental to writing a good specification where more text, images, tables are needed to explain the expected behavior than just scenarii descriptions&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-style: italic; font-weight: bold;"&gt;Nesting tables all the way down,...&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;The first shots at my HtmlSpecifications were encouraging. Simple literate specifications can even be a good way to give examples of an API usage, as in the &lt;a href="http://specs.googlecode.com/svn/samples/LiterateSpecifications/org.specs.mock.mockitoSpec.html"&gt;Mockito specification for specs&lt;/a&gt;.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;However, I really was missing the table formats I used to work with in Fitnesse.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Then came the next "vision": tables are good, nested tables are better because they can look closer to the data structures we're juggling with all day long. So I should allow tables to be nested and composed of other tables. Hey, even having tabs would be nice, wouldn't it?!&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;The good news is that nothing else that pure Scala was needed for that. &lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Let me introduce: Properties, Fields and Forms&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;Properties&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;The basic building block of a &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;Form&lt;/span&gt; is a Property (of type &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;Prop&lt;/span&gt; in specs, &lt;span class="Apple-style-span"  style="font-family:arial;"&gt;&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;Property&lt;/span&gt; &lt;/span&gt;being reserved for something like &lt;a href="http://www.scala-lang.org/node/29"&gt;this&lt;/a&gt;).&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;A &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;Prop&lt;/span&gt; is something which has:&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;a label&lt;/li&gt;&lt;li&gt;an expected value&lt;/li&gt;&lt;li&gt;an actual value&lt;/li&gt;&lt;li&gt;a constraint&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;The most common type of property is declared like this in a &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;Form&lt;/span&gt;:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"   style="  ;font-family:arial;font-size:13px;"&gt;&lt;pre class="prettyprint"  style=" margin-left: 2em; padding-top: 0.5em; padding-right: 0.5em; padding-bottom: 0.5em; padding-left: 0.5em; border-left-width: 3px; border-left-style: solid; border-left-color: rgb(204, 204, 204); font-size:110%;"&gt;&lt;span class="com" style="color: rgb(136, 0, 0); "&gt;// "Name" is the property label, "Eric" is its actual value&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;&lt;br /&gt;val name &lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;=&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; prop&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;(&lt;/span&gt;&lt;span class="str" style="color: rgb(0, 136, 0); "&gt;"Name"&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;,&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="str" style="color: rgb(0, 136, 0); "&gt;"Eric"&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;)&lt;/span&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;and the expected value can be set with:&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"   style="  ;font-family:arial;font-size:13px;"&gt;&lt;pre class="prettyprint"  style=" margin-left: 2em; padding-top: 0.5em; padding-right: 0.5em; padding-bottom: 0.5em; padding-left: 0.5em; border-left-width: 3px; border-left-style: solid; border-left-color: rgb(204, 204, 204); font-size:110%;"&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;name&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;(&lt;/span&gt;&lt;span class="str" style="color: rgb(0, 136, 0); "&gt;"Bob"&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;)&lt;/span&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;Once executed the property will check for the equality of the actual and expected value, using a &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;beEqualTo&lt;/span&gt; matcher. Note that it is possible to change the matcher used to check the values:&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"   style="  ;font-family:arial;font-size:13px;"&gt;&lt;pre class="prettyprint"  style=" margin-left: 2em; padding-top: 0.5em; padding-right: 0.5em; padding-bottom: 0.5em; padding-left: 0.5em; border-left-width: 3px; border-left-style: solid; border-left-color: rgb(204, 204, 204); font-size:110%;"&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;val name &lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;=&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; prop&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;(&lt;/span&gt;&lt;span class="str" style="color: rgb(0, 136, 0); "&gt;"Name"&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;,&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="str" style="color: rgb(0, 136, 0); "&gt;"Eric"&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;).&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;matchesWith&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;(&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;equalToIgnoringCase&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;(&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;_&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;))&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;&lt;br /&gt;name&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;(&lt;/span&gt;&lt;span class="str" style="color: rgb(0, 136, 0); "&gt;"eric"&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;).&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;execute&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;.&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;isOk &lt;/span&gt;&lt;span class="com" style="color: rgb(136, 0, 0); "&gt;// true&lt;/span&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;Fields&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Actually there is an even simpler building block for Forms than a &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;Prop&lt;/span&gt;: a &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;Field&lt;/span&gt;. A &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;Field &lt;/span&gt;is simply a piece of input or output data with a label:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"   style="  ;font-family:arial;font-size:13px;"&gt;&lt;pre class="prettyprint"  style=" margin-left: 2em; padding-top: 0.5em; padding-right: 0.5em; padding-bottom: 0.5em; padding-left: 0.5em; border-left-width: 3px; border-left-style: solid; border-left-color: rgb(204, 204, 204); font-size:110%;"&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;val firstName &lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;=&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; field&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;(&lt;/span&gt;&lt;span class="str" style="color: rgb(0, 136, 0); "&gt;"First name"&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;,&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="str" style="color: rgb(0, 136, 0); "&gt;"Eric"&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;) // just a name and value&lt;/span&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"   style="  ;font-family:arial;font-size:13px;"&gt;&lt;pre class="prettyprint"  style=" margin-left: 2em; padding-top: 0.5em; padding-right: 0.5em; padding-bottom: 0.5em; padding-left: 0.5em; border-left-width: 3px; border-left-style: solid; border-left-color: rgb(204, 204, 204); font-size:110%;"&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;val tradeId &lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;=&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; field&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;(&lt;/span&gt;&lt;span class="str" style="color: rgb(0, 136, 0); "&gt;"Trade id"&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;,&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="pln" style=""&gt;&lt;span class="Apple-style-span" style="color: rgb(0, 136, 0);"&gt;trade.getId&lt;/span&gt;&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;) // retrieved from the database&lt;/span&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-style: italic; font-weight: bold; "&gt;Forms&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Armed with &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;Props&lt;/span&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt; &lt;/span&gt;and &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;Fields&lt;/span&gt;, building a &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;Form&lt;/span&gt; is plain easy:&lt;/div&gt;&lt;div&gt;&lt;ol&gt;&lt;li&gt;declare the props and fields of the form&lt;/li&gt;&lt;li&gt;declare how they should be layed out&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"   style="  ;font-family:arial;font-size:13px;"&gt;&lt;pre class="prettyprint"  style=" margin-left: 2em; padding-top: 0.5em; padding-right: 0.5em; padding-bottom: 0.5em; padding-left: 0.5em; border-left-width: 3px; border-left-style: solid; border-left-color: rgb(204, 204, 204); font-size:110%;"&gt;&lt;span class="kwd" style="color: rgb(0, 0, 136); "&gt;class&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="typ" style="color: rgb(102, 0, 102); "&gt;Person&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="kwd" style="color: rgb(0, 0, 136); "&gt;extends&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="typ" style="color: rgb(102, 0, 102); "&gt;Form&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;{&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;&lt;br /&gt;val firstName &lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;=&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; prop&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;(&lt;/span&gt;&lt;span class="str" style="color: rgb(0, 136, 0); "&gt;"First name"&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;,&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="str" style="color: rgb(0, 136, 0); "&gt;"Eric"&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;)&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;&lt;br /&gt;val lastName &lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;=&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; prop&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;(&lt;/span&gt;&lt;span class="str" style="color: rgb(0, 136, 0); "&gt;"Last name"&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;,&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="str" style="color: rgb(0, 136, 0); "&gt;"Torreborre"&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;)&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;&lt;br /&gt;tr&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;(&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;firstName&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;)&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;&lt;br /&gt;tr&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;(&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;lastName&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;)&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;In the &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;Person&lt;/span&gt; form above, we decided that the properties should be displayed on 2 different rows but they might as well be displayed on the same row:&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"   style="  ;font-family:arial;font-size:13px;"&gt;&lt;pre class="prettyprint"  style=" margin-left: 2em; padding-top: 0.5em; padding-right: 0.5em; padding-bottom: 0.5em; padding-left: 0.5em; border-left-width: 3px; border-left-style: solid; border-left-color: rgb(204, 204, 204); font-size:110%;"&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;   tr&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;(&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;firstName, lastName&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;)&lt;/span&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;And this can get arbitrarily more complex because you add also forms in a row:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"   style="  ;font-family:arial;font-size:13px;"&gt;&lt;pre class="prettyprint"  style=" margin-left: 2em; padding-top: 0.5em; padding-right: 0.5em; padding-bottom: 0.5em; padding-left: 0.5em; border-left-width: 3px; border-left-style: solid; border-left-color: rgb(204, 204, 204); font-size:110%;"&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;   tr&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;(&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;firstName, lastName, address&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;) // address is a Form representing the address&lt;/span&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Tabs are also very easy to use when there is more data to organize:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"   style="  ;font-family:arial;font-size:13px;"&gt;&lt;pre class="prettyprint"  style=" margin-left: 2em; padding-top: 0.5em; padding-right: 0.5em; padding-bottom: 0.5em; padding-left: 0.5em; border-left-width: 3px; border-left-style: solid; border-left-color: rgb(204, 204, 204); font-size:110%;"&gt;&lt;span class="kwd" style="color: rgb(0, 0, 136); "&gt;class&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="typ" style="color: rgb(102, 0, 102); "&gt;ClubMember&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="kwd" style="color: rgb(0, 0, 136); "&gt;extends&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="typ" style="color: rgb(102, 0, 102); "&gt;Form&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;{&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="kwd" style="color: rgb(0, 0, 136); "&gt;new&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; tabs&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;()&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;{&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;&lt;br /&gt; &lt;/span&gt;&lt;span class="kwd" style="color: rgb(0, 0, 136); "&gt;new&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; tab&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;(&lt;/span&gt;&lt;span class="str" style="color: rgb(0, 136, 0); "&gt;"Contact details"&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;)&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;{&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;&lt;br /&gt;   tr&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;(&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;field&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;(&lt;/span&gt;&lt;span class="str" style="color: rgb(0, 136, 0); "&gt;"First name"&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;,&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="str" style="color: rgb(0, 136, 0); "&gt;"Eric"&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;))&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;&lt;br /&gt;   tr&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;(&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;field&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;(&lt;/span&gt;&lt;span class="str" style="color: rgb(0, 136, 0); "&gt;"Last name"&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;,&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="str" style="color: rgb(0, 136, 0); "&gt;"Torreborre"&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;))&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;&lt;br /&gt; &lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;}&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;&lt;br /&gt; &lt;/span&gt;&lt;span class="kwd" style="color: rgb(0, 0, 136); "&gt;new&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; tab&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;(&lt;/span&gt;&lt;span class="str" style="color: rgb(0, 136, 0); "&gt;"Sports"&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;)&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;{&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;&lt;br /&gt;   th2&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;(&lt;/span&gt;&lt;span class="str" style="color: rgb(0, 136, 0); "&gt;"Sport"&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;,&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="str" style="color: rgb(0, 136, 0); "&gt;"Years of practice"&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;)&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;&lt;br /&gt;   tr&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;(&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;field&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;(&lt;/span&gt;&lt;span class="str" style="color: rgb(0, 136, 0); "&gt;"Squash"&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;,&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="lit" style="color: rgb(0, 102, 102); "&gt;10&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;))&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;&lt;br /&gt;   tr&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;(&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;field&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;(&lt;/span&gt;&lt;span class="str" style="color: rgb(0, 136, 0); "&gt;"Tennis"&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;,&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="lit" style="color: rgb(0, 102, 102); "&gt;5&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;))&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;&lt;br /&gt;   tr&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;(&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;field&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;(&lt;/span&gt;&lt;span class="str" style="color: rgb(0, 136, 0); "&gt;"Windsurf"&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;,&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="lit" style="color: rgb(0, 102, 102); "&gt;2&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;))&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;&lt;br /&gt; &lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;}&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;}&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;Special forms&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;As usual, as few patterns emerge when you start using something more frequently.  For example, it is pretty common to display data in straightforward tables with column headers. In that case, each cell should be a property with no label and the properties labels should be used to create the header row. The &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;TableForm&lt;/span&gt; serves exactly that purpose:&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"   style="  ;font-family:arial;font-size:13px;"&gt;&lt;pre class="prettyprint"  style=" margin-left: 2em; padding-top: 0.5em; padding-right: 0.5em; padding-bottom: 0.5em; padding-left: 0.5em; border-left-width: 3px; border-left-style: solid; border-left-color: rgb(204, 204, 204); font-size:110%;"&gt;&lt;span class="kwd" style="color: rgb(0, 0, 136); "&gt;class&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="typ" style="color: rgb(102, 0, 102); "&gt;PayLine&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;(&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;vDate&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;:&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="typ" style="color: rgb(102, 0, 102); "&gt;String&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;,&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; p&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;:&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="typ" style="color: rgb(102, 0, 102); "&gt;Double&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;,&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; r&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;:&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="typ" style="color: rgb(102, 0, 102); "&gt;Double&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;)&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="kwd" style="color: rgb(0, 0, 136); "&gt;extends&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="typ" style="color: rgb(102, 0, 102); "&gt;LineForm&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;{&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;&lt;br /&gt;val valueDate &lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;=&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; field&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;(&lt;/span&gt;&lt;span class="str" style="color: rgb(0, 136, 0); "&gt;"Value date"&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;,&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; vDate&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;)&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;&lt;br /&gt;val pay &lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;=&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; prop&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;(&lt;/span&gt;&lt;span class="str" style="color: rgb(0, 136, 0); "&gt;"NPV_PAY_NOTIONAL"&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;,&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; pricePay&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;(&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;valueDate&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;))(&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;p&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;)&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;&lt;br /&gt;val rec &lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;=&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; prop&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;(&lt;/span&gt;&lt;span class="str" style="color: rgb(0, 136, 0); "&gt;"NPV_REC_NOTIONAL"&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;,&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; priceRec&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;(&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;valueDate&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;))(&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;r&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;)&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;}&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="kwd" style="color: rgb(0, 0, 136); "&gt;new&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="typ" style="color: rgb(102, 0, 102); "&gt;TableForm&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;{&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;&lt;br /&gt;tr&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;(&lt;/span&gt;&lt;span class="typ" style="color: rgb(102, 0, 102); "&gt;PayLine&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;(&lt;/span&gt;&lt;span class="str" style="color: rgb(0, 136, 0); "&gt;"6/1/2007"&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;,&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;-&lt;/span&gt;&lt;span class="lit" style="color: rgb(0, 102, 102); "&gt;1732.34&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;,&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="lit" style="color: rgb(0, 102, 102); "&gt;0.0&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;))&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;&lt;br /&gt;tr&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;(&lt;/span&gt;&lt;span class="typ" style="color: rgb(102, 0, 102); "&gt;PayLine&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;(&lt;/span&gt;&lt;span class="str" style="color: rgb(0, 136, 0); "&gt;"4/30/2008"&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;,&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;-&lt;/span&gt;&lt;span class="lit" style="color: rgb(0, 102, 102); "&gt;580332.88&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;,&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="lit" style="color: rgb(0, 102, 102); "&gt;0.0&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;))&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;}.&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;report&lt;/span&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;In a &lt;span class="Apple-style-span"  style=" ;font-family:'courier new';"&gt;TableForm&lt;/span&gt;, each row is a &lt;span class="Apple-style-span"  style=" ;font-family:'courier new';"&gt;LineForm&lt;/span&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt; &lt;/span&gt;whose properties and fields will not have their labels displayed inside the table, but which will be used to build the table header. There is an even more cryptic (at least to the eyes of some,...) way of creating such a table using a &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;DataTable&lt;/span&gt;, the &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;DataTableForm&lt;/span&gt;:&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"   style="  ;font-family:arial;font-size:13px;"&gt;&lt;pre class="prettyprint"  style=" margin-left: 2em; padding-top: 0.5em; padding-right: 0.5em; padding-bottom: 0.5em; padding-left: 0.5em; border-left-width: 3px; border-left-style: solid; border-left-color: rgb(204, 204, 204); font-size:110%;"&gt;&lt;span class="kwd" style="color: rgb(0, 0, 136); "&gt;new&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="typ" style="color: rgb(102, 0, 102); "&gt;TradePrice&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;{&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="str" style="color: rgb(0, 136, 0); "&gt;"Value date"&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;    &lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;|&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="str" style="color: rgb(0, 136, 0); "&gt;"NPV_PAY_NOTIONAL"&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;    &lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;|&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="str" style="color: rgb(0, 136, 0); "&gt;"NPV_REC_NOTIONAL"&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;    &lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;|&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="str" style="color: rgb(0, 136, 0); "&gt;"6/1/2007"&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;      &lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;!&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;-&lt;/span&gt;&lt;span class="lit" style="color: rgb(0, 102, 102); "&gt;1732.34&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;              &lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;!&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="lit" style="color: rgb(0, 102, 102); "&gt;0.0&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;                   &lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;|&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="str" style="color: rgb(0, 136, 0); "&gt;"4/30/2008"&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;     &lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;!&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;-&lt;/span&gt;&lt;span class="lit" style="color: rgb(0, 102, 102); "&gt;580332.88&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;            &lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;!&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="lit" style="color: rgb(0, 102, 102); "&gt;0.0&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;                   &lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;|&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;{&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;(&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;vDate&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;:&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="typ" style="color: rgb(102, 0, 102); "&gt;String&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;,&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; pay&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;:&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="typ" style="color: rgb(102, 0, 102); "&gt;Double&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;,&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; rec&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;:&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="typ" style="color: rgb(102, 0, 102); "&gt;Double&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;)&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;=&gt;&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;&lt;br /&gt;tr&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;(&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;vDate&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;,&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; prop&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;(&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;pricePay&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;(&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;vDate&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;))(&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;pay&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;),&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; prop&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;(&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;priceRec&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;(&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;vDate&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;))(&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;rec&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;))&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;}&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;}.&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;report&lt;/span&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;In that example, the properties are created on the fly, without any label at all, because the table header will be directly taken from the &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;DataTable&lt;/span&gt; header.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;BagForm&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This is certainly the most advanced table featured at the moment. In many situations, each row of the table maps to an entity of the domain (let's say a "Customer"). &lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;The &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;BagForm&lt;/span&gt; allows to specify that a Seq of actual entities should match the expected one declared on each row:&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"   style="  ;font-family:arial;font-size:13px;"&gt;&lt;pre class="prettyprint"  style=" margin-left: 2em; padding-top: 0.5em; padding-right: 0.5em; padding-bottom: 0.5em; padding-left: 0.5em; border-left-width: 3px; border-left-style: solid; border-left-color: rgb(204, 204, 204); font-size:110%;"&gt;&lt;span class="kwd" style="color: rgb(0, 0, 136); "&gt;case&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="kwd" style="color: rgb(0, 0, 136); "&gt;class&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="typ" style="color: rgb(102, 0, 102); "&gt;CustomerLine&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;(&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;name&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;:&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="typ" style="color: rgb(102, 0, 102); "&gt;String&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;,&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; age&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;:&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="typ" style="color: rgb(102, 0, 102); "&gt;Int&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;)&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="kwd" style="color: rgb(0, 0, 136); "&gt;extends&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="typ" style="color: rgb(102, 0, 102); "&gt;EntityLineForm&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;[&lt;/span&gt;&lt;span class="typ" style="color: rgb(102, 0, 102); "&gt;Customer&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;]&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;{&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="com" style="color: rgb(136, 0, 0); "&gt;// the prop method accepts a function here, taking the proper attribute on the "Entity"&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;&lt;br /&gt;prop&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;(&lt;/span&gt;&lt;span class="str" style="color: rgb(0, 136, 0); "&gt;"Name"&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;,&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;(&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;_&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;:&lt;/span&gt;&lt;span class="typ" style="color: rgb(102, 0, 102); "&gt;Customer&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;).&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;getName&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;)(&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;name&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;)&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;&lt;br /&gt;prop&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;(&lt;/span&gt;&lt;span class="str" style="color: rgb(0, 136, 0); "&gt;"Age"&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;,&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;(&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;_&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;:&lt;/span&gt;&lt;span class="typ" style="color: rgb(102, 0, 102); "&gt;Customer&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;).&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;getAge&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;)(&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;age&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;)&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;}&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="kwd" style="color: rgb(0, 0, 136); "&gt;class&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="typ" style="color: rgb(102, 0, 102); "&gt;Customers&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;(&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;actualCustomers&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;:&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="typ" style="color: rgb(102, 0, 102); "&gt;Seq&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;[&lt;/span&gt;&lt;span class="typ" style="color: rgb(102, 0, 102); "&gt;Customer&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;])&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="kwd" style="color: rgb(0, 0, 136); "&gt;extends&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; Bag&lt;/span&gt;&lt;span class="typ" style="color: rgb(102, 0, 102); "&gt;Form&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;(&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;actualCustomers&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;)&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="com" style="color: rgb(136, 0, 0); "&gt;// usage example&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="kwd" style="color: rgb(0, 0, 136); "&gt;new&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="typ" style="color: rgb(102, 0, 102); "&gt;Customers&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;(&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;customersFromTheDatabase&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;)&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;{&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;&lt;br /&gt;tr&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;(&lt;/span&gt;&lt;span class="typ" style="color: rgb(102, 0, 102); "&gt;CustomerLine&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;(&lt;/span&gt;&lt;span class="str" style="color: rgb(0, 136, 0); "&gt;"Eric"&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;,&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="lit" style="color: rgb(0, 102, 102); "&gt;36&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;))&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;&lt;br /&gt;tr&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;(&lt;/span&gt;&lt;span class="typ" style="color: rgb(102, 0, 102); "&gt;CustomerLine&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;(&lt;/span&gt;&lt;span class="str" style="color: rgb(0, 136, 0); "&gt;"Bob"&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;,&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt; &lt;/span&gt;&lt;span class="lit" style="color: rgb(0, 102, 102); "&gt;27&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;))&lt;/span&gt;&lt;span class="pln" style="color: rgb(0, 0, 0); "&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="pun" style="color: rgb(102, 102, 0); "&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;Each Customer &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;EntityLineForm&lt;/span&gt; declares some properties whose actual values are "extracted" from an a&lt;img src="http://www.blogger.com/img/blank.gif" alt="Italique" border="0" class="gl_italic" /&gt;ctual entity.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;When the table is constructed (see "usage example"), each line is tested against the actual entities passed as parameter (&lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;customersFromTheDatabase&lt;/span&gt;), then only the best matches are kept to create the final table, based on the number of successful properties on each line. And if some expected rows are not matched by any actual entity, they are added at the end of the table as failures. A picture is worth 1000 words, the result is &lt;a href="http://specs.googlecode.com/svn/samples/LiterateSpecifications/org.specs.samples.bagFormSpec.html"&gt;here&lt;/a&gt;.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;Conclusion&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;The paint on &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;LiterateSpecification&lt;/span&gt; and &lt;span class="Apple-style-span"  style="font-family:'courier new';"&gt;Form&lt;/span&gt; is still fresh! I'm promoting it as "Alpha" for the next specs release (1.5.0), because I expect a few bugs to crop in here and there and lots of additional features to be necessary to support "industrial" specifications.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;In any case, I expect that the vision I had and the support of the Scala language will allow everyone to create beautiful specifications with lots of examples to discuss with Subject Matter Experts.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;a href="http://scala-tools.org/repo-releases/org/scala-tools/testing/specs/1.5.0/"&gt;Try it&lt;/a&gt;, send me all your feedback, I hope you'll like it, I sure do!&lt;/div&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5336273-4753888601887588835?l=etorreborre.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://etorreborre.blogspot.com/feeds/4753888601887588835/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5336273&amp;postID=4753888601887588835' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5336273/posts/default/4753888601887588835'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5336273/posts/default/4753888601887588835'/><link rel='alternate' type='text/html' href='http://etorreborre.blogspot.com/2009/04/fit-like-library-for-scala.html' title='A FIT-like library for Scala'/><author><name>Eric</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://farm3.static.flickr.com/2203/2118752971_7becaf7476_t.jpg'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5336273.post-8617472684411639694</id><published>2009-03-11T22:36:00.006+09:00</published><updated>2009-03-17T12:22:00.918+09:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='decision'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><title type='text'>How we decide</title><content type='html'>&lt;div&gt;&lt;span class="Apple-style-span"  style=" ;font-family:'Times New Roman';"&gt;&lt;div style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 3px; padding-right: 3px; padding-bottom: 3px; padding-left: 3px; width: auto; font: normal normal normal 100%/normal Georgia, serif; text-align: left; "&gt;For once, let's think about something else than programming. Or will we ;-)?&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-weight: bold; "&gt;&lt;span class="Apple-style-span" style="font-style: italic; "&gt;The bookshop&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Two weeks ago, going back home after some consulting, I arrived early at the airport with a few dollars in my pocket, and I decided to stop by the book shop to have a look at some refreshing mental food.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Haha, I wish that was so easy! I had a first round, looking at almost all the books, then I narrowed down my choice to a few books, maybe 2 or 3 and started thinking really hard.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;"Do I prefer a short and small book?"&lt;/div&gt;&lt;div&gt;"Should I take the most interesting?"&lt;/div&gt;&lt;div&gt;"Should it rather be entertaining? I should relax"&lt;/div&gt;&lt;div&gt;"Do I have enough cash? If not, is that worth withdrawing more money?"&lt;/div&gt;&lt;div&gt;"Do I rather to read a more political book?"&lt;/div&gt;&lt;div&gt;"Do I need a book at all?"&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;All this thinking (I'm not kidding, I spent at least 20 minutes) convinced me that this was the book I should buy: &lt;a href="http://www.amazon.com/How-We-Decide-Jonah-Lehrer/dp/0618620117/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1236778604&amp;amp;sr=8-1"&gt;How we decide&lt;/a&gt;.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-style: italic; font-weight: bold; "&gt;Feelings can be a good decision-maker&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This book was for me a very good follow-up on Damasio's book &lt;a href="http://www.amazon.com/Descartes-Error-Emotion-Reason-Human/dp/0380726475/ref=sr_1_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1237108937&amp;amp;sr=8-1"&gt;Descartes' error&lt;/a&gt; where we learn, through different stories, the role of emotions in the decision making process. Like the story of that guy having a brain injury and not feeling any more emotions. &lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;One morning, coming to Damasio's lab he explained everyone how he's been able to stay very calm in his car, avoiding a deadly car accident due to the iced road that morning. After the session, as he was about to leave, the doctor asked him to select the best date for the next appointment.  That emotionless guy stood there,  weighting for 30 minutes all the possibles options that were open on his schedule. The team just wanted to strangle him for not being able to make up his mind!&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This kind of story is one of many showing the power of emotions or, you could say, the power of the unconscious decision circuits. Most of other Jonas Lehrer examples are taken from games, whether decisions must be taken fast, like baseball or american football, or whether decisions are very complex, like chess or backgammon. &lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;In all those cases, Jonas Lehrer tells us that we have "dopamin learning circuits" which are directly connected to the reward system. Some neurons register when "it's good", when "it works", while others register the surrounding context and the consequences of our actions. The net result is that we progressively get a "feeling", an "intuition" of what works and what doesn't.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;For example, &lt;a href="http://en.wikipedia.org/wiki/Garry_Kasparov"&gt;Garry Kasparov&lt;/a&gt; has a global feeling of what is a good strategic position on the chessboard. &lt;a href="http://en.wikipedia.org/wiki/Joe_Montana"&gt;Joe Montana&lt;/a&gt; had an instinctive feeling of when was the best moment to make a pass on the field. We must also note that both of them have spent a lot of time thinking hard about how they failed, how they made mistakes.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 255); "&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;Is there anything we can use out of this for programming?&lt;/span&gt; &lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;We should fail early, fail often and try to learn as much as possible from our failures:&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;by reducing the develop-test cycle so that we have very frequent feedbacks of when changes are breaking stuff&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;by using retrospectives/post mortems to analyse what is right when working as a group of people on a project&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;by scrutinizing the code when it hurts. We have to change 100 lines of code all over the place for a seemingly innocent evolution. That hurts, doesn't it? Let's use this as a reinforcement for extreme refactoring. Next time we have to decide if we should refactor or not, the decision should &lt;span class="Apple-style-span" style="font-style: italic; "&gt;feel &lt;/span&gt;obvious&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;This is also a good justification for code &lt;span class="Apple-style-span" style="font-style: italic; "&gt;elegance&lt;/span&gt;. Code is elegant when it's easy to read, modify, reuse and getting that feeling helps making all the micro-decisions when writing code:&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;naming&lt;/li&gt;&lt;li&gt;formatting&lt;/li&gt;&lt;li&gt;refactoring&lt;/li&gt;&lt;li&gt;abstracting&lt;/li&gt;&lt;li&gt;checking, validating&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-style: italic; font-weight: bold; "&gt;Dont always trust your feelings, Luke&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Our "dopamin circuits" are trained to recognize success or failure patterns and give us a good feeling of what the next decision should be. Unless we totally fool ourselves. &lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;For example, we're easily &lt;a href="http://www.amazon.com/Fooled-Randomness-Hidden-Chance-Markets/dp/1587990717"&gt;fooled by randomness&lt;/a&gt;, thinking we understand how probabilities work. In 1913, in a casino at the Roulette wheel, the Black color came out 26 times in a row! Can you imagine the number of people who put all their money on the White after the tenth or fifteenth time,... Even if you're reminded that each draw is independent from the previous one, wouldn't you bet all your money too?&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;That's not the only situation where we should make better use of our reason. Behavioral science has now plenty of evidence showing how limited our rationality is:&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;we sometimes just don't learn from failing patterns. In some crisis situations, when emotions are high, we keep doing the wrong thing despite any adverse evidence. It's just impossible to think out of the box.&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;we're generally very bad at understanding probabilities.  For example, if there are 10 lottery tickets at 10$ and the reward is 200$, there's no reason not to play. But if we learn that we buy one ticket while another guy/gal buys the remaining 9 tickets we usually find the situation unfair and we don't play!&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;we're subject to "framing issues" and "loss aversion". Will you prefer your doctor to say that you have 20% chances to die or 80% chances to live?&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div&gt;The solution here is to use our frontal precortex and really try to take rational decisions knowing our natural biaises and weaknesses.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 255); "&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;Is there anything we can use out of this for programming?&lt;/span&gt; &lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Programming is a highly rational activity, but not always. Let's not forget to use our frontal precortex when:&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;we have "impossible" bugs. Especially under pressure, we're can sometimes be completely stucked on a bug just thinking: "this is impossible!!!! I'm doing A, I should have B!!!!". In this situation the best is to stop and brainstorm. Try to come out with as many ideas as possible. Then carefully select and experiment each of them. There's no magic in programming, things happen for a reason, so let's try to find it fast!&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;we're tuning for performance. Performance tuning should be as scientific as possible. If I have the feeling that X could be slowing down my system, I shouldn't tweak anything unless I have real evidence that X is the culprit&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;we're interacting with others and &lt;a href="http://www.codinghorror.com/blog/archives/000584.html"&gt;our ego gets in the way&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-style: italic; font-weight: bold; "&gt;Too much information&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Rationality= good. Too much rationality = bad. &lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Yes, this not obvious but for example, too much information is not the necessary ingredient for a good decision: &lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;some clients are tasting and comparing strawberry jams. They do a first ranking of their preferences. Now we ask them to think about &lt;span class="Apple-style-span" style="font-style: italic;"&gt;why&lt;/span&gt; they like them and the rankings are completely changed! I have the feeling that this is related to &lt;a href="http://en.wikipedia.org/wiki/Condorcet's_paradox"&gt;Condorcet's paradox&lt;/a&gt;.&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;numerous studies suggest that "experts" may not be good predictors of what's really going to happen in their field (well, also because they can be biaised by their own opinions)&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;before MRI, doctors were sending patients with back pains to rest for a few days. Now they can &lt;span class="Apple-style-span" style="font-style: italic;"&gt;see&lt;/span&gt; inside the body they suggest surgery and medecine. Even to well-being patients!&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 255); "&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;Is there anything we can use out of this for programming?&lt;/span&gt; &lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Focus!&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;when tuning performance, it is very easy to be completely overwhelmed by data. I would suggest taking one "red flag" only (like a display issue) and making a thourough analysis of that only issue until it is completely explained, ignoring what's not relevant&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;&lt;a href="http://www.amazon.com/Lean-Software-Development-Agile-Toolkit/dp/0321150783/ref=sr_1_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1237258151&amp;amp;sr=1-1"&gt;lean programming&lt;/a&gt; suggests that accumulating huge backlogs of features and issues may not be the best and most way to make decisions. One reason is that we're very sensitive to &lt;a href="http://en.wikipedia.org/wiki/Independence_of_irrelevant_alternatives"&gt;irrelevant alternatives.&lt;/a&gt;&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;Conclusion: Emotions vs Reason? &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;I really learned something with that book. When too many rational criteria come up to my mind on which book I should buy, I'm just losing my time and most probably I am going for a bad choice. I'd rather let my emotions drive me and learn instinctively what a good decision is for me (talk about an obsessional guy,...).&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;In conclusion, the debate is not such much "&lt;span class="Apple-style-span" style="font-style: italic;"&gt;emotions or reason&lt;/span&gt;" but more why and how we use both of them to make our decisions.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;ul&gt;&lt;/ul&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5336273-8617472684411639694?l=etorreborre.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://etorreborre.blogspot.com/feeds/8617472684411639694/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5336273&amp;postID=8617472684411639694' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5336273/posts/default/8617472684411639694'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5336273/posts/default/8617472684411639694'/><link rel='alternate' type='text/html' href='http://etorreborre.blogspot.com/2009/03/how-we-decide.html' title='How we decide'/><author><name>Eric</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://farm3.static.flickr.com/2203/2118752971_7becaf7476_t.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5336273.post-6959295036627134614</id><published>2009-02-13T23:28:00.016+09:00</published><updated>2011-08-30T07:44:03.107+09:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='arrows'/><category scheme='http://www.blogger.com/atom/ns#' term='scala'/><title type='text'>An exercise with arrows in Scala</title><content type='html'>I'm still a bit fascinated by the so-called "computing models" that you get when programming with &lt;a href="http://etorreborre.blogspot.com/2009/01/doing-my-homework.html"&gt;monads&lt;/a&gt; or &lt;a href="http://www.haskell.org/arrows/"&gt;arrows&lt;/a&gt;. &lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;That fascination comes from that fact that the things I'm using everyday can be abstracted to useful entities. Of course, it is pretty well-known to programmers that functions can be described formally with parameters, types, return values and so on. Given the definition of functions you can even compose them and create a useful function out of 2 other ones.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;But I was pretty amazed, when reading the paper from John Hughes on &lt;a href="http://www.cse.chalmers.se/~rjmh/Papers/arrows.pdf"&gt;Arrows&lt;/a&gt;, to see that there are lots of properties and combinators that you can define for your basic computing blocks. And that these combinators, defined at an abstract level, can then be used to structure the concrete and dirty work of your everyday functions.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;The Challenge&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;3 few weeks ago, I was intrigued by the small challenge brought up by Debasish Gosh, about translating one &lt;a href="http://debasishg.blogspot.com/2009/01/seeking-scala-equivalent-for-this.html"&gt;Haskell snippet to Scala&lt;/a&gt;. The function clusterBy, in Haskell, is defined by:&lt;/div&gt;&lt;br /&gt;&lt;code class="prettyprint"&gt;clusterBy :: Ord b =&gt; (a -&gt; b) -&gt; [a] -&gt; [[a]]&lt;br /&gt;clusterBy f = M.elems . M.map reverse . M.fromListWith (++) . map (f &amp;amp;&amp;amp;&amp;amp; return)&lt;/code&gt;&lt;br /&gt;Basically, it takes a list of elements and returns a list of lists, where the elements are grouped according to their result via a function. If 2 elements have the same result with f, then they end up in the same list.  For example:&lt;code class="prettyprint"&gt;clusterBy length ["ant", "part", "all"] =&gt; [["all", "ant"], ["part"]]&lt;/code&gt;That example looked interesting to me for 2 reasons:&lt;div&gt;&lt;ul&gt;&lt;li&gt;that's not the kind of thing that you program in one line with Java&lt;/li&gt;&lt;li&gt;it uses Arrows and I remembered that some code for &lt;a href="http://scala.sygneca.com/code/arrows"&gt;Arrows in Scala&lt;/a&gt; was available&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;So I set out to rewrite the clusterBy function in Scala, using Arrows. &lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;The solution&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;Here's my solution (partial here, read more at the end):&lt;/div&gt;&lt;br /&gt;&lt;code class="prettyprint"&gt;def clusterBy[A, B](l: List[A])(f: A =&gt; B) =&lt;br /&gt;  (listArr(f &amp;amp;&amp;amp;&amp;amp; identity) &gt;&gt;&gt; arr(groupByFirst) &gt;&gt;&gt; arr(second) &gt;&gt;&gt; arr(reverse))(l)&lt;/code&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;And the clusterBy function in action as a &lt;a href="http://code.google.com/p/specs"&gt;specs&lt;/a&gt; expectation:&lt;/div&gt;&lt;br /&gt;&lt;code class="prettyprint"&gt;clusterBy(List("ant", "part", "all"))(_.length) must beEqualTo(List(List("all", "ant"), List("part")))&lt;/code&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Let's dissect the clusterBy function to understand what exactly Arrows are bringing to the table. &lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-style: italic; font-weight: bold;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-weight: bold; "&gt;&lt;span class="Apple-style-span" style="font-style: italic; "&gt;"Lifting" a function to an Arrow (what the h...?)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;First of all, for our understanding, we can remove some noise:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;arr(groupByFirst) is just taking a function, groupByFirst, and "lifting" it to make it an Arrow object which can play nicely with other arrows using the &gt;&gt;&gt; operator. &lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;There &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;are&lt;/span&gt; implicit definitions in the Arrows object transforming functions to Arrows but unfortunately type inference didn't seem to authorize me the removal of arr(...). If I could do it, I would get something like:&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;code class="prettyprint"&gt;def clusterBy[A, B](l: List[A])(f: A =&gt; B) =&lt;br /&gt; (listArr(f &amp;amp;&amp;amp;&amp;amp; identity) &gt;&gt;&gt; groupByFirst &gt;&gt;&gt; second &gt;&gt;&gt; reverse)(l)&lt;br /&gt;&lt;/code&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-weight: bold; "&gt;&lt;span class="Apple-style-span" style="font-style: italic; "&gt;&gt;&gt;&gt;, the "sequence" operator&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;How should we read that expression now? &lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;The &gt;&gt;&gt; operator reads very easily. It says:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;f1 &gt;&gt;&gt; f2 =&gt; take the result of f1 and give it to f2.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;It is the equivalent of the composition operator, except that it reads the other way around, from left to right. With composition we would have "f2 . f1". The &gt;&gt;&gt; operator (arguably) follows the natural reading flow (well, in English at least,...).&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;&amp;amp;&amp;amp;&amp;amp;, the "branching" operator&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;The next important operator is &lt;code class="prettyprint"&gt;&amp;&amp;&amp;&lt;/code&gt;:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;code class="prettyprint"&gt;f1 &amp;&amp;&amp; f2 &lt;/code&gt;=&gt; A function with takes an input and returns a pair with the results of both f1 and f2.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;So &lt;code class="prettyprint"&gt;(f &amp;&amp;&amp; identity)&lt;/code&gt; in our example simply takes an element and creates a pair containing the result of the application of f and the element itself:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;code class="prettyprint"&gt;(f(x), x)&lt;/code&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;But what we want to do is to apply this function (an Arrow more precisely) to the list of elements  which is our input. That's exactly what listArr does! It creates a Arrow taking a list (read "enhanced function") from a function taking a single element.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;Reading the whole expression in detail&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;With all that knowledge, let's read again the definition now with our example as an illustration. &lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;The input data is:&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;code class="prettyprint"&gt;List("ant", "part", "all")&lt;/code&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 255);"&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;"take the list of elements and return (f(x), x) for each element":&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;code class="prettyprint"&gt;listArr(f &amp;amp;&amp;amp;&amp;amp; identity)&lt;br /&gt;&lt;/code&gt;&lt;/div&gt;&lt;code class="prettyprint"&gt;&lt;/code&gt;&lt;div&gt;&lt;code class="prettyprint"&gt;=&gt; List((3, "ant"), (4, "part"), (3, "all"))&lt;/code&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 255);"&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;"then group by the first element"&lt;/span&gt;&lt;/span&gt;, i.e. create a list of pairs where the first element is f(x) and the second element is a list of all y where f(x) == f(y))&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;code class="prettyprint"&gt;&gt;&gt;&gt; groupByFirst&lt;/code&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;=&gt; &lt;code class="prettyprint"&gt;List((4, List("part"), (3, List("ant", "all")))&lt;/code&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 255);"&gt;"then take the second element"&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;code class="prettyprint"&gt;&gt;&gt;&gt; second&lt;/code&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;code class="prettyprint"&gt;=&gt; List(List("part"), List("ant", "all"))&lt;/code&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;Note that the "second" function defined here just takes the second element of a pair. This means that the &gt;&gt;&gt; operator is smart enough to know that we're operating on lists of pairs and not on a single pair!&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;&lt;span class="Apple-style-span" style="color: rgb(51, 51, 255);"&gt;"then reverse the list"&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;code class="prettyprint"&gt;&gt;&gt;&gt; reverse&lt;br /&gt;=&gt; List(List("ant", "all"), List("part"))&lt;/code&gt;&lt;/div&gt;&lt;div&gt;&lt;code class="prettyprint"&gt;&lt;br /&gt;&lt;/code&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-style: italic; font-weight: bold; "&gt;The hidden stuff under the carpet&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;To be able to create this nice one-liner in Scala, I had to create special-purpose functions: groupByFirst, reverse, second.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;First of all, you can argue that the real meat of the clusterBy function is actually the groupByFirst function:&lt;/div&gt;&lt;div&gt;&lt;code class="prettyprint"&gt;def groupByFirst[A, B] = new Function1[List[(A, B)], List[(A, List[B])]] {&lt;br /&gt;  def apply(l: List[(A, B)]) =&lt;br /&gt;    l.foldLeft(new HashMap[A, List[B]]: Map[A, List[B]]) { (res: Map[A, List[B]], cur: Pair[A, B]) =&gt;&lt;/code&gt;&lt;/div&gt;&lt;div&gt;&lt;code class="prettyprint"&gt;      val (key, value) = cur&lt;/code&gt;&lt;/div&gt;&lt;div&gt;&lt;code class="prettyprint"&gt;      res.update(key, value :: res.getOrElse(key, Nil))}.toList&lt;br /&gt;}&lt;/code&gt;&lt;/div&gt;&lt;br /&gt;And actually if you look at Debasish's blog, there are full Scala solutions that fill the same space as the groupByFirst function. On the other hand, I find that the Arrows notation shows pretty well the process flow between "elementary" functions.&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Then you can wonder why I couldn't basically "detach" the reverse operation from the List class, like this maybe?&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;code class="prettyprint"&gt;def reverseList[T](l: List[T]) = l.reverse&lt;/code&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"   style=" ;font-family:-webkit-monospace;font-size:13px;"&gt;def reverse[T] = reserveList _ &lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"   style=" ;font-family:-webkit-monospace;font-size:13px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"   style=" ;font-family:-webkit-monospace;font-size:13px;"&gt;&lt;span class="Apple-style-span"   style="  ;font-family:Georgia;font-size:16px;"&gt;However, this just doesn't work because the type inference seems to be not constrained enough when it comes to sequence the functions with the &gt;&gt;&gt;  operator. The only way I found to have this working was to define a Function1 object:&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;code class="prettyprint"&gt;def reverse[T] = new Function1[List[T], List[T]] {&lt;br /&gt;  def apply(l: List[T]) = l.reverse&lt;br /&gt;}&lt;/code&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;This last issue is maybe the biggest drawback when using Arrows with Scala. Now I can propose my own challenge to the Scala community. Can you find a better way ;-) ?...&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5336273-6959295036627134614?l=etorreborre.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://etorreborre.blogspot.com/feeds/6959295036627134614/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5336273&amp;postID=6959295036627134614' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5336273/posts/default/6959295036627134614'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5336273/posts/default/6959295036627134614'/><link rel='alternate' type='text/html' href='http://etorreborre.blogspot.com/2009/02/exercise-with-arrows-in-scala.html' title='An exercise with arrows in Scala'/><author><name>Eric</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://farm3.static.flickr.com/2203/2118752971_7becaf7476_t.jpg'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5336273.post-2650246717230365328</id><published>2009-01-05T12:46:00.008+09:00</published><updated>2009-01-07T09:23:38.117+09:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='monads'/><category scheme='http://www.blogger.com/atom/ns#' term='scalaz'/><category scheme='http://www.blogger.com/atom/ns#' term='scala'/><title type='text'>Doing my homework</title><content type='html'>&lt;div&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;The mapFilter function&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;I recently tried to write a function using Scala which would apply a function to a list, then filter all the resulting elements according to a criteria. In my infinite wisdom I named it "mapFilter". Here is an example of what I expected:&lt;br /&gt;&lt;br /&gt;&lt;code class="prettyprint"&gt;mapFilter(List(1, 2), (x:Int) =&gt; if (x%2 == 0) Some(x*2) else None)&lt;br /&gt;// returns List(4)&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Of course I wanted this function to be as efficient as possible so it had to do the job in one pass. Otherwise, I could just use a combination of "map" and "filter" to do the trick:&lt;br /&gt;&lt;code class="prettyprint"&gt;&lt;br /&gt;&lt;/code&gt;&lt;div&gt;&lt;code class="prettyprint"&gt;def mapFilter[T, U](l: List[T], f: T =&gt;Option[U]): List[U] = {&lt;/code&gt;&lt;/div&gt;&lt;div&gt;&lt;code class="prettyprint"&gt;  l.map(f(_)).filter(_.isDefined).map(_.get)&lt;/code&gt;&lt;/div&gt;&lt;div&gt;&lt;code class="prettyprint"&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;3 passes on the list, we can definitely do better than this!&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;Using flatMap&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;Somehow I remembered that the Option class is a Monad, and my subconscious related the bind operation of monads to the flatMap operation on Iterable. I should be able to do something with it,... &lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Then I experimented this:&lt;br /&gt;&lt;br /&gt;&lt;code class="prettyprint"&gt;List(1, 2) flatMap { (x:Int) =&gt; if (x%2 == 0) Some(x*2) else None }&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Bingo, it works!&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Now, if you read the signature of the flatMap method (on the Iterable trait), understanding how this works is not so clear:&lt;br /&gt;&lt;br /&gt;&lt;code class="prettyprint"&gt;def flatMap[B](f: A =&gt; Iterable[B]): Iterable[B]&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Option is not an Iterable, this shouldn't compile! Yes, it does, because any Option can be implicitly converted to an Iterable thanks an implicit conversion:&lt;br /&gt;&lt;code class="prettyprint"&gt;&lt;br /&gt;object Option {&lt;br /&gt;  /** An implicit conversion that converts an option to an iterable value */&lt;br /&gt;  implicit def option2Iterable[A](xo: Option[A]): Iterable[A] = xo.toList&lt;/code&gt;&lt;/div&gt;&lt;div&gt;&lt;code class="prettyprint"&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Sometimes I wish there was a way to display all the available implicit definitions for a given scope,...&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;Monads again,...&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;What about the "Monadic" stuff then? Tony Morris, on the Scala mailing-list, left me a message hinting at a purer solution:&lt;br /&gt;&lt;blockquote&gt;Hi Eric,&lt;br /&gt;You can write mapFilter using Scalaz which has a MonadEmptyPlus  data&lt;br /&gt;structure. I suggest you do not use the available subversion of the&lt;br /&gt;List.flatMap type signature from which to learn&lt;/blockquote&gt;So I thought that I should do my homework, have a look at it and understand why MonadEmptyPlus was a good way to write the mapFilter function.&lt;br /&gt;&lt;br /&gt;You can find all the code from the scalaz project &lt;a href="http://code.google.com/p/scalaz/"&gt;here&lt;/a&gt;.  The package I'm exploring today is the "control" package. &lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;A bit of Scalaz exploration&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;Let's jump right to the MonadEmptyPlus:&lt;br /&gt;&lt;code class="prettyprint"&gt;&lt;br /&gt;trait MonadEmptyPlus[M[_]] extends MonadPlus[M] with MonadEmpty[M]&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Ohhh. We are high on abstraction here, because MonadPlus is:&lt;br /&gt;&lt;br /&gt;&lt;code class="prettyprint"&gt;trait MonadPlus[M[_]] extends Monad[M] with Plus[M]&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;And MonadEmpty is, as you can guess:&lt;br /&gt;&lt;code class="prettyprint"&gt;&lt;br /&gt;&lt;/code&gt;&lt;/div&gt;&lt;div&gt;&lt;code class="prettyprint"&gt;trait MonadEmpty[M[_]] extends Monad[M] with Empty[M]&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Last but not least, Monad is:&lt;br /&gt;&lt;code class="prettyprint"&gt;&lt;br /&gt;&lt;/code&gt;&lt;/div&gt;&lt;div&gt;&lt;code class="prettyprint"&gt;trait Monad[M[_]] extends Pure[M] with Bind[M] with Functor[M] with Apply[M]&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Honestly, I didn't suspect that my homework would take me so long! Yet, since every concept and notion of what is a Monad seems to be carefully crafted in Scalaz, this is a good idea to spend a bit of time trying to understand each of them.&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;Pure&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The Pure trait is defined by:&lt;br /&gt;&lt;code style="prettyprint"&gt;&lt;br /&gt;/**&lt;br /&gt; * Project the given value into the environment that is abstracted over. &lt;br /&gt; */&lt;br /&gt;def pure[A](a: A): P[A]&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;It actually says that you can take any value of some type A, and using the "pure" function, put it in a "Container" of type P, called the "environment" here. I guess that the idea is to say that once the value is well protected and controlled in its container, it is "pure". That being said, this is still very abstract. Fortunately we have some implicit values in the the Pure object giving us some examples of what it means to be "pure":&lt;br /&gt;&lt;br /&gt;&lt;code class="prettyprint"&gt; implicit val OptionPure = new Pure[Option] {&lt;br /&gt;  def pure[A](a: A) = Some(a)&lt;br /&gt;}&lt;br /&gt;implicit val ListPure = new Pure[List] {&lt;br /&gt;  def pure[A](a: A) = List(a)&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;A Pure[Option] of the value a is simply the value "a" inside the "Some" container. So if you think of Monads as "Containers" for computation then "pure" is what creates the container with a value inside.&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;Bind&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Bind is the evil twin of Pure and, as far as I'm concerned, the essence of monads because it is the basis of computing with Monads:&lt;br /&gt;&lt;code class="prettyprint"&gt;&lt;br /&gt;/**&lt;br /&gt; * Binds a function through an environment (known also as sequencing).&lt;br /&gt; */&lt;br /&gt;trait Bind[BD[_]] {&lt;br /&gt;  /**&lt;br /&gt;   * Binds the given value with the given value through the environment.&lt;br /&gt;   */&lt;br /&gt;  def bind[A, B](f: A =&gt; BD[B], ba: BD[A]): BD[B]&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Every word is important here. You have an "environment" again. This "environment" or "Container" contains values which have been put there using the pure function for example. Now, what you want to do is to apply a function to the value inside the container, without the value leaving it. So you "bind" a function to the environment. Again, concrete examples, given by the implicit values in the Bind object will help us understand what's going on:&lt;br /&gt;&lt;br /&gt;&lt;code class="prettyprint" a="" bind="" for=""&gt;implicit val OptionBind = new Bind[Option] {&lt;br /&gt;  def bind[A, B](f: A =&gt; Option[B], a: Option[A]) = a flatMap f&lt;br /&gt;}&lt;br /&gt;implicit val ListBind = new Bind[List] {&lt;br /&gt;  def bind[A, B](f: A =&gt; List[B], a: List[A]) = a flatMap f&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;When I bind a function to an Option, like Some(4), it is as if I'm doing:&lt;br /&gt;&lt;code class="prettyprint"&gt;&lt;br /&gt;val f = x =&gt; Some(x + 2)&lt;/code&gt;&lt;div&gt;&lt;code class="prettyprint"&gt;bind(f, Some(4))&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;1. apply the function to the inner element&lt;br /&gt;&lt;br /&gt;Some(f(4)) === Some(Some(6))&lt;br /&gt;&lt;br /&gt;2. "flatten" the inner result by removing the inner "box"&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Some(6)&lt;br /&gt;&lt;br /&gt;This brings 2 remarks here:&lt;br /&gt;&lt;br /&gt;1. "bind" for a monad is indeed the "flatMap" function of an Iterable in Scala. It maps the value then flatten the results. The big difference is in the function signature. While flatMap is a method on Iterable and accepts a function returning another Iterable, the bind function accepts a "Container" type of any sort and returns the same container type; Binding to an Option will return an Option, binding to a List will return a List ("I suggest you do not use the available subversion of the List.flatMap type signature from which to learn"...)&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;Functor&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;2. why is it necessary to have a "bind" operation? If I want to get Some(6) as a result, something like "map" should be enough. True. And this even has a name, this is the "fmap" operation of the Functor trait (also mixed in by Monad):&lt;br /&gt;&lt;code class="prettyprint"&gt;&lt;br /&gt;trait Functor[F[_]] {&lt;br /&gt; /**&lt;br /&gt;  * Maps the given function across the given environment.&lt;br /&gt;  */&lt;br /&gt;  def fmap[A, B](f: A =&gt; B, fa: F[A]): F[B]&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;The "fmap" operation just means transforming the value(s) inside the Container to something else. But the bind operation offers an additional advantage. It allows to compose functions returning values in the Container. I can't compose directly 2 functions returning an Option:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;code class="prettyprint"&gt;val f = (x:Int) =&gt; if (x % 2 == 0) Some(x) else None&lt;br /&gt;val g = (x:Int) =&gt; if (x % 3 == 0) Some(x) else None&lt;br /&gt;&lt;br /&gt;// this doesn't work since g returns an Option and f expects an Int&lt;br /&gt;val composed = f . g // ??&lt;/code&gt;&lt;/div&gt;&lt;code class="prettyprint"&gt;&lt;/code&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;But I can compose them using "bind":&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;code class="prettyprint"&gt;// this works as expected&lt;br /&gt;val composed: (Int =&gt; Option[Int]) = (x:Int) =&gt; g(x).bind(f)&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-style: italic; font-weight: bold; "&gt;Apply&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;The definition of Apply is this one:&lt;br /&gt;&lt;code class="prettyprint"&gt;&lt;br /&gt;trait Apply[AP[_]] {&lt;br /&gt;  def apply[A, B](f: AP[A =&gt; B], fa: AP[A]): AP[B]&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;And a good example is provided for Lists:&lt;br /&gt;&lt;code class="prettyprint"&gt;&lt;br /&gt;implicit val ListApply = new Apply[List] {&lt;br /&gt;  def apply[A, B](f: List[A =&gt; B], a: List[A]) = f flatMap (f =&gt; a map(f(_)))&lt;/code&gt;&lt;/div&gt;&lt;div&gt;&lt;code class="prettyprint"&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;We take a list of functions, a list of values, and we return a list with the results of all the functions applied to all the values. Actually this is the essence of the "Applicative" model of programming, so I don't really understand why it's been added to the Monad trait. I guess it is because monad computations imply applicative computations as described &lt;a href="http://lucdup.blogspot.com/2008/01/tmp_2968.html"&gt;here&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;Empty and Plus&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Now that the tour of Monad is over, we can look at the Empty and Plus traits.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Empty is very easy, it just returns an empty "Container" (named "Environment" here):&lt;br /&gt;&lt;code class="prettyprint"&gt;&lt;br /&gt;&lt;/code&gt;&lt;/div&gt;&lt;code class="prettyprint"&gt;&lt;/code&gt;&lt;div&gt;&lt;code class="prettyprint"&gt;&lt;div&gt;trait Empty[E[_]] {&lt;/div&gt;&lt;div&gt;  /**&lt;/div&gt;&lt;div&gt;   * Returns the empty environment.&lt;/div&gt;&lt;div&gt;   */&lt;/div&gt;&lt;div&gt;  def empty[A]: E[A]&lt;/div&gt;&lt;div&gt;}&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;implicit val OptionEmpty = new Empty[Option] {&lt;/div&gt;&lt;div&gt;  def empty[A] = None&lt;/div&gt;&lt;div&gt;}&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;implicit val ListEmpty = new Empty[List] {&lt;/div&gt;&lt;div&gt;  def empty[A] = Nil&lt;/div&gt;&lt;div&gt;}&lt;/div&gt;&lt;br /&gt;&lt;/code&gt;Plus defines what it means to "append" or "merge" two environments together. Its result is very specific to the underlying Container type as you can see with the implicit values:&lt;br /&gt;&lt;code class="prettyprint"&gt;&lt;br /&gt;trait Plus[P[_]] {&lt;br /&gt;  /**&lt;br /&gt;   * Appends the two given values.&lt;br /&gt;   */&lt;br /&gt;  def plus[A](a1: =&gt; P[A], a2: =&gt; P[A]): P[A]&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;implicit val OptionPlus = new Plus[Option] {&lt;br /&gt;  def plus[A](a1: =&gt; Option[A], a2: =&gt; Option[A]) = a1 orElse a2&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;implicit val ListPlus = new Plus[List] {&lt;br /&gt;  def plus[A](a1: =&gt; List[A], a2: =&gt; List[A]) = a1 ::: a2&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Empty and Plus are indeed defining some kind of addition operation on Monads, aren't they?&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-weight: bold; "&gt;&lt;span class="Apple-style-span" style="font-style: italic; "&gt;The real academic mapFilter function ;-)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-style: italic; font-weight: bold;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;With all that at hand I should now be able to create my mapFilter function ;-)&lt;/div&gt;&lt;div&gt;&lt;code class="prettyprint"&gt;&lt;br /&gt;import scalaz.control._&lt;br /&gt;def mapFilter[T, A[_], U](f: T =&gt; A[U], m: A[T])(implicit m1: MonadEmptyPlus[A])= {&lt;/code&gt;&lt;/div&gt;&lt;div&gt;&lt;code class="prettyprint"&gt;  m1.bind(f, m)&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Is that all? Yes, let's try it:&lt;br /&gt;&lt;code class="prettyprint"&gt;&lt;br /&gt;// Note that Scala type inferencer doesn't infer the types here&lt;br /&gt;mapFilter[Int, List, Int]((x:Int) =&gt; if (x%2 == 0) List(x*2) else Nil,&lt;br /&gt;                          List(1, 2))&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;However we may wonder: is that really necessary to have a MonadEmptyPlus? No, a simple Monad is also ok:&lt;br /&gt;&lt;code class="prettyprint"&gt;&lt;br /&gt;def mapFilter[T, A[_], U](f: T =&gt; A[U], m: A[T])(implicit m1: Monad[A]) = { &lt;/code&gt;&lt;/div&gt;&lt;div&gt;&lt;code class="prettyprint"&gt;  m1.bind(f, m) &lt;/code&gt;&lt;/div&gt;&lt;div&gt;&lt;code class="prettyprint"&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;And that's understable with the List example because we're using "flatMap" under the covers! But this is not totally satisfying. Somehow, I would like to enforce that if f returns the empty element for a given MonadEmptyPlus, then that element is filtered from the result. &lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;Even better with FoldRight&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;We can actually define this using another abstraction from the control package: FoldRight.&lt;/div&gt;&lt;code class="prettyprint"&gt;&lt;br /&gt;trait FoldRight[F[_]] {&lt;br /&gt;  /**&lt;br /&gt;   * Fold the given function across the given environment.&lt;br /&gt;   */&lt;br /&gt;  def foldRight[A, B](t: F[A], b: B, f: (A, =&gt; B) =&gt; B): B&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;FoldRight works exactly as the foldRight method on Iterable in Scala and allows us to use the Empty and Plus definition from MonadEmptyPlus, accumulating the mapped values, unless they're empty:&lt;br /&gt;&lt;code class="prettyprint"&gt;&lt;br /&gt;def mapFilter[T, A[_], U](m: A[T], f: T =&gt; A[U])&lt;br /&gt;                         (implicit m1: MonadEmptyPlus[A], f1: FoldRight[A]) = {&lt;br /&gt;  f1.foldRight[T, A[U]](m, m1.empty, (t, result) =&gt; m1.plus(f(t), result))&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;And that's the end of the assignement for today!&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;Conclusion&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This exploration of Scalaz was very interesting for me because:&lt;/div&gt;&lt;div&gt;&lt;ol&gt;&lt;li&gt;The abstractions are nicely separated&lt;/li&gt;&lt;li&gt;Implicit values provide lots of concrete examples&lt;/li&gt;&lt;li&gt;The programming style is very close to the use of type classes in Haskell (which wraps my mind in a new way). The drawback is less type inference by Scala&lt;/li&gt;&lt;li&gt;I had to think again about that "bind" function and why it was so special&lt;/li&gt;&lt;li&gt;It gives me the desire to understand more deeply what are the differences between the so-called "models of programming": functor, monads, applicative, arrows,...&lt;/li&gt;&lt;li&gt;I haven't been blogging for a looooonnnnng time.&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5336273-2650246717230365328?l=etorreborre.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://etorreborre.blogspot.com/feeds/2650246717230365328/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5336273&amp;postID=2650246717230365328' title='7 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5336273/posts/default/2650246717230365328'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5336273/posts/default/2650246717230365328'/><link rel='alternate' type='text/html' href='http://etorreborre.blogspot.com/2009/01/doing-my-homework.html' title='Doing my homework'/><author><name>Eric</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://farm3.static.flickr.com/2203/2118752971_7becaf7476_t.jpg'/></author><thr:total>7</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5336273.post-8565375419195743614</id><published>2008-10-15T04:55:00.014+09:00</published><updated>2008-10-21T17:14:35.427+09:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='commando'/><title type='text'>Commando programming</title><content type='html'>&lt;blockquote&gt;When you program for a trader you don't have time to write tests&lt;br /&gt;&lt;/blockquote&gt;I've been wondering for some time if the quote above, or a variant like: "you don't have time to refactor your code", was really true or not. I generally bug my colleagues when they don't write tests along with their code and I usually prophetize that they will actually &lt;span style="font-weight: bold;"&gt;spare&lt;/span&gt; time by writing tests instead of losing time.&lt;br /&gt;&lt;br /&gt;Unfortunately I don't have strong evidence if this is &lt;a href="http://scruffylookingcatherder.com/archive/2008/01/22/tdd-proven-effective-or-is-it.aspx"&gt;true or not&lt;/a&gt;. The only thing I can say is that almost every time I've tried to take shortcuts in my developments, I've been bitten by very silly bugs and regretted my recklessness!&lt;br /&gt;&lt;br /&gt;Anyway, what would you do if you had to code at the speed of light for a drug-addict trader (and they really need drugs those days,...)? What kind of practices would you adopt to go faster? How would you prepare yourself for those situations?&lt;br /&gt;&lt;br /&gt;With those questions in mind, I was happy to be recently involved in a one week project where the biggest  part of my job was to do some "Commando programming" to create the necessary tools to support the project.&lt;br /&gt;&lt;br /&gt;In this post, I'll do my own retrospective of that project and try to highlight the points which I think are decisive in the context of "Commando programming". Actually most of the "Ding!" points  (does that ring a bell to you?)  below are applicable to any programming situation. The only difference is that there's no time for preparation in a "Commando" situation. You have to be seriously fit for the job.&lt;br /&gt;&lt;br /&gt;&lt;update&gt; This post is too long, that's the Steve Yegge syndrom,... So here's a summary of the take-away points, so you can get a first impression if it's worth reading or not:&lt;br /&gt; &lt;span style="font-weight: bold; font-style: italic; color: rgb(51, 51, 255);"&gt;Ding!&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);"&gt; &lt;/span&gt;&lt;span style="font-weight: bold; font-style: italic; color: rgb(51, 51, 255);"&gt;Know your destination&lt;/span&gt;&lt;br /&gt;  &lt;span style="font-style: italic; font-weight: bold; color: rgb(51, 51, 255);"&gt;Ding!&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);"&gt; &lt;/span&gt;&lt;span style="font-style: italic; font-weight: bold; color: rgb(51, 51, 255);"&gt;Be a fast code-reader&lt;/span&gt;&lt;br /&gt; &lt;span style="font-style: italic; font-weight: bold; color: rgb(51, 51, 255);"&gt;Ding!&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);"&gt; &lt;/span&gt;&lt;span style="font-weight: bold; font-style: italic; color: rgb(51, 51, 255);"&gt;Know at least one scripting language really well&lt;/span&gt;&lt;br /&gt; &lt;span style="font-style: italic; font-weight: bold; color: rgb(51, 51, 255);"&gt;Ding! &lt;/span&gt;&lt;span style="font-weight: bold; font-style: italic; color: rgb(51, 51, 255);"&gt;Know your platform ecosystem&lt;/span&gt;&lt;br /&gt; &lt;span style="font-style: italic; font-weight: bold; color: rgb(51, 51, 255);"&gt;Ding! Don't rewrite anything&lt;/span&gt;&lt;br /&gt;  &lt;span style="font-style: italic; font-weight: bold; color: rgb(51, 51, 255);"&gt;Ding! Take careful risks and prototype&lt;/span&gt;&lt;br /&gt; &lt;span style="font-weight: bold; font-style: italic; color: rgb(51, 51, 255);"&gt;Ding! Have at least one high-level functional test&lt;/span&gt;&lt;br /&gt;         &lt;span style="font-weight: bold; font-style: italic; color: rgb(51, 51, 255);"&gt;Ding! Test the difficult stuff&lt;/span&gt;&lt;br /&gt;        &lt;span style="font-weight: bold; font-style: italic;"&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;Ding! Isolate the file system&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;Ding! Use the console&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;Ding! Have lots of sample data&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-style: italic; font-weight: bold; color: rgb(51, 51, 255);"&gt;Ding! Time is money, concepts are time&lt;/span&gt;&lt;br /&gt;    &lt;span style="font-weight: bold; font-style: italic; color: rgb(51, 51, 255);"&gt;Ding! PPP: Practice, Practice, Practice!!!&lt;/span&gt;&lt;br /&gt;   &lt;span style="font-style: italic; font-weight: bold; color: rgb(51, 51, 255);"&gt;Ding! Cool down and document/review your code&lt;/span&gt;&lt;br /&gt;  &lt;span style="font-weight: bold; font-style: italic; color: rgb(51, 51, 255);"&gt;Ding! Cool down and take a break&lt;/span&gt;&lt;br /&gt; &lt;/update&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; font-weight: bold;"&gt;The project&lt;/span&gt;: &lt;span style="font-weight: bold; font-style: italic;"&gt;go see your doctor, now!&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;In my company we've setup a methodology and a set of tools to assess the performance of our customers deployments and one of them asked us to come over for one week to deploy this so-called "HealthCheck" process. After the first initial interviews, we determined that one very essential objective of the project was to reduce the time to save a trade from 1.2 seconds to less than 500 ms. Very clear and quantifiable objective, that's a good start!&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic; color: rgb(51, 51, 255);"&gt;Ding!&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);"&gt; &lt;/span&gt;&lt;span style="font-weight: bold; font-style: italic; color: rgb(51, 51, 255);"&gt;Know your destination&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;First point here, this may look totally obvious but still it needs to be said: the overall user objective must be very clear for everyone.&lt;br /&gt;&lt;br /&gt;This point is important for at least 4 reasons:&lt;br /&gt;&lt;ol&gt;&lt;li&gt;It's hard to say that you're going fast if you don't know where you're going!&lt;br /&gt;&lt;/li&gt;&lt;li&gt;The fastest development in the world is the one you don't do because you don't need it&lt;/li&gt;&lt;li&gt;It helps a lot, as seen later, to have a clear overall objective when negotiating with yourself or with your customer the next micro-task to develop&lt;/li&gt;&lt;li&gt;Developing fast is not the alpha and omega of productivity. Seeing the project from its overall objective can make you realize that saving other people's time may actually more important than saving your own&lt;br /&gt;&lt;/li&gt;&lt;/ol&gt;&lt;span style="font-style: italic; font-weight: bold;"&gt;Reading the thermometer&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Assessing the performance of our systems involves parsing and analyzing lots of different log files: client requests, server requests, database requests, sql execution times, workflow times, garbage collection times,... The scripts we started working with were shell scripts doing the following:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Reading and managing log files, including archiving old versions&lt;/li&gt;&lt;li&gt;Parsing the files and extracting the time spent for a given "quantity" (client request, SQL call,...)&lt;/li&gt;&lt;li&gt;Computing small statistics: minimum / average / maximum time&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Creating graphs in order to be able to spot "spikes" that we will be able to label as "Red flags"&lt;/li&gt;&lt;/ul&gt;The scripts we had were doing the job, but were pretty slow considering the amount of data we had to process. More than one hour could be spent just running the scripts on a subset of the log files.&lt;br /&gt;&lt;br /&gt;Analyzing the scripts, I thought that I could implement something more effective,(...)&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; font-weight: bold; color: rgb(51, 51, 255);"&gt;Ding!&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);"&gt; &lt;/span&gt;&lt;span style="font-style: italic; font-weight: bold; color: rgb(51, 51, 255);"&gt;Be a fast code-reader&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Commando programming may involve reading a lot of code that's not yours before being able to do anything else. This has been written before: we usually spend far more time reading code than writing it. Reading code faster will necessarily speed you up.&lt;br /&gt;&lt;br /&gt;(...) more effective so I started prototyping something using Ruby. Why Ruby? Because I knew I would be fast to implement my ideas with it. Or was I?&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; font-weight: bold; color: rgb(51, 51, 255);"&gt;Ding!&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);"&gt; &lt;/span&gt;&lt;span style="font-weight: bold; font-style: italic; color: rgb(51, 51, 255);"&gt;Know at least one scripting language really well&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;No, I wasn't so fast,... I haven't programmed in Ruby for quite some time and I've forgotten many of the idioms or methods of the core api. My head is so full of Scala nowadays, that I would certainly have been faster using Scala for that task. The morality here is that knowing a "scripting" language is very helpful but you really need to have everything in your mental RAM in order to be effective.&lt;br /&gt;&lt;br /&gt;This goes for api knowledge as well as "environmental" knowledge: you should spend no time figuring out how to develop/build/test/deploy your code. Being stuck for 2 hours because you don't know how to set a class path for example is a huge waste of value.&lt;br /&gt;&lt;br /&gt;Anyway, I was able to show the effectiveness of using a language like Ruby to speed up the analysis time so I decided to officially port the scripts from shell to another language. But now which language should I select for my commando developments?&lt;br /&gt;&lt;br /&gt;Choosing a language is a vast question and the criteria governing that choice may not be entirely related to the intrinsic language features. This choice may also be governed by more "contextual" considerations, like the ability of other developers to learn the language and associated tools.&lt;br /&gt;&lt;br /&gt;In my case, Groovy was the winner for 4 reasons:&lt;br /&gt;&lt;ol&gt;&lt;li&gt;It has all sort of features to help write code faster like closures, literals,...&lt;/li&gt;&lt;li&gt;It can access Java libraries and I was planning to integrate some Database analysis tools we had written in Java&lt;br /&gt;&lt;/li&gt;&lt;li&gt;That's one of the closest language to Java on the JVM so other developers will be able to pick up the new scripts faster. And it is usually better known than JRuby, Scala or Jython for example&lt;br /&gt;&lt;/li&gt;&lt;/ol&gt;&lt;span style="font-style: italic; font-weight: bold; color: rgb(51, 51, 255);"&gt;Ding! &lt;/span&gt;&lt;span style="font-weight: bold; font-style: italic; color: rgb(51, 51, 255);"&gt;Know your platform ecosystem&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;I also knew, when choosing Groovy, that I would be able to use all sorts of &lt;a href="http://groovy.codehaus.org/Cookbook+Examples"&gt;niceties&lt;/a&gt;. One of them was the Ant integration which allowed me to easily reuse the &lt;a href="http://ant.apache.org/manual/CoreTasks/exec.html"&gt;exec&lt;/a&gt; task (see below). Knowing my way around Ant was a good thing (I know that's not exceptional for a Java developer, it's just an example ;-) ).&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;Rewrite everything?&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;That was the most "dangerous" part of this project.&lt;br /&gt;&lt;br /&gt;I would honestly have preferred avoiding it. Since I had to replace one small part of the scripts with some new Groovy logic and also, since the scripts would need to be extended anyway, I decided to rewrite them all in Groovy.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; font-weight: bold; color: rgb(51, 51, 255);"&gt;Ding! Don't rewrite anything&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Haha, I just said I did it! Well, not that you can never rewrite code or some library part, but you have to be damn sure about what you're doing. So,...&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; font-weight: bold; color: rgb(51, 51, 255);"&gt;Ding! Take careful risks and prototype&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Doing "Commando programming" will undoubtedly require taking risks and trying out some solutions before going on. More than ever, it needs to be done with lots of care because the last thing you want, is to find yourself in the middle of the road at the end of the week, because you didn't have time to finish implementing your new "Grand Vision", whatever it is.&lt;br /&gt;&lt;br /&gt;In my case I mitigated the risks a lot by "wrapping" the existing shell commands in my Groovy scripts. So I was essentially running the same thing but executed from Groovy! That was really worth it because in the process, I could also reduce a lot the existing amount of code thanks to the usual Groovy tricks.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;Test or not to test&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Now we come to the meat of the discussion! TDD, BDD, unit tests, code-and-see, what should be done? I honestly don't have a definitive answer but here's what I did.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic; color: rgb(51, 51, 255);"&gt;Ding! Have at least one high-level functional test&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;You need to have at least one high-level functional test guiding you in your developments. Something which you can come back to and say: "this case still doesn't pass, I'm not finished yet". You may actually have implemented more code than just what was necessary to pass that test case but at least this primary use case should be ok.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic; color: rgb(51, 51, 255);"&gt;Ding! Test the difficult stuff&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Complex logic is definitely something you want to isolate and test early. If you don't unit test it thoroughly, it will usually haunt you in the most painful way: hidden inside all the rest. Besides, this complex logic usually means "value" for your customer, more than anything else, so it's worth cherishing it.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;Ding! Isolate the file system&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;Actually, you should soon realize that any interaction with the file system is slowing you down.&lt;br /&gt;Being able to mock the file system, or to write to temporary files with automatic clean up is a huge time-saver for unit testing. In a "Commando" situation you would be prepared with this and have all sorts of functions and ready to use design to help with this. In my case, I had to write all lot of it,...&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;Ding! Use the console&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;The &lt;a href="http://en.wikipedia.org/wiki/REPL"&gt;Read-Eval-Print loop&lt;/a&gt;. In my developments I found very useful to test interactively my regular expressions inside the &lt;a href="http://groovy.codehaus.org/Groovy+Console"&gt;Groovy console&lt;/a&gt;. Whether or not I turned those expressions to actual unit tests,... I didn't do it all the time I must say. One reason is that, once I had confidence that this regular expression was encapsulated in a higher-level concept, like "extractClassName", it would be right forever.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;Ding! Have lots of sample data&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;If possible try to gather as much sample data as you can and run it through your program. Does it blow-up right away? Well, that's actually good, it may be the sign that you forgot a requirement. Instead of spending time trying to refine your requirements until they're exhaustive, or try to think about all possible cases, run the system with the maximum data available. There are 2 drawbacks to this: one is the time spent re-executing large datasets and analyzing failures, the other is not knowing what to look at! Your program may not break with enough smoke to prove you wrong.&lt;br /&gt;&lt;br /&gt;On my project I had lots of log files I could run my scripts against. And fortunately my scripts broke in very unambiguous ways, showing where I was wrong.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;To design or not to design&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;So you're programming like hell, you don't have time to draw all those nifty UML diagrams or devise the best design patterns for the job. "We'll design when we'll have time."&lt;br /&gt;&lt;br /&gt;But is it really buying you time? I don't think so. I'm not saying that you should write diagrams or add tons of interface and clever indirections.&lt;br /&gt;&lt;br /&gt;But you need design in the sense that the problem and solution domains should be very, very clear to you.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; font-weight: bold; color: rgb(51, 51, 255);"&gt;Ding! Time is money, concepts are time&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;So it's worth taking a bit of time and think: "What's really this system like?". Clarifying the concepts is very, very important. For us, it was first of all giving proper names to our analysis concepts:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;An entity can have measurable properties ("A trade server has requests")&lt;/li&gt;&lt;li&gt;For each property, you can define several quantities ("each request can take some average time")&lt;/li&gt;&lt;li&gt;For a given "Test environment", we can run several "Test campaigns"&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;This helped a lot in our discussions, in structuring the scripts and planning our actions.&lt;br /&gt;&lt;br /&gt;The next step was to realize that the type of queries we wanted to execute to analyze logging data looked pretty much like SQL queries: "between 15:00 and 15:03. return all update queries with a time &gt; 300ms and group them by workflow rules". This gave us a good hint that the next step for our scripts would be to put everything in a database!&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;Coding&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Consider this:&lt;br /&gt;&lt;br /&gt;"I know where I'm going, I have a clear mind about my system and it's concepts, I know how to write minimal and effective tests, is there anything more I could do now to speed up my coding?"&lt;br /&gt;&lt;br /&gt;Let's reformulate the sentence above:&lt;br /&gt;&lt;br /&gt;"I know I want a gold medal at the Olympics, I know I'll get it by being a table tennis champion and I know the rules of the game, I have a very good racket,  is there anything more I could do now to be the best at table tennis?"&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic; color: rgb(51, 51, 255);"&gt;Ding! PPP: Practice, Practice, Practice!!!&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;That's so obvious I'm ashamed to have to write it,... but that makes a real difference for "Commando programming":&lt;br /&gt;&lt;ul&gt;&lt;li&gt;technique: practice your regular-expression-fu for example, or know how to &lt;a href="http://etorreborre.blogspot.com/2008/07/my-first-icfp-contest-part-2-parser.html"&gt;write a parser for a simple language&lt;/a&gt; (I planned to do that for the verbose gc logs but we didn't have time nor interest to rewrite this part)&lt;br /&gt;&lt;/li&gt;&lt;li&gt;libraries: no time should be spent looking at the API docs&lt;/li&gt;&lt;li&gt;tools: if you need remote debugging, it should be fast to setup, because you did it thousands of times before&lt;br /&gt;&lt;/li&gt;&lt;li&gt;computer science: you should have at least a good sense of the complexity of your algorithms&lt;/li&gt;&lt;li&gt;integration: how to call Excel from Groovy to create graphics was one of our coding issues&lt;/li&gt;&lt;li&gt;system / network tools: that wasn't too necessary on our project, but I can imagine this making a real difference in other settings&lt;/li&gt;&lt;/ul&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;Cooling down for a minute&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Two things of equal importance I've also noticed during that week. First of all, when I tried to go fast I couldn't help but noticing entropy. Yes my code was doing the job, but at the same time it wasn't always consistent, documented, properly named or refactored.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; font-weight: bold; color: rgb(51, 51, 255);"&gt;Ding! Cool down and document/review your code&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;This really can look like a pure loss of time. But I really observed that it wasn't. For 2 reasons:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Actually it doesn't take that long to review/document/refactor the code (try it at home)! Except maybe for the naming part, it's a bit like playing sudoku, you just try to make things fall into place&lt;/li&gt;&lt;li&gt;I observed that I was much less dragged down implementing new feature on clear-cut code with obvious intentions, I had much less mental noise. You know, like: "This &lt;span style="font-family:courier new;"&gt;computeTotal&lt;span style="font-family:arial;"&gt; &lt;/span&gt;&lt;/span&gt;function is actually also sorting the results, so I know I don't have to do it there". If computing and sorting are better separated or named, that can reduce the "mental tax" while reasoning about something else.&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;The second thing is the equivalent of "cooling down the code",... applied to your body!&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic; color: rgb(51, 51, 255);"&gt;Ding! Cool down and take a break&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;We're not machines. I was so enthusiastic with that project that I almost coded day and night (and I was away from my family,...). But I also observed one very interesting phenomenon: time was slowing down! Sometimes I was just starring at my screen without realizing that 5 minutes had passed without doing nothing substantial. That should be a clear signal for taking a break. This gives me an interesting idea: "stopwatch" programming. Prepare the list of tasks you want to program and program features by 5, 7  or 10 minutes cycles. And watch out when you have almost empty cycles.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; font-weight: bold;"&gt;The doctor says: fit for service&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Conclusion for the project: it was very successful (this doesn't happen all the time so I'm glad to report it). We were able to analyze and spot 3 major bottlenecks and give good recommendations so that our maximum saving time was eventually around 400 ms. The tools served us, but actually ours brains served us more.&lt;br /&gt;&lt;br /&gt;But what's the biggest room in the world? The room for improvement ;-) ! I think we'll be able to transfer more of that wisdom to smarter analysis scripts in the future.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;Back to the original question&lt;br /&gt;&lt;/span&gt;&lt;blockquote&gt;When you program for a trader you don't have time to write tests&lt;/blockquote&gt;True? False? At the light of my experience of this project, the first thing I'm really convinced of is the first part of the sentence: &lt;span style="font-weight: bold;"&gt;"You don't have time"&lt;/span&gt;!&lt;br /&gt;&lt;br /&gt;And if you don't have time, you can essentially do 2 things:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;u&gt;Observe and experiment&lt;/u&gt;: whatever makes you spare time is good. In my case, writing tests is a proven way to go faster for example&lt;br /&gt;&lt;/li&gt;&lt;li&gt;&lt;u&gt;Practice, learn, invest&lt;/u&gt;:  new tools, new languages, new libraries, &lt;a href="http://etorreborre.blogspot.com/2008/07/my-first-icfp-contest-part-2-parser.html"&gt;contests&lt;/a&gt;, &lt;a href="http://codingdojo.org/"&gt;coding dojos&lt;/a&gt;,...&lt;/li&gt;&lt;/ul&gt;Ok. Thanks for listening, I leave you there, I have to go,... and practice my "Commando-fu"!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5336273-8565375419195743614?l=etorreborre.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://etorreborre.blogspot.com/feeds/8565375419195743614/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5336273&amp;postID=8565375419195743614' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5336273/posts/default/8565375419195743614'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5336273/posts/default/8565375419195743614'/><link rel='alternate' type='text/html' href='http://etorreborre.blogspot.com/2008/10/commando-programming.html' title='Commando programming'/><author><name>Eric</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://farm3.static.flickr.com/2203/2118752971_7becaf7476_t.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5336273.post-8009657677922259357</id><published>2008-07-27T15:14:00.004+09:00</published><updated>2008-07-27T17:05:39.440+09:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='haskell'/><category scheme='http://www.blogger.com/atom/ns#' term='scala'/><title type='text'>My first ICFP contest, Part 2: parser combinators are so cool</title><content type='html'>This is the sequel to my &lt;a href="http://etorreborre.blogspot.com/2008/07/my-first-icfp-contest-part-1-ten-days.html"&gt;last post on participating to the ICFP contest 2008&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;The Setup&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;As Curt &lt;a href="http://www.starling-software.com/en/blog/icfpc2008/2008/07/12.well-start-one-day.html"&gt;wrote it&lt;/a&gt;, we entered the contest doing things that should have been done well before:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;setting up the wifi access so that I can connect with my laptop (it never worked so Bryan had to get out and buy new network cables)&lt;br /&gt;&lt;/li&gt;&lt;li&gt;exchanging keys to access the Subversion repository&lt;/li&gt;&lt;li&gt;learning how to use the &lt;a href="http://www.starling-software.com/en/qam.html"&gt;build and test system&lt;/a&gt;&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;I guess that we need to make some mistakes (at least) once oneself before getting it!&lt;br /&gt;&lt;br /&gt;However, the biggest lesson for me in this contest, was the use of Parser Combinators.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;Do you speak Martian?&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The second programming task in the contest, after setting up the network connection to be able to receive messages and send back commands, was to parse the input messages giving the position of the Martian rover, and its immediate environment.&lt;br /&gt;&lt;br /&gt;As &lt;a href="http://smlnj.org/icfp08-contest/task.html#htoc7"&gt;the task specifies&lt;/a&gt;,  there are 5 kinds of messages:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;initialization messages -&gt; what's the map like? what are you technical constants?&lt;br /&gt;&lt;/li&gt;&lt;li&gt;telemetry stream -&gt; where are you, what's your speed, what's around?&lt;/li&gt;&lt;li&gt;adverse event -&gt; crashed in a crater!&lt;br /&gt;&lt;/li&gt;&lt;li&gt;success -&gt; yes, home!&lt;br /&gt;&lt;/li&gt;&lt;li&gt;end of run -&gt; time and status&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;The format of those messages is not particularly complex but the telemetry message can contain an unlimited number of obstacles descriptions for example.&lt;br /&gt;&lt;br /&gt;For many developers, the most obvious way to transforms meaningless strings to meaningful objects like Telemetry or Martian, would be to start traversing the string, check for characters like 'T' for Telemetry, or 'I' for Initialization then pass the rest of the string to another function and continue slicing the string until all data is analyzed.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;i&gt;Parser combinators&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;But Haskell developers know better,... They can just describe the grammar of the messages they're parsing by &lt;span style="font-style: italic;"&gt;combining parsers&lt;/span&gt;, then applying the resulting parser to the string input.&lt;br /&gt;&lt;br /&gt;Something like that: &lt;br /&gt;&lt;code class="prettyprint"&gt;&lt;br /&gt;telemetry :: Parser Message&lt;br /&gt;telemetry = do ts &lt;- int&lt;br /&gt;               state &lt;- accelStateParser&lt;br /&gt;               turning &lt;- turningStateParser&lt;br /&gt;               spaces&lt;br /&gt;               x &lt;- double&lt;br /&gt;               y &lt;- double&lt;br /&gt;               dir &lt;- double&lt;br /&gt;               speed &lt;- double&lt;br /&gt;               obstacles &lt;- obstaclesParser&lt;br /&gt;               messageEnd&lt;br /&gt;               return $ Telemetry ts state turning x y dir speed obstacles&lt;br /&gt;&lt;br /&gt;accelStateParser :: Parser AccelState&lt;br /&gt;accelStateParser = (accelerating &lt;|&gt; braking &lt;|&gt; rolling)&lt;br /&gt;&lt;br /&gt;accelerating = stateParser 'a' Accelerating&lt;br /&gt;braking = stateParser 'b' Braking&lt;br /&gt;rolling = stateParser '-' Rolling&lt;br /&gt;&lt;br /&gt;stateParser :: Char -&gt; a -&gt; Parser a&lt;br /&gt;stateParser c cons = do _ &lt;- char c&lt;br /&gt;                        return cons&lt;br /&gt;&lt;br /&gt;turningStateParser :: Parser TurningState&lt;br /&gt;turningStateParser = hardleft &lt;|&gt; softleft&lt;br /&gt;                         &lt;|&gt; hardright &lt;|&gt; softright&lt;br /&gt;                         &lt;|&gt; straight&lt;br /&gt;&lt;br /&gt;hardleft  = stateParser 'L' HardLeft&lt;br /&gt;softleft  = stateParser 'l' SoftLeft&lt;br /&gt;hardright = stateParser 'R' HardRight&lt;br /&gt;softright = stateParser 'r' SoftRight&lt;br /&gt;straight  = stateParser '-' Straight&lt;br /&gt;obstaclesParser :: Parser [Object]&lt;br /&gt;obstaclesParser = do l &lt;- many obstacleParser&lt;br /&gt;                     return l&lt;br /&gt;&lt;br /&gt;obstacleParser :: Parser Object&lt;br /&gt;obstacleParser = do code &lt;- oneOf "bchm"&lt;br /&gt;                    spaces&lt;br /&gt;                    case code of&lt;br /&gt;                         'b' -&gt; object Boulder&lt;br /&gt;                         'c' -&gt; object Crater&lt;br /&gt;                         'h' -&gt; object Home&lt;br /&gt;                         'm' -&gt; martian&lt;br /&gt;                         _   -&gt; error "Can't get here in parser!"&lt;br /&gt;&lt;br /&gt;object :: (Circle -&gt; a) -&gt; Parser a&lt;br /&gt;object cons = do x &lt;- double&lt;br /&gt;                 y &lt;- double&lt;br /&gt;                 r &lt;- double&lt;br /&gt;                 spaces&lt;br /&gt;                 return $ cons (Circle (x:.y) r)&lt;br /&gt;&lt;br /&gt;martian :: Parser Object&lt;br /&gt;martian = do x &lt;- double&lt;br /&gt;             y &lt;- double&lt;br /&gt;             dir &lt;- double&lt;br /&gt;             speed &lt;- double&lt;br /&gt;             spaces&lt;br /&gt;             return $ Martian (Circle (x:.y) 0.4) dir speed&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;In the above code, you can read that the parser for telemetry messages is a sequence of different parsers: a timestamp parser, a acceleration state parser, a turning state parser, ..., a parser for lists of obstacles. And a parser for list of obstacles is just "many" parsers for one obstacle. This is indeed as easy as being able to describe a grammar for that mini-language. &lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;i&gt;"k" should better be 1&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;But I didn't say that describing a grammar was easy! For instance, at first, we made a mistake in the way we managed spaces. We had almost each parser trying to consume spaces before doing its job. This generated unnecessary ambiguity in the parsing. &lt;br /&gt;&lt;br /&gt;For example, let's say that you want to be able to parse this kind of message:&lt;br /&gt; &lt;code class="prettyprint"&gt;&lt;br /&gt;" O T O O T"&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;(honestly, I don't know why you would like to do something like that but you're the boss here)&lt;br /&gt; &lt;br /&gt;You'd better combine those 3 parsers:&lt;ul&gt;&lt;li&gt;spaces -&gt; will consume any number of spaces &lt;/li&gt;&lt;br /&gt;&lt;li&gt;oneParser = char 'O' &gt;&gt;= spaces -&gt; will consume 'O' followed by any number of spaces&lt;/li&gt;&lt;br /&gt;&lt;li&gt;twoParser = char 'T' &gt;&gt;= spaces -&gt; will consume 'T' followed by any number of spaces &lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;If you define parsers differently, having "oneParser" and "twoParser" consuming spaces before they parse their character, which parser should be used on the first space of the message? &lt;br /&gt;&lt;br /&gt;Actually, parser combinators in Haskell are also capable of backtracking and "unparsing" results if the chosen parser was not appropriate (trying things with the the "try" parser combinator). But why add more complexity when things can be described more directly?&lt;br /&gt;&lt;br /&gt;Of course, there is a formal theory behind the example above: &lt;a href="http://en.wikipedia.org/wiki/LL_parser&lt;br /&gt;"&gt;LL(k) grammars&lt;/a&gt;, where LL(1) is a type of grammar only needing to look at the next character to know which parser to use. But there's nothing like a simple "real-life" example to be able to grasp it.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;i&gt;Compare and contrast&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Now, honestly, most other contest solutions were not using Parser combinators and the aren't soooo ugly (&lt;a href="http://hpaste.org/8927"&gt;in Dylan for example&lt;/a&gt;). I think they're just less readable and more error-prone because of index manipulation.&lt;br /&gt;&lt;br /&gt;For fun and comparison, I also developed a similar parser in Scala:&lt;br /&gt;&lt;code class="prettyprint"&gt;&lt;br /&gt;  def telemetryMessage = for { timestamp    &lt;- intNumber&lt;br /&gt;                               accState     &lt;- accelState&lt;br /&gt;                               turnState    &lt;- turningState&lt;br /&gt;                               vehicleX     &lt;- doubleNumber&lt;br /&gt;                               vehicleY     &lt;- doubleNumber&lt;br /&gt;                               vehicleDir   &lt;- doubleNumber&lt;br /&gt;                               vehicleSpeed &lt;- doubleNumber&lt;br /&gt;                               obstacles    &lt;- objects }&lt;br /&gt;                          yield Telemetry(timestamp, accState(), turnState(),&lt;br /&gt;                                          vehicleX, vehicleY, vehicleDir, &lt;br /&gt;                                          vehicleSpeed, obstacles)&lt;br /&gt;                        &lt;br /&gt;  def accelState = (  &lt;br /&gt;      "a" ^^^ Accelerating&lt;br /&gt;    | "b" ^^^ Braking&lt;br /&gt;    | "-" ^^^ Rolling&lt;br /&gt;  )&lt;br /&gt; &lt;br /&gt;  def turningState = (  &lt;br /&gt;      "l" ^^^ SoftLeft&lt;br /&gt;    | "L" ^^^ HardLeft&lt;br /&gt;    | "-" ^^^ Straight&lt;br /&gt;    | "r" ^^^ SoftRight&lt;br /&gt;    | "R" ^^^ HardRight&lt;br /&gt;  )&lt;br /&gt;&lt;br /&gt;  def objects = rep(objectParser)&lt;br /&gt;  def objectParser: Parser[Object] = (&lt;br /&gt;      "b" ~ spaces ~&gt; circleParser ^^ (new Boulder(_))&lt;br /&gt;    | "c" ~ spaces ~&gt; circleParser ^^ (new Crater(_))&lt;br /&gt;    | "h" ~ spaces ~&gt; circleParser ^^ (new Crater(_))&lt;br /&gt;    | "m" ~ spaces ~&gt; martianParser&lt;br /&gt;  )&lt;br /&gt;     &lt;br /&gt;  def circleParser = for { x &lt;- doubleNumber&lt;br /&gt;                           y &lt;- doubleNumber&lt;br /&gt;                           r &lt;- doubleNumber }&lt;br /&gt;                     yield new Circle(x, y, r)&lt;br /&gt;                    &lt;br /&gt;  def martianParser = for { circle &lt;- circleParser&lt;br /&gt;                            dir    &lt;- doubleNumber&lt;br /&gt;                            speed  &lt;- doubleNumber }&lt;br /&gt;                      yield new Martian(circle, dir, speed)&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;The interesting things in the Scala implementation, although it could seem a bit ugly at first sight, are:&lt;ul&gt;&lt;br /&gt;&lt;li&gt;the use of a sequence operator "~&gt;" getting rid of what has already been parsed. Indeed, when we have parsed "b " we know we need to create a Boulder object. The 'b' character and the space are then not usefull anymore&lt;/li&gt;&lt;br /&gt;&lt;li&gt;the use of functions to transform the parsed string into a meaningful object, like "l" ^^^ SoftLeft, creating a new SoftLeft object (it's a case class) when parsing "l"&lt;/li&gt;&lt;br /&gt;&lt;li&gt;implicit definitions. In the example above I don't need to declare my parser for 'l' as letter("l"). Combining "l" with another parser automatically creates a parser for the character 'l'&lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;&lt;b&gt;&lt;i&gt;Why XML?&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;One of the reasons for the ubiquitous adoption (and hate!) of XML is, I think, the general availability of parsers and binding libraries. This is why this often becomes the easiest option for configuration files (is it changing with  YAML and JSON?). &lt;br /&gt;&lt;br /&gt;It doesn't have to be that way. I hope that parser combinators can now bring another legitimate option when considering the implementation of an external mini-language for configuration or protocol. They're simple to use and readily available on your Java platform through Scala. Try them at home!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5336273-8009657677922259357?l=etorreborre.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://etorreborre.blogspot.com/feeds/8009657677922259357/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5336273&amp;postID=8009657677922259357' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5336273/posts/default/8009657677922259357'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5336273/posts/default/8009657677922259357'/><link rel='alternate' type='text/html' href='http://etorreborre.blogspot.com/2008/07/my-first-icfp-contest-part-2-parser.html' title='My first ICFP contest, Part 2: parser combinators are so cool'/><author><name>Eric</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://farm3.static.flickr.com/2203/2118752971_7becaf7476_t.jpg'/></author><thr:total>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5336273.post-1793537994481641070</id><published>2008-07-08T22:32:00.006+09:00</published><updated>2008-07-17T00:58:29.354+09:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='icfp08'/><category scheme='http://www.blogger.com/atom/ns#' term='haskell'/><title type='text'>My first ICFP contest, Part 1: ten days to learn Haskell</title><content type='html'>I wanted to do that since I read &lt;a href="http://blog.moertel.com/pages/seven-lessons-from-the-icfp-programming-contest"&gt;Tom's Moertel post&lt;/a&gt; on his participation to the ICFP contest 2001. I Told my wife: "One day, I'll participate to the &lt;a href="http://icfpcontest.org/"&gt;ICFP contest&lt;/a&gt;" (head slightly tilted up with a brilliant and determined look in the eyes).&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; font-weight: bold;"&gt;One day, I'll participate to the ICFP contest&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Fortunately, that day came quick as I met, last month, &lt;a href="http://www.starling-software.com/en/"&gt;other geeks in Tokyo&lt;/a&gt; ready to get into the race. I should say über-geeks when I realize the gaps I have in my knowledge of some fields of computing compared to them. System and network programming being only the emerged part of the iceberg.&lt;br /&gt;&lt;br /&gt;I met them for the first time during the monthly meeting of the &lt;a href="http://www.starling-software.com/en/blog/tokyo-functional-programming/"&gt;Tokyo Society for the Application of Currying&lt;/a&gt; (try to explain that to your grandma). That night we decided that we would form a team for the next ICFP contest. Then, we spent the next week discussing the team name, that was the difficult decision. The easy one was the language we would use. What is THE language of choice for functional hackers? &lt;a href="http://www.haskell.org/"&gt;Haskell&lt;/a&gt; of course! [flaming starts here]&lt;br /&gt;&lt;br /&gt;But how do you learn Haskell in 10 days and hope to be able to contribute at least a little?&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;Learn Haskell in 10 days!&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Actually, I've reading about Haskell for a while now, starting with the very enjoyable &lt;a href="http://research.microsoft.com/%7Esimonpj/papers/history-of-haskell/history.pdf"&gt;A History of Haskell: Being LazyWith Class&lt;/a&gt;. From there and working with Scala for the past year, I had understood the following:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;the "functional" approach vs the "sequential" approach&lt;/li&gt;&lt;li&gt;immutable data structures&lt;br /&gt;&lt;/li&gt;&lt;li&gt;functions as first-class objects&lt;br /&gt;&lt;/li&gt;&lt;li&gt;lazy evaluation&lt;/li&gt;&lt;li&gt;thinking in types&lt;/li&gt;&lt;li&gt;thinking in higher-ranked types&lt;/li&gt;&lt;li&gt;Options / Maybe&lt;/li&gt;&lt;li&gt;List operations: map, folds,...&lt;/li&gt;&lt;li&gt;pattern matching&lt;/li&gt;&lt;li&gt;type classes / traits&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;This makes an impressive list of language features but I must say that Scala is a really "transitional language" for Java programmers towards Haskell. Riiiight. I "understand" it but how well can I program within 10 days?&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;The laundry list&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Using a programming language always involves a wealth of down-to-earth considerations worth getting right as fast as possible. Here is my list and how I organized my environment:&lt;br /&gt;&lt;ol&gt;&lt;li&gt;the compiler: I used GHC and the Hugs interpreter for Windows&lt;/li&gt;&lt;li&gt;the editor: I used the Functional plugin for eclipse, providing editing, compilation, creation of an executable&lt;br /&gt;&lt;/li&gt;&lt;li&gt;the librairies: I downloaded / compiled and installed QuickCheck2 and otherwise used the bundled libraries with GHC&lt;/li&gt;&lt;li&gt;code coverage: hcp (not really indispensable for a beginning but a nice to have for a non-strict language. I was happy to eventually get it to work)&lt;br /&gt;&lt;/li&gt;&lt;/ol&gt;This is clearly non-optimal yet (and I wonder if it can be on Windows?) and I'll certainly explore:&lt;br /&gt;&lt;ol&gt;&lt;li&gt;a better editor: considering what can be done in Java, it's pretty amazing that there is no high-class editor for Haskell. I'll try Yi of course, but it's still in its infancy. "Rename function" or "Rename data type" should be pretty accessible (provided enough open-source or company support, that is,...)&lt;/li&gt;&lt;li&gt;a build/test/continuous integration system. The guys from &lt;a href="http://www.starling-software.com/"&gt;Starling Software&lt;/a&gt; have their own system for that matter (&lt;a href="http://www.starling-software.com/en/qam.html"&gt;QAM&lt;/a&gt;) and I haven't seen yet such a standard thing for Haskell. &lt;/li&gt;&lt;li&gt;more libraries, libraries management: I still haven't done it on windows but I plan to install Cabal and download the whole Hackage.&lt;/li&gt;&lt;li&gt;Switching to linux forever. I'll certainly cover this in part 2 but Windows seems less and less a decent alternative for hackers (I didn't say &lt;a href="http://www.youtube.com/watch?v=Vhh_GeBPOhs"&gt;developers&lt;/a&gt; ;-) ).&lt;br /&gt;&lt;/li&gt;&lt;/ol&gt;Yes, all set-up! I can start my first tutorial!&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; font-weight: bold;"&gt;No, I'd rather do a code dojo first&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Why bother using a tutorial when I have a nice little programming exercise at hand? I did it in Java, I did it in &lt;a href="http://etorreborre.blogspot.com/2006/01/roman-code-kata-mcmlxxiv-1974.html"&gt;Ruby&lt;/a&gt;, I did it in Scala, it was time to use Haskell.&lt;br /&gt;&lt;br /&gt;Basically the question is: write a program to transform Roman number back and forth to Integers: unromanize("MCMLXXII") == 1972.&lt;br /&gt;&lt;br /&gt;Here is my solution with Haskell:&lt;code class="prettyprint"&gt;&lt;br /&gt;&lt;br /&gt;-- SOME VALUES DECLARATION&lt;br /&gt;-- definition of the roman letters with their numerical value&lt;br /&gt;romansList = [("I", 1), ("IV",4), ("V", 5), ("IX", 9), ("X", 10), ("XL", 40),&lt;br /&gt;              ("L", 50), ("XC", 100), ("C", 100), ("CD", 400), ("D", 500), &lt;br /&gt;              ("CM", 900), ("M", 1000)]&lt;br /&gt;-- numerical values only&lt;br /&gt;values = map snd romansList&lt;br /&gt;-- letters only&lt;br /&gt;letters = map fst romansList&lt;br /&gt;&lt;br /&gt;-- return the value corresponding to a letter&lt;br /&gt;value :: String -&gt; Int&lt;br /&gt;value c = findWithDefault 1 c (fromList romansList)&lt;br /&gt;&lt;br /&gt;-- return the letter corresponding to a value&lt;br /&gt;letter :: Int -&gt; String&lt;br /&gt;letter i = findWithDefault "I" i reversed       &lt;br /&gt;          where reversed = fromList(map switch romansList)&lt;br /&gt;                switch = (\(x, y) -&gt; (y, x))&lt;br /&gt;&lt;br /&gt;-- to get the value of a RomanString, start&lt;br /&gt;-- from the end and accumulate the value of each letter&lt;br /&gt;-- substract the value of the current letter if its value is&lt;br /&gt;-- inferior to the previous one&lt;br /&gt;-- ex: "XIV" -&gt; 5 (for "V") - 1 (for "I") + 10 (for "X")&lt;br /&gt;unromanize :: String -&gt; Int&lt;br /&gt;unromanize s = fst(foldr add (0, 0) s)&lt;br /&gt;&lt;br /&gt;add :: Char -&gt; (Int, Int) -&gt; (Int, Int)&lt;br /&gt;add c (total, lastElem) = &lt;br /&gt;    if (lastElem &lt;= val) then (total + val, val)                           &lt;br /&gt;                         else (total - val, val)&lt;br /&gt;                           where val = value [c] &lt;br /&gt;                           -- [c] is the Char 'c' as a String   &lt;br /&gt;&lt;br /&gt;-- return the RomanString corresponding to an Int value &lt;br /&gt;-- start with an empty string and the value to convert &lt;br /&gt;-- use the romanizeString function to compute the RomanString &lt;br /&gt;&lt;br /&gt;romanize :: Int -&gt; String&lt;br /&gt;romanize i = fst $ romanizeString ("",  i)&lt;br /&gt;&lt;br /&gt;-- if the remainder is 0, there is no more conversion to do&lt;br /&gt;-- otherwise, try to substract the biggest possible RomanChar from the remainder&lt;br /&gt;-- this RomanChar is then appended to the current result&lt;br /&gt;romanizeString :: (String, Int) -&gt; (String, Int)&lt;br /&gt;romanizeString result@(res, remain) =&lt;br /&gt;   if (remain == 0) then result&lt;br /&gt;                    else romanizeString(res ++ (letter maxi), remain - maxi)&lt;br /&gt;                        where maxi = maxValue remain&lt;br /&gt;                              maxValue r = last (takeWhile (&lt;= r) values)&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;And here is a simple QuickCheck properties that I wrote to check the conversion:&lt;code class="prettyprint"&gt;&lt;br /&gt;prop_unromanize_romanize :: Int -&gt; Property&lt;br /&gt;prop_unromanize_romanize x = x &gt;= 0 ==&gt; unromanize(romanize x) == x&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;First impressions&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Working on that simple example (but also on string generators to check the reverse property romanize(unromanize) -- much more tricky) led me to the following observations:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;programming in Haskell really "feels" pure and elegant. There is an economy of symbols which is quite remarkable for such an expressivity&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;not having variables feels like programming in a straight jacket when coming from an imperative background! But the feeling disappears quickly&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;the "functional/data" orientation makes a whole different mental model when you come from an object-oriented perspective. It looks like your objects, encapsulating their data and showing only their business logic have been turned inside out and methods carefully separated from data.&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;browsing the libraries feels like there a huge drive towards abstraction in Haskell. Nowhere else I've seen concepts decomposed so that Monoids would emerge and be reused&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;new language, new librairies. It really takes time  but Haskell libraries are full of jewels which are really worth knowing inside out (the same can certainly be said from many languages)&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;although the Prelude contains a lot of useful functions, some facilities you may find in scripting languages are missing. We recently discussed the lack of a "split" operation for Strings on the Haskell mailing-list recently. I know that different people have different semantics for a "split" operation but not including any kind in the standard library feels like it's missing something&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;the compiler is your friend! It goes at great length explaining you why and where you failed to properly type your program&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;span style="font-style: italic; font-weight: bold;"&gt;Go, go, go&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;After those 2 or 3 sessions of installation and programming I was very far from being able to code on my own for the contest, yet I would be able to pair-up and provide a vigilant pair of eyes on someone else code.  Stay tuned for part 2: the contest!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5336273-1793537994481641070?l=etorreborre.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://etorreborre.blogspot.com/feeds/1793537994481641070/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5336273&amp;postID=1793537994481641070' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5336273/posts/default/1793537994481641070'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5336273/posts/default/1793537994481641070'/><link rel='alternate' type='text/html' href='http://etorreborre.blogspot.com/2008/07/my-first-icfp-contest-part-1-ten-days.html' title='My first ICFP contest, Part 1: ten days to learn Haskell'/><author><name>Eric</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://farm3.static.flickr.com/2203/2118752971_7becaf7476_t.jpg'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5336273.post-1488905446200230480</id><published>2008-07-03T22:37:00.009+09:00</published><updated>2008-07-08T13:21:44.380+09:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='monads'/><category scheme='http://www.blogger.com/atom/ns#' term='scala'/><title type='text'>Everyday monads</title><content type='html'>&lt;b&gt;&lt;i&gt;Abstraction&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;&lt;p&gt;Abstraction is the bread and butter of our trade, right? The problem is that when you abstract too much, you don't always know where you came from and why.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;You see? The sentence above is already very abstract ;-). Let's try to be more concrete. I want to talk about Monads and the kind of problems they solve:&lt;/p&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt; monads --&gt; powerful abstract beast&lt;/li&gt;&lt;li&gt; problems they solve --&gt; very concrete&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;b&gt;&lt;i&gt;A classical code sample&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;&lt;p&gt;I was trying to fix an issue with our software 2 days ago when I realized that part of my data wasn't valid: "What!? My exposure is not valid, how come!?". A quick look at the code revealed that the validity of an exposure is determined by the following (actually simplified,...):&lt;/p&gt;&lt;br /&gt;&lt;code class="prettyprint"&gt;public boolean isValid() {&lt;br /&gt;   return traded &amp;amp;&amp;amp; !matured &amp;amp;&amp;amp; &lt;br /&gt;          !excluded &amp;amp;&amp;amp; !internal &amp;amp;&amp;amp; leId &gt; 1;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;/p&gt;&lt;p&gt;Of course I started blaming the programmer: thank you smart boy, I know that my exposure is not valid but would you have the decency to explain me WHY? Is it untraded and/or matured and/or excluded,...?&lt;/p&gt;&lt;br /&gt;&lt;p&gt;I quickly realized why more precise error reporting hadn't been written: time to market. Writing big if/then/else and accumulating error messages is tedious, error-prone and boring to the extreme.&lt;/p&gt;&lt;br /&gt;&lt;b&gt;&lt;i&gt;Monads to the rescue!&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;&lt;p&gt;Somehow, that's the kind of problem that Monads can solve. One way to see Monads is to consider them as a convenient way to chain computations:&lt;/p&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;you put something in the Monad&lt;/li&gt;&lt;li&gt;you apply one or more functions to it, the results stay in the Monad ("What goes in the Matrix, stays in the Matrix" as I've read somewhere)&lt;/li&gt;&lt;li&gt;then you can extract the final result&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;br /&gt;Here is the use of a Monad (coded in Scala but you could achieve the same kind of effect in Java) to solve the problem:&lt;br /&gt;&lt;p&gt;&lt;code class="prettyprint"&gt;&lt;br /&gt;  def isValid(e: Exposure) = {&lt;br /&gt;     e.traded ?~! ("is not traded") ?~! (!e.matured, "is matured") ?~!&lt;br /&gt;     (!e.excluded, "is excluded") ?~! (!e.internal, "is internal") ?~!&lt;br /&gt;     (e.leId &gt; 1, "doesn't have a defined legal entity")&lt;br /&gt;  }&lt;br /&gt;  val exposure = new Exposure&lt;br /&gt;  exposure.traded = false&lt;br /&gt;  exposure.matured = true&lt;br /&gt;  isValid(exposure) match {&lt;br /&gt;    case f: Failure =&gt; {&lt;br /&gt;      println(f.messages.mkString("exposure is not valid because: it ", ", it ", ""))&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;&lt;/code&gt;&lt;/p&gt;&lt;p&gt;It outputs:&lt;br /&gt;&lt;code class="prettyprint"&gt; exposure is not valid because: it is not traded, it is matured, it doesn't have a defined legal entity&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;p&gt;So, at the (small) expense of changing the operator from &amp;amp;&amp;amp; to ?~! and adding a specific message at each step we get a nice failure message indicating precisely what went wrong.&lt;/p&gt;&lt;br /&gt;&lt;b&gt;&lt;i&gt;How does this work?&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;&lt;p&gt;I implemented this cryptic ?~! operator as a small variation of the existing ?~! method on the &lt;a href="http://github.com/dpp/liftweb/tree/master/lift/src/main/scala/net/liftweb/util/Can.scala"&gt;Can&lt;/a&gt; class from the &lt;a href="http://liftweb.net/index.php/Main_Page"&gt;lift&lt;/a&gt; webframework.&lt;/p&gt;&lt;p&gt;A Can is kind of box which either be &lt;span style="font-style: italic;"&gt;Empty&lt;/span&gt;, &lt;span style="font-style: italic;"&gt;Full(of something)&lt;/span&gt; or a &lt;span style="font-style: italic;"&gt;Failure(reason)&lt;/span&gt;. So when you perform computations on a Can, either:&lt;/p&gt;&lt;ol&gt;&lt;li&gt;there is nothing to do (Empty) --&gt; the result will stay Empty or become a Failure&lt;/li&gt;&lt;li&gt;there is a value (Full(value)) --&gt; the result will either stay Full or become a Failure&lt;/li&gt;&lt;li&gt;it is a Failure --&gt; it will stay a Failure, possibly with an additional error message&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;What are the 2 operations I need on the Can objects? First of all I need an operation to "put a boolean in the monad". Here I have used an implicit definition:&lt;br /&gt;&lt;code class="prettyprint"&gt;  implicit def returnCan(b: Boolean): Can[Boolean] = if (b) Full(true) else Empty&lt;br /&gt;&lt;/code&gt;&lt;p&gt;then I need one method which will either return the same Can if some boolean is true or a new Failure accumulating one more error message:&lt;/p&gt;&lt;code class="prettyprint"&gt;&lt;br /&gt;def ?~!(b: Boolean, msg: String): Can[A] = {&lt;br /&gt;  if (b) this else Failure(msg :: this.messages)&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;What happens if the first boolean expression is false?&lt;ol&gt;&lt;br /&gt;&lt;li&gt;an Empty Can is created (representing a failure with no cause yet)&lt;/li&gt;&lt;li&gt;the ?~!(msg: String) method is called and returns a Failure(msg)&lt;/li&gt;&lt;li&gt;when the ?~!(b: Boolean, msg: String) method is called, it either returns the same Failure, or create a new one with one more error message&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;And what happens if the first boolean expression is true?&lt;br /&gt;&lt;ol&gt;&lt;li&gt;a Full Can is created (representing a success)&lt;/li&gt;&lt;li&gt;the ?~!(msg: String) method is called and returns the Full can&lt;/li&gt;&lt;li&gt;when the ?~!(b: Boolean, msg: String) method is called, it either returns the same Full can if b is true, or create a new Failure Can with one error message&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;b&gt;&lt;i&gt;What I will not do in this post&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Although that would be interesting, I will not try to show why the behavior described above actually correspond to a Monad, with the mandatory &lt;a href="http://en.wikipedia.org/wiki/Monad_(functional_programming)#Axioms"&gt;Monad laws&lt;/a&gt;. The first reason is that it is too late to do so ;-). The second reason is that I'm not sure it matters much. What is certainly more important is that Monads, Arrows, Applicative Functors and &lt;a href="http://lucdup.blogspot.com/2008/01/tmp_22.html"&gt;their kind&lt;/a&gt; provide powerful models of computations which can also be applied to Everyday-Enterprisey jobs.&lt;br /&gt;&lt;br /&gt;And, by the way, Scala provides syntactic sugar for operators definition, but there is really nothing which avoid us to do the same thing in Java:&lt;code class="prettyprint"&gt;&lt;br /&gt;&lt;br /&gt;// warning!! pseudo-java code!!&lt;br /&gt;class Result {&lt;br /&gt;  private List&lt;String&gt; messages = new ArrayList&lt;String&gt;();&lt;br /&gt;  private Result(List&lt;String&gt; msgs) {&lt;br /&gt;    this.messages = msgs;&lt;br /&gt;  }&lt;br /&gt;  public Result andCheck(Boolean b, String msg) {&lt;br /&gt;    if (b) return this; &lt;br /&gt;    else return new Result(messages.append(msg));&lt;br /&gt;  }&lt;br /&gt;  static public check(Boolean b, String msg) {&lt;br /&gt;    return new Result().andCheck(b, msg);&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public Result isValid(e: Exposure) = {&lt;br /&gt;  check(e.traded, ("is not traded")).&lt;br /&gt;  andCheck(!e.matured, "is matured").&lt;br /&gt;  andCheck(!e.excluded, "is excluded").&lt;br /&gt;  andCheck(!e.internal, "is internal").&lt;br /&gt;  andCheck(e.leId &gt; 1, "doesn't have a defined legal entity")&lt;br /&gt;}&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;i&gt;Eureka&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;I hope I haven't brought more confusion on the topic of Monads with that not-very-formal post. This is a world I'm just starting to explore and I just love when I can have some "Eureka" moments and write better code!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5336273-1488905446200230480?l=etorreborre.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://etorreborre.blogspot.com/feeds/1488905446200230480/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5336273&amp;postID=1488905446200230480' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5336273/posts/default/1488905446200230480'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5336273/posts/default/1488905446200230480'/><link rel='alternate' type='text/html' href='http://etorreborre.blogspot.com/2008/07/everyday-monads.html' title='Everyday monads'/><author><name>Eric</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://farm3.static.flickr.com/2203/2118752971_7becaf7476_t.jpg'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5336273.post-6799443751045266049</id><published>2008-06-09T10:48:00.004+09:00</published><updated>2008-06-09T11:58:04.865+09:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='algorithm'/><category scheme='http://www.blogger.com/atom/ns#' term='specs'/><category scheme='http://www.blogger.com/atom/ns#' term='scala'/><title type='text'>Edit distance in Scala</title><content type='html'>&lt;div xmlns='http://www.w3.org/1999/xhtml'&gt;How handy.&lt;br /&gt;&lt;br /&gt;I was just looking for a way to highlight differences between 2 strings for my &lt;a href='http://code.google.com/p/specs'&gt;Behavior-Driven Development library,&lt;b&gt; specs&lt;/b&gt;&lt;/a&gt;. And reading the excellent &lt;a href='http://www.amazon.com/Algorithm-Design-Manual-Steve-Skiena/dp/0387948600'&gt;Algorithm Design Manual&lt;/a&gt;. Intuitively, I was thinking that there was a better way to show string differences than the one used in jUnit:&lt;br /&gt;&lt;br /&gt;&lt;code class='prettyprint'&gt;Expected kit&amp;lt;t&amp;gt;en but was kit&amp;lt;ch&amp;gt;en&lt;/code&gt;&lt;br /&gt;&lt;code class='prettyprint'&gt;Expected &amp;lt;skate&amp;gt; but was &amp;lt;kite&amp;gt;&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;The first message shows that &amp;lt;t&amp;gt; has been replaced by &amp;lt;ch&amp;gt;, while the second message says that everything is different! There must be a way to find the minimal set of operations necessary to transform one string to another, right?&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;i&gt;The Levenshtein distance&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;There is indeed. The "Edit" distance, also called &lt;a href='http://en.wikipedia.org/wiki/Levenshtein_distance'&gt;"Levenshtein" distance&lt;/a&gt;, computes exactly this, the minimal number of insertions, deletions and substitutions required to transform "World" into "Peace" (5, they're very far apart,...).&lt;br /&gt;&lt;br /&gt;The computation is usually done by:&lt;br /&gt;&lt;ol&gt;&lt;li&gt;examining the first 2 or last 2 letters of each string&lt;/li&gt;&lt;li&gt;assuming the eventual transformation which may occur for those 2 letters: &lt;/li&gt;&lt;/ol&gt;&lt;ul&gt;&lt;li&gt;they're equal:                             &lt;font face='Courier New'&gt;"h..." and "h...."  --&gt; distance = 0&lt;/font&gt;&lt;br /&gt;  &lt;/li&gt;&lt;li&gt;the first one has been removed:    &lt;font face='Courier New'&gt;"ah..." and "h..."  --&gt; &lt;/font&gt;&lt;font face='Courier New'&gt;distance &lt;/font&gt;&lt;font face='Courier New'&gt;= 1&lt;/font&gt;  &lt;/li&gt;&lt;li&gt;the second one has been inserted: &lt;font face='Courier New'&gt;"h..." and "ah..."  --&gt; &lt;/font&gt;&lt;font face='Courier New'&gt;distance &lt;/font&gt;&lt;font face='Courier New'&gt;= 1&lt;/font&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;they've been substituted:             &lt;font face='Courier New'&gt;"ha..." and "ga..." --&gt; &lt;/font&gt;&lt;font face='Courier New'&gt;distance &lt;/font&gt;&lt;font face='Courier New'&gt;= 1&lt;/font&gt;&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;li&gt;  3. saying that the final distance is the minimum of the distance when choosing one the&lt;br /&gt;&lt;/li&gt;&lt;li&gt;      4 possibilities + the distance resulting from the consequences of that choice&lt;br /&gt;&lt;/li&gt;&lt;br /&gt;You can notice that many variations are possible: the distance could be different depending on the letters being added or removed, there could be more allowed operations such as the swapping of 2 letters (distance 1 instead of 2 substitutions of distance 2),...&lt;br /&gt;&lt;br /&gt;Then the final result can either be constructed incrementally or recursively. Incrementally, for "skate" and "kite" you will compute costs from any small prefix to another prefix:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;"s" to "k"   --&gt; distance 1&lt;br /&gt;&lt;/li&gt;&lt;li&gt;"s" to "ki"   --&gt; distance 2&lt;br /&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;"sk" to "ki" --&gt; distance 2&lt;/li&gt;&lt;li&gt;"ska" to "kit" --&gt; distance 3 &lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;Then you increase the prefix size and you reuse the distance numbers found for smaller prefixes. The different results can be stored in the following matrix:&lt;br /&gt;&lt;br /&gt;&lt;font face='Courier New'&gt;   s k a t e&lt;br /&gt;&lt;br /&gt;k  1 1 2 3 4&lt;br /&gt;i  2 2 2 3 4&lt;br /&gt;t  3 3 3 2 3&lt;br /&gt;e  4 4 4 4 2&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;Recursively, you do the inverse and you establish that the distance between 2 strings can be computed from knowing the distance between smaller prefixes and you travel the matrix to its upper left corner.&lt;br /&gt;&lt;br /&gt;This is a very good example of &lt;a href='http://en.wikipedia.org/wiki/Dynamic_programming'&gt;Dynamic Programming&lt;/a&gt;, where the optimal quantity (the distance at [i, j] in the matrix) you're looking for only depends on:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;the optimal quantity for a subset of the data (the distance at [i-1, j-1], [i-1, j], [i, j-1])&lt;br /&gt;&lt;/li&gt;&lt;li&gt;the resulting state (the substrings defined by a position [i-x, j-x] in the matrix)&lt;/li&gt;&lt;li&gt;&lt;b&gt;not&lt;/b&gt; how you got to that resulting state&lt;/li&gt;&lt;/ul&gt;&lt;i&gt;&lt;b&gt;The EditDistance trait&lt;/b&gt;&lt;/i&gt;&lt;br /&gt;&lt;br /&gt;For the record, I will write here the code I'm using for the implementation, which is constructing the solution incrementally, as opposed to the recursive solution presented on Tony's blog, &lt;a href='http://blog.tmorris.net/finding-the-levenshtein-distance-in-scala/'&gt;here&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;The few interesting things to notice about this code are:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;u&gt;the algorithm itself&lt;/u&gt; ("initializing the matrix"), which is very straightforward&lt;/li&gt;&lt;li&gt;&lt;u&gt;the minimum function&lt;/u&gt; which is a bit strange because it is not comparing all alternatives. The cost for an insertion is only computed if the cost for a suppression is not better than the cost for a substitution. I would like to work out a formal proof on why this is correct by my intuition tells me that a substitution is generally a "good" operation. It has the same cost as an insertion or a suppression but processes 2 letters at the time. So if we find a better cost using a suppression, it is going to be the best&lt;br /&gt;&lt;/li&gt;&lt;li&gt;&lt;u&gt;the matrix traversal&lt;/u&gt; to retrieve one of the possible paths showing the letters which needs to be transformed for each string: not so fun code with a lot of corner cases&lt;/li&gt;&lt;li&gt;&lt;u&gt;the "separators" feature&lt;/u&gt; which allows to select the separators of your choice to display the string differences&lt;/li&gt;&lt;/ul&gt;&lt;code class='prettyprint'&gt;&lt;br /&gt;trait EditDistance {&lt;br /&gt;  /**&lt;br /&gt;   * Class encapsulating the functions related to the edit distance of 2 strings&lt;br /&gt;   */&lt;br /&gt;  case class EditMatrix(s1: String, s2: String) {&lt;br /&gt;    /* matrix containing the edit distance for any prefix of s1 and s2: &lt;br /&gt;       matrix(i)(j) = edit distance(s1[0..i], s[0..j])*/&lt;br /&gt;    val matrix = new Array[Array[int]](s1.length + 1, s2.length + 1)&lt;br /&gt;&lt;br /&gt;    /* initializing the matrix */&lt;br /&gt;    for (i &lt;- 0 to s1.length;&lt;br /&gt;         j &lt;- 0 to s2.length) {&lt;br /&gt;      if (i == 0) matrix(i)(j) = j // j insertions&lt;br /&gt;      else if (j == 0) matrix(i)(j) = i  // i suppressions&lt;br /&gt;      else matrix(i)(j) = min(matrix(i - 1)(j) + 1, // suppression&lt;br /&gt;                              matrix(i - 1)(j - 1) + (if (s1(i - 1) == s2(j - 1)) 0 &lt;br /&gt;                                                           else 1), // substitution&lt;br /&gt;                              matrix(i)(j - 1) + 1) // insertion&lt;br /&gt;   &lt;br /&gt;    }&lt;br /&gt;    /** @return the edit distance between 2 strings */&lt;br /&gt;    def distance = matrix(s1.length)(s2.length)&lt;br /&gt;&lt;br /&gt;    /** prints the edit matrix of 2 strings */&lt;br /&gt;    def print = {&lt;br /&gt;      for (i &lt;- 0 to s1.length) {&lt;br /&gt;        def row = for (j &lt;- 0 to s2.length) yield matrix(i)(j)&lt;br /&gt;        println(row.mkString("|"))&lt;br /&gt;      }&lt;br /&gt;      this&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    /** @return a (String, String) displaying the differences between each &lt;br /&gt;         input strings. The used separators are parenthesis: '(' and ')'*/&lt;br /&gt;    def showDistance: (String, String) = showDistance("()")&lt;br /&gt;&lt;br /&gt;    /**&lt;br /&gt;     * @param sep separators used to hightlight differences. If sep is empty,&lt;br /&gt;     * then no separator is used. If sep contains one character, it is &lt;br /&gt;     * taken as the unique separator. If sep contains 2 or more characters, &lt;br /&gt;     * the first 2 characters are taken as opening separator and closing &lt;br /&gt;     * separator.&lt;br /&gt;     *&lt;br /&gt;     * @return a (String, String) displaying the differences between each &lt;br /&gt;     *  input strings. The used separators are specified by the caller.&lt;br /&gt;     */&lt;br /&gt;    def showDistance(sep: String) = {&lt;br /&gt;      val (firstSeparator, secondSeparator) = separators(sep)&lt;br /&gt;      def modify(s: String, c: Char): String = modifyString(s, c.toString)&lt;br /&gt;      def modifyString(s: String, mod: String): String = { &lt;br /&gt;         (firstSeparator + mod + secondSeparator + s).&lt;br /&gt;         removeAll(secondSeparator + firstSeparator)&lt;br /&gt;       }&lt;br /&gt;      def findOperations(dist: Int, i: Int, j:Int, s1mod: String, s2mod: String): (String, String) = {&lt;br /&gt;        if (i == 0 &amp;&amp; j == 0) {&lt;br /&gt;            ("", "")&lt;br /&gt;        }&lt;br /&gt;        else if (i == 1 &amp;&amp; j == 1) {&lt;br /&gt;            if (dist == 0) (s1(0) + s1mod, s2(0) + s2mod)&lt;br /&gt;          else (modify(s1mod, s1(0)), modify(s2mod, s2(0)))&lt;br /&gt;        }&lt;br /&gt;        else if (j &lt; 1) (modifyString(s1mod, s1.slice(0, i)), s2mod)&lt;br /&gt;        else if (i &lt; 1) (s1mod, modifyString(s2mod, s2.slice(0, j)))&lt;br /&gt;        else {&lt;br /&gt;          val (suppr, subst, ins) = (matrix(i - 1)(j), matrix(i - 1)(j - 1), &lt;br /&gt;                                           matrix(i)(j - 1))  &lt;br /&gt;          if (suppr &lt; subst)&lt;br /&gt;            findOperations(suppr, i - 1, j, modify(s1mod, s1(i - 1)), s2mod)&lt;br /&gt;          else if (ins &lt; subst)&lt;br /&gt;            findOperations(ins, i, j - 1, s1mod, modify(s2mod, s2(j - 1)))&lt;br /&gt;          else if (subst &lt; dist)&lt;br /&gt;            findOperations(subst, i - 1, j - 1, modify(s1mod, s1(i - 1)), &lt;br /&gt;                                           modify(s2mod, s2(j - 1)))&lt;br /&gt;          else&lt;br /&gt;            findOperations(subst, i - 1, j - 1, s1(i - 1) + s1mod, s2(j - 1) + s2mod)&lt;br /&gt;        }&lt;br /&gt;      }&lt;br /&gt;      findOperations(distance, s1.length, s2.length, "", "")&lt;br /&gt;    }&lt;br /&gt;    def min(suppr: Int, subst: Int, ins: =&gt;Int) = {&lt;br /&gt;      if(suppr &lt; subst) suppr&lt;br /&gt;      else if (ins &lt; subst) ins&lt;br /&gt;      else subst&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;  def editDistance(s1: String, s2: String): Int = EditMatrix(s1, s2).distance&lt;br /&gt;  def showMatrix(s1: String, s2: String) = EditMatrix(s1, s2).print&lt;br /&gt;  def showDistance(s1: String, s2: String) = EditMatrix(s1, s2).showDistance&lt;br /&gt;  def showDistance(s1: String, s2: String, sep: String) = EditMatrix(s1, s2).showDistance(sep)&lt;br /&gt;&lt;br /&gt;  private def separators(s: String) = (firstSeparator(s), secondSeparator(s))&lt;br /&gt;  private def firstSeparator(s: String) = if (s.isEmpty) "" else s(0).toString&lt;br /&gt;  private def secondSeparator(s: String) = {&lt;br /&gt;    if (s.size &lt; 2) firstSeparator(s)  else s(1).toString&lt;br /&gt;  }&lt;br /&gt;}&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;i&gt;&lt;b&gt;Conclusion&lt;/b&gt;&lt;/i&gt;&lt;br /&gt;&lt;br /&gt;What's the conclusion? Computer science &lt;b&gt;is&lt;/b&gt; useful of course, but you only recognize it once you know it!&lt;br /&gt;&lt;p/&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5336273-6799443751045266049?l=etorreborre.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://etorreborre.blogspot.com/feeds/6799443751045266049/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5336273&amp;postID=6799443751045266049' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5336273/posts/default/6799443751045266049'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5336273/posts/default/6799443751045266049'/><link rel='alternate' type='text/html' href='http://etorreborre.blogspot.com/2008/06/edit-distance-in-scala_245.html' title='Edit distance in Scala'/><author><name>Eric</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://farm3.static.flickr.com/2203/2118752971_7becaf7476_t.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5336273.post-4631978447366675437</id><published>2008-05-08T21:58:00.007+09:00</published><updated>2008-05-09T00:04:30.048+09:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='algorithm'/><category scheme='http://www.blogger.com/atom/ns#' term='scala'/><title type='text'>Algorithmic panic - take 2</title><content type='html'>Shame, shame, shame.&lt;br /&gt;&lt;br /&gt;&lt;i&gt;&lt;b&gt;The Blog Writing Rule&lt;/b&gt;&lt;/i&gt;&lt;br /&gt;&lt;br /&gt;Writing at least one blog entry per month. That should be feasible, no? Unfortunately, last Friday, I was already more than 10 days late, so I &lt;u&gt;had&lt;/u&gt; to do something!&lt;br /&gt;&lt;br /&gt;And, when pressing the "Post" button, I had this awkward feeling that something was not really right with my "solution". Not really on the "speed" side because I knew that my list accesses were sloppy, but even on the "correctness" side. But I was past the date, damn it!&lt;br /&gt;&lt;br /&gt;&lt;i&gt;&lt;b&gt;Fortunately, someone's watching&lt;/b&gt;&lt;/i&gt;&lt;br /&gt;&lt;br /&gt;Ok, enough justifications. I was in fact very pleased to get so many insightful comments, be it on my blog, or on reddit. It's a pleasure to see that so many people care for &lt;a href="http://xkcd.com/386/"&gt;The Truth&lt;/a&gt; on the Internet ( ;-) )&lt;br /&gt;&lt;br /&gt;So I tried my best to review things up and to derive the maximum number of lessons out of theses errors. I'll start with that, then I'll copy the revised version of the algorithm.&lt;br /&gt;&lt;br /&gt;&lt;i&gt;&lt;b&gt;Lessons learned&lt;/b&gt;&lt;/i&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;&lt;b&gt;Check your definitions&lt;/b&gt;. The median definition is very straightforward,... if you have an odd number of elements. For an even number, you can elaborate several definitions, such as &lt;a href="http://economicsbulletin.vanderbilt.edu/2004/volume3/EB-04C10011A.pdf"&gt;that one&lt;/a&gt;. For the code below, I choose to take define the median as a pair of values, which is both "middle" elements if the list is even. Pairs of values are then conveyed across all computations, so that it's always possible to eventually take the mean value if you feel like it&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;b&gt;Don't trust test generation&lt;/b&gt;. I promise my tests were green when I posted! However, the generation parameters were very bad: the number of tests was too small, the number and range of elements in the lists also. The solution here was tested on larger samples (up to 8000 tests), with larger lists (of same size or different sizes)&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;b&gt;Check your invariants&lt;/b&gt;. In this kind of "Divide and Conquer" approach, I should make sure that I have indeed the same problem, on a smaller scale, after dividing it. The code below shows that it is possible to take the same number of elements on each list, so that the median of the resulting lists is unchanged&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;b&gt;Know your data structures&lt;/b&gt;. I knew that the most basic list was not necessarily efficient but I must say that I didn't check anything. And, in fact, when Jorge pointed out that List had an O(n) access for size, I was quite surprised. The other interesting thing is that optimizing an operation can be totally feasible but quite tricky to implement (see the medianOfSortedListAndOneElement function below). And of course, takeWhile(_) was pretty bad!&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;i&gt;&lt;b&gt;The algorithm&lt;/b&gt;&lt;/i&gt;&lt;br /&gt;&lt;br /&gt;First of all, lets start by the definition of the median as a couple of values, which may be the same if the list has an odd number of elements:&lt;br /&gt;&lt;code class="prettyprint"&gt;&lt;br /&gt;  case class ExtendedSeq(list: Seq[Int]) {&lt;br /&gt;    def median: (Int, Int) = {&lt;br /&gt;      if (list.isOdd)&lt;br /&gt;        (list(list.size / 2), list(list.size / 2))&lt;br /&gt;      else&lt;br /&gt;        (list(list.size / 2 - 1), list(list.size / 2))&lt;br /&gt;    }&lt;br /&gt;    def isOdd = list.size % 2 != 0&lt;br /&gt;  }&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;If elements access and size are O(1) as in the RandomAccessSeq collection, that should be ok in terms of complexity (thanks Jorge).&lt;br /&gt;&lt;br /&gt;Here is the heart of the algorithm:&lt;br /&gt;&lt;code class="prettyprint"&gt;&lt;br /&gt;  def median(l1: Seq[Int], l2:Seq[Int]): (Int, Int) = {&lt;br /&gt;    if (l1.isEmpty) l2.median&lt;br /&gt;    else if (l2.isEmpty) l1.median&lt;br /&gt;    else if (l1.size == 1) medianOfSortedListPlusOneElement(l2, l1(0))&lt;br /&gt;    else if (l2.size == 1) medianOfSortedListPlusOneElement(l1, l2(0))&lt;br /&gt;    else {&lt;br /&gt;      val (m1, m2) = (l1.median, l2.median)&lt;br /&gt;      m1.intersection(m2) match {&lt;br /&gt;        case Some(m) =&gt; m&lt;br /&gt;        case None =&gt; if (m1 &lt; m2) median(trimLists(l1, l2)) &lt;br /&gt;                     else median(trimLists(l2, l1))&lt;br /&gt;      }&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;The code above starts by checking some special cases: one empty list, a list + one element, then 2 "regular" median values. &lt;br /&gt;&lt;br /&gt;The first observation is that, if the median values (which are pairs of values) have a non-empty intersection, this intersection will be the median of both sorted lists. &lt;br /&gt;&lt;br /&gt;This is the not-so readable code for checking if there is an overlap between 2 pairs:&lt;br /&gt;&lt;code class="prettyprint"&gt;&lt;br /&gt;case class ExtendedPair(pair1: (Int, Int)) {&lt;br /&gt;  def intersection(pair2: (Int, Int)) = {&lt;br /&gt;    if (pair2._1 &lt;= pair1._1 &amp;&amp; pair1._2 &lt;= pair2._2) Some(pair1)&lt;br /&gt;    else if (pair1._1 &lt;= pair2._1 &amp;&amp; pair2._2 &lt;= pair1._2) Some(pair2)&lt;br /&gt;    else if (pair1._1 &lt;= pair2._1 &amp;&amp; pair2._1 &lt;= pair1._2 &amp;&amp; pair1._2 &lt;= pair2._2)&lt;br /&gt;      Some((pair2._1, pair1._2))&lt;br /&gt;    else if (pair2._1 &lt;= pair1._1 &amp;&amp; pair1._1 &lt;= pair2._2 &amp;&amp; pair2._2 &lt;= pair1._2) &lt;br /&gt;      Some((pair1._1, pair2._2))&lt;br /&gt;    else None&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Finally, if there is no overlap, we need to remove elements from both lists so that the median of the remaining 2 lists will be the same as the median of the original lists:&lt;br /&gt;&lt;code class="prettyprint"&gt;&lt;br /&gt;def trimLists(l1: Seq[int], l2: Seq[Int]) = {&lt;br /&gt;  val removableElements = if (l1.size &lt; l2.size) l1.size / 2 else l2.size / 2 &lt;br /&gt;  (l1.drop(removableElements), l2.take(l2.size - removableElements))&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;This time, I removed the same number of elements below the lower median and above the higher one (but still, a written mathematical proof would be more satisfying than "it's obvious that,...").&lt;br /&gt;&lt;br /&gt;On the complexity side, I still have to check that I can find a data structure (a Seq) providing drop and take operations in O(1). From what I read of the implementation of RandomAccessSeq, that should be the case. RandomAccessSeq is using indexes to provide O(1) "slicing" operations as suggested by Mikko in the comments of the previous post.&lt;br /&gt;&lt;br /&gt;&lt;i&gt;&lt;b&gt;Brutal force&lt;/b&gt;&lt;/i&gt;&lt;br /&gt;&lt;br /&gt;And finally, I can't resist showing you the horror of finding the median of a sorted list and a supplementary element in O(1). There may be a subtle trick to do that (please say, if you find it!), but I used the brutal force:&lt;br /&gt;&lt;code class="prettyprint"&gt;&lt;br /&gt;def medianOfSortedListPlusOneElement(list: Seq[Int], element: Int) = {&lt;br /&gt;  if (list.size == 1){&lt;br /&gt;    if (element &gt;= list(0))&lt;br /&gt;      (list(0), element)&lt;br /&gt;    else&lt;br /&gt;      (element, list(0))&lt;br /&gt;  } &lt;br /&gt;  else if (list.isOdd) {&lt;br /&gt;    if (element &gt;= list(list.size / 2)) {&lt;br /&gt;      if (element &lt;= list(list.size / 2 + 1))&lt;br /&gt;        (list(list.size / 2), element)&lt;br /&gt;      else&lt;br /&gt;        (list(list.size / 2), list(list.size / 2 + 1))&lt;br /&gt;    } else {&lt;br /&gt;      if (element &gt;= list(list.size / 2 - 1))&lt;br /&gt;        (element, list(list.size / 2))&lt;br /&gt;      else&lt;br /&gt;        (list(list.size / 2 - 1), list(list.size / 2))&lt;br /&gt;    }&lt;br /&gt;  } else {&lt;br /&gt;    if (element &gt;= list(list.size / 2 - 1)) {&lt;br /&gt;      if (element &lt;= list(list.size / 2))&lt;br /&gt;        (element, element)&lt;br /&gt;      else&lt;br /&gt;        (list(list.size / 2), list(list.size / 2))&lt;br /&gt;    } else {&lt;br /&gt;        (list(list.size / 2 - 1), list(list.size / 2 - 1))&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Not pretty, uh? Like the final approach for the 4-colors theorem.&lt;br /&gt;&lt;br /&gt;&lt;i&gt;&lt;b&gt;Measuring the complexity&lt;/b&gt;&lt;/i&gt;&lt;br /&gt;&lt;br /&gt;On my way to this post, I wanted to implement something which would store the test data and say if the curve looks like an O(log(n)), an O(nlog(n)),... In fact it doesn't seem so easy to do without any human validation and tweaking of the test data size until the complexity really shows. I'm thinking that using an utility to graph the performances and compare it with known complexities would be a good first step.&lt;br /&gt;&lt;br /&gt;But maybe driving the test generation so as to get a good confidence on the possible complexity of the algorithm would be even better. I'll try to have a look at that,... Maybe for my next post?&lt;br /&gt;&lt;br /&gt;Thanks for all the comments, I hope I didn't add more and more silliness to my previous errors.&lt;br /&gt;&lt;br /&gt;PS: I liked a lot Tom's comments about the 2-keys indexing problem. Indeed, the problem was underspecified, in terms of access frequency, memory, etc,... And a log(n) solution would certainly be ok in my specific case.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5336273-4631978447366675437?l=etorreborre.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://etorreborre.blogspot.com/feeds/4631978447366675437/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5336273&amp;postID=4631978447366675437' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5336273/posts/default/4631978447366675437'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5336273/posts/default/4631978447366675437'/><link rel='alternate' type='text/html' href='http://etorreborre.blogspot.com/2008/05/algorithmic-panic-take-2.html' title='Algorithmic panic - take 2'/><author><name>Eric</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://farm3.static.flickr.com/2203/2118752971_7becaf7476_t.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5336273.post-7696797854822487004</id><published>2008-05-02T22:23:00.005+09:00</published><updated>2008-05-03T00:35:26.304+09:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='scala'/><title type='text'>Algorithmic panic</title><content type='html'>Last week I read a blog post about an interview for Google and I thought: "Oh my god, I don't know how to do that, I'm lost, I have no idea, I will never find the solution".&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;Flashback&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Two weeks ago, I've ordered and started reading the excellent book, "&lt;a href="http://www.amazon.com/gp/product/0387948600"&gt;The Algorithm Design Manual&lt;/a&gt;". That book is very good. Teaching algorithms and data structures can be something very dry when you consider it from a very scholar perspective. But it becomes really fun and challenging when you realize that:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;it solves real worthy problems&lt;/li&gt;&lt;li&gt;no amount of processing power can be overcome a clever algorithm when there is one&lt;/li&gt;&lt;li&gt;there's often no "right" solution but a combination of different approaches with different trade-offs&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;So the book is filled with "war stories" showing the author investigating some problems, trying to ask relevant questions until there's a "ah-ah" moment where the problem is sufficiently characterized. Then the solution is refined to get a completely satisfying result.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;Decode this!&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;For example, the author and his team had to provide a new way to decrypt DNA fragments, knowing that there was a new technique using small probes returning small fragments of the whole string. Say your DNA is ATGCCTCGATTG, the probes will find AT, TG, GA, TT, TG, all of which are substrings of the initial string. And from all those pieces, you have to find the original DNA string. For this kind of problem, the sheer number of elements kills instantly any brutal-force approach. Originally they had to sequence DNA fragments of 50.000 characters, knowing that more that 1.5 million pieces should be combined to get the answer. Don't try this at home!&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;Binary tree &gt; HashTable &gt; Suffix Tree&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;For their algorithm to work they had to set up a dictionary allowing the search of substrings of length k inside strings of length 2k. The question is: how to do that &lt;span style="font-style: italic;"&gt;very&lt;/span&gt; efficiently? A student proposed a HashTable which would do the job in O(log(k)). But this was still too slow! Hey, what can be faster than a HashTable??!! A &lt;a href="http://en.wikipedia.org/wiki/Suffix_tree"&gt;Suffix Tree&lt;/a&gt;. &lt;br /&gt;&lt;br /&gt;In this specific case they were searching elements which looked very close to one another, just being different by one character. And a suffix tree happens to organize the &lt;span style="font-style: italic;"&gt;suffixes&lt;/span&gt; of a set of strings so that it's easy to search for a string which is just one character away. &lt;br /&gt;&lt;br /&gt;Understanding the structure of the problem yielded much better results than a HashTable: for a 8000 characters long DNA, the HashTable time was 2 days against 650 seconds for the SuffixTree (the "compressed" version, because the original one was blowing up the memory!). &lt;span style="font-style: italic;"&gt;That&lt;/span&gt; was my "ah-ah" moment.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;The dreadful interview question&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;So I thought I was ready for any algorithmic question. Until I read the blog: "find the median of 2 sorted lists in O(log(n))". I don't know why, reading that blog, I imagined myself over the phone, trying to solve this problem and my mind got totally blank. Panic.&lt;br /&gt;&lt;br /&gt;The same night, going to bed, I told myself: ok, relax, have a fresh look at it. And I slept happily a few minutes later because the principle of the solution wasn't that difficult. &lt;br /&gt;&lt;br /&gt;First of all, the median of a sorted list is the middle element. That's an O(1) search! Then, given the medians for each list, the median of both lists is something like the median of the 2 sublists of all elements between median1 and median2. That's where we get our O(log(n)) from, because with a recursive search, we're pretty sure to cut the problem size by 2 at each step.&lt;br /&gt;&lt;br /&gt;I programmed the rest the day after. Again, I think that this is a tribute to Scala. The result is really readable and I used ScalaCheck to verify it by generating lists of same and different sizes. On the other hand, I had hard time figuring out the proper bound conditions (ScalaCheck was very helpful here):&lt;code class="prettyprint"&gt;&lt;br /&gt;&lt;br /&gt;trait Median {&lt;br /&gt;  def median(l1: List[Int], l2:List[Int]): Int = {&lt;br /&gt;    if (l1.isEmpty || l2.isEmpty || l1.last &lt;= l2.head) (l1 ::: l2).median&lt;br /&gt;    else if (l2.last &lt; l1.head) (l2 ::: l1).median&lt;br /&gt;    else {&lt;br /&gt;      val (m1, m2) = (l1.median, l2.median)&lt;br /&gt;      if (m2 &lt; m1) median(l1.takeBetween(m2, m1), l2.takeBetween(m2, m1))&lt;br /&gt;      else if (m2 &gt; m1) median(l1.takeBetween(m1, m2), l2.takeBetween(m1, m2))&lt;br /&gt;      else m1&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;  implicit def toExtendedList(list: List[Int]) = ExtendedList(list)&lt;br /&gt;  case class ExtendedList(list: List[Int]) {&lt;br /&gt;    def takeBetween(a: Int, b: Int) = list.dropWhile(_ &lt;= a).takeWhile(_ &lt;= b) &lt;br /&gt;    def median = list(list.size / 2)&lt;br /&gt;  }&lt;br /&gt;}&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;Data structures everywhere&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Funnily, yesterday, one of my colleagues asked me if I knew an easy, fast and clever way to access some values classified with 2 keys:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;there's only one value for (key1, key2)&lt;/li&gt;&lt;li&gt;she wants to get the value for (key1, key2) (fast,...)&lt;br /&gt;&lt;/li&gt;&lt;li&gt;she wants to get all values for key1 (fast again,...)&lt;br /&gt;&lt;/li&gt;&lt;li&gt;she wants to get all values for key2 (fast also,...)&lt;/li&gt;&lt;/ul&gt;This looks like a very classical problem for Java programmers but surprisingly I found no existing, open-source, solution for this (if you know that, please tell me. I looked at things like TreeMaps or MultiMaps for example but they don't fit the requirement of a fast 2 key search with either of the keys). &lt;br /&gt;&lt;br /&gt;After some research and some thinking we concluded that 3 hashmaps sharing the values and dedicated to answer each different query fast would be the best thing to do. But now that I know that HashTables are not the ultimate solution to everything,...&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5336273-7696797854822487004?l=etorreborre.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://etorreborre.blogspot.com/feeds/7696797854822487004/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5336273&amp;postID=7696797854822487004' title='13 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5336273/posts/default/7696797854822487004'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5336273/posts/default/7696797854822487004'/><link rel='alternate' type='text/html' href='http://etorreborre.blogspot.com/2008/05/algorithmic-panic.html' title='Algorithmic panic'/><author><name>Eric</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://farm3.static.flickr.com/2203/2118752971_7becaf7476_t.jpg'/></author><thr:total>13</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5336273.post-631259757342444568</id><published>2008-03-13T21:58:00.028+09:00</published><updated>2008-03-27T23:16:27.501+09:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='scalacheck'/><category scheme='http://www.blogger.com/atom/ns#' term='specs'/><category scheme='http://www.blogger.com/atom/ns#' term='scala'/><title type='text'>PDD: Properties Driven Development</title><content type='html'>Quick quizz: 1 + 2 =? and 1 + 2 + 3 =? and 1 + 2 + 3 + 4 =?&lt;br /&gt;&lt;br /&gt;In other words, what is the result of the sumN function, which sums all integers from 1 to n?&lt;br /&gt;&lt;br /&gt;If we had to use a "direct" (some would say "naive") TDD approach, we could come with the following code:&lt;br /&gt;[Warning!! The rest of the post assumes that you have some basic knowledge of &lt;a href="http://www.scala-lang.org/"&gt;Scala&lt;/a&gt;, &lt;a href="http://code.google.com/p/specs/"&gt;specs&lt;/a&gt; and &lt;a href="http://code.google.com/p/scalacheck/"&gt;Scalacheck&lt;/a&gt;,... ]&lt;br /&gt;&lt;br /&gt;&lt;code class="prettyprint"&gt;"the sumN function should" {&lt;br /&gt;  def sumN(n: Int) = (1 to n) reduceLeft((a:Int, b:Int) =&gt; a + b)&lt;br /&gt;  "return 1 when summing from 1 to 1" in {&lt;br /&gt;    sumN(1) must_== 1&lt;br /&gt;  }&lt;br /&gt;  "return 2 when summing from 1 to 2" in {&lt;br /&gt;   sumN(2) must_== 3&lt;br /&gt;  }&lt;br /&gt;  "return 6 when summing from 1 to 3" in {&lt;br /&gt;    sumN(3) must_== 6&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;But if we browse our mental book of "mathematical recipes" we remember that&lt;br /&gt;&lt;br /&gt;&lt;code class="prettyprint"&gt;sumN(n) = n * (n + 1) / 2&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;This is actually a much more interesting property to test and Scalacheck helps us in checking that:&lt;code class="prettyprint"&gt;&lt;br /&gt;&lt;br /&gt;"the sumN function should" {&lt;br /&gt;  def sumN(n: Int) = (1 to n) reduceLeft((a:Int, b:Int) =&gt; a + b)&lt;br /&gt;  "return n(n+1)/2 when summing from 1 to n" in {&lt;br /&gt;    val sumNInvariant = (n: Int) =&gt; sumN(n) == n * (n + 1) / 2&lt;br /&gt;    property(sumNInvariant) must pass&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Even better, Scalacheck has no reason to assume that n is strictly positive! So it quickly fails on &lt;code class="prettyprint"&gt;n == -1&lt;/code&gt; and a better implementation is:&lt;code class="prettyprint"&gt;&lt;br /&gt;&lt;br /&gt;"the sumN function should" {&lt;br /&gt;  def sumN(n: Int) = {&lt;br /&gt;     assume(n &gt;= 0) // will throw an IllegalArgumentException if the constraint is violated&lt;br /&gt;     (1 to n) reduceLeft((a:Int, b:Int) =&gt; a + b)&lt;br /&gt;  }&lt;br /&gt;  "return n(n+1)/2 when summing from 1 to n" in {&lt;br /&gt;     val sumNInvariant = (n: Int) =&gt; n &lt;= 0 || sumN(n) == n * (n + 1) / 2&lt;br /&gt;    property(sumNInvariant) must pass          &lt;br /&gt;  }   &lt;br /&gt;}&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;This will be ok and tested for a large number of values for n. Using properties is indeed quite powerful. Recreation time! You can now have a look at &lt;a href="http://www.ebi.ac.uk/%7Ehulme/videos/spj-typedriventestinginhaskell.h264.mp4"&gt;this movie&lt;/a&gt;, where Simon Peyton-Jones shows that Quickcheck (the Haskell ancestor of Scalacheck) detects interesting defaults in a bit packing algorithm.&lt;br /&gt;&lt;br /&gt;Fine, fine, but honestly, all those examples look very academic: graph algorithms, mathematical formulas, bits packing,... Can we apply this kind of approach to our mundane, day-to-day development? Tax accounting, DVD rentals, social websites?&lt;br /&gt;&lt;br /&gt;I am going to take 2 small examples from my daily work and see how PDD could be used [yes, YAA, Yet Another Acronym,... PDD stands for Properties Driven Development (and not &lt;a href="http://en.wikipedia.org/wiki/Pervasive_developmental_disorder"&gt;that PDD&lt;/a&gt;)].&lt;br /&gt;&lt;ol&gt;&lt;li&gt;From the &lt;a href="http://demo.liftweb.net/"&gt;&lt;span style="font-style: italic;"&gt;lift&lt;/span&gt;&lt;/a&gt; framework (courtesy of Jamie Webb): a 'camelCase' function which transforms underscored names to CamelCase names&lt;/li&gt;&lt;li&gt;From my company software: some pricer extension code for Swaps where fees values have to be subtracted from the NPV (Net Present Value) under certain conditions&lt;br /&gt;&lt;/li&gt;&lt;/ol&gt;I'll develop the examples first and try to draw conclusions latter on.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; font-weight: bold;"&gt;Example 1: camelCase&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;So what are the properties we can establish for that first example? Can we describe it informally first?&lt;br /&gt;&lt;br /&gt;The camelCase function should CamelCase a name which is under_scored, removing each underscore and capitalizing the next letter.&lt;br /&gt;&lt;br /&gt;Try this at home, this may not be so easy! Here is my proposal:&lt;code class="prettyprint"&gt;&lt;br /&gt;&lt;br /&gt;def previousCharIsUnderscore(name: String, i: Int) = i &gt; 1 &amp;amp;&amp;amp; name.charAt(i - 1) == '_'&lt;br /&gt;def underscoresNumber(name: String, i: Int) = {&lt;br /&gt;  if (i == 0) 0&lt;br /&gt;  else name.substring(0, i).toList.count(_ == '_')&lt;br /&gt;}&lt;br /&gt;def indexInCamelCased(name: String, i: Int) = i - underscoresNumber(name, i)&lt;br /&gt;def charInCamelCased(n: String, i: Int) = camelCase(n).charAt(indexInCamelCased(n, i))&lt;br /&gt;&lt;br /&gt;val doesntContainUnderscores = property((name: String) =&gt; !camelCase(name).contains('_'))&lt;br /&gt;&lt;br /&gt;val isCamelCased = property ((name: String) =&gt; {&lt;br /&gt;  name.forall(_ == '_') &amp;amp;&amp;amp; camelCase(name).isEmpty ||&lt;br /&gt;  name.toList.zipWithIndex.forall { case (c, i) =&gt;&lt;br /&gt;    c == '_' ||&lt;br /&gt;    indexInCamelCased(name, i) == 0 &amp;amp;&amp;amp; charInCamelCased(name, i) == c.toUpperCase ||&lt;br /&gt;    !previousCharIsUnderscore(name, i) &amp;amp;&amp;amp; charInCamelCased(name, i) == c ||&lt;br /&gt;    previousCharIsUnderscore(name, i) &amp;amp;&amp;amp; charInCamelCased(name, i) == c.toUpperCase&lt;br /&gt;  }&lt;br /&gt;})&lt;br /&gt;doesntContainUnderscores &amp;amp;&amp;amp; isCamelCased must pass&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;This property says that:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;the CamelCased name must not contain underscores anymore&lt;/li&gt;&lt;li&gt;if the name contains only underscores, then the CamelCased name must be empty&lt;/li&gt;&lt;li&gt;for each letter in the original name, either:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;it is an underscore&lt;br /&gt;&lt;/li&gt;&lt;li&gt;it is the first letter after some underscores, then it becomes the first letter of the CamelCased word and should be uppercased&lt;/li&gt;&lt;li&gt;the previous character isn't an underscore, so it should be unchanged&lt;/li&gt;&lt;li&gt;the previous character is an underscore, so the letter should be uppercased&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt;Before running Scalacheck, we also need to create a string generator with some underscores:&lt;code class="prettyprint"&gt;&lt;br /&gt;&lt;br /&gt;implicit def underscoredString: Arbitrary[String] = new Arbitrary[String] {&lt;br /&gt;def arbitrary = for { length &lt;- choose(0, 5)                                                  string &lt;-   vectorOf(length, frequency((4, alphaNumChar),                                                                (1, elements('_'))))                       } yield List.toString(string)  } &lt;/code&gt;&lt;br /&gt;&lt;br /&gt;This works and in the process of working on the properties I observed that:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;the full specification for CamelCasing name is not so easy!&lt;/li&gt;&lt;br /&gt;&lt;li&gt;it is not trivial to relate the resulting name to its original. I had to play with indices and the number of underscores to be able to relate characters before and after. However, if that code is in place, the testing code is almost only 1 line per property to check&lt;br /&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;the properties above specify unambiguously the function. I could also have specify weaker properties with less code, by not avoiding to specify that some letters should be unchanged or that the CamelCased name contains an uppercased letter without checking its position.&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;span style="font-style: italic; font-weight: bold;"&gt;Example 2: Pricer extension&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The logic for this extension is:&lt;br /&gt;&lt;ol&gt;&lt;li&gt;to have the NPV (NetPresentValue) being calculated by the Parent pricer&lt;/li&gt;&lt;li&gt;to collect all fees labeled "UNDERLYING PREMIUM" for that trade&lt;/li&gt;&lt;li&gt;to subtract the fee value from the NPV if the valuation date for the trade is &gt;= the fee settlement date&lt;/li&gt;&lt;li&gt;to apply step 3 only if a pricing parameter named "INCLUDE_FEES "is set to true, while another pricing parameter "NPV_INCLUDE_CASH" is set to false&lt;/li&gt;&lt;/ol&gt;This looks certainly like a lot of jargon for most of you but I guess that it is pretty close to a lot of "Business" requirements. What would a property for those requirements look like (in pseudo Scala code)?&lt;code class="prettyprint"&gt;&lt;br /&gt;&lt;br /&gt;(originalNPV, fees, valuation date, pricing parameters) =&gt;&lt;br /&gt;&lt;br /&gt;if (!INCLUDE_FEES ||  NPV_INCLUDE_CASH)&lt;br /&gt;  newNPV == originalNPV&lt;br /&gt;else&lt;br /&gt;  newNPV == originalNPV - fees.reduceLeft(0) { (fee, result) =&gt; result +&lt;br /&gt;              if (fee.isUnderlyingPremium &amp;amp;&amp;amp; fee.settlementDate &lt;= valuationDate)      &lt;br /&gt;                fee.getValue    &lt;br /&gt;              else      &lt;br /&gt;                0&lt;br /&gt;            }&lt;/code&gt;&lt;br /&gt;The most remarkable thing about this property is that it looks very close to the actual implementation. On the other hand, Scalacheck will be able to generate a lot of test cases: &lt;ul&gt;&lt;li&gt;an empty fee list&lt;/li&gt;&lt;li&gt;a list with no underlying premium fee&lt;/li&gt;&lt;li&gt;a list with a fee which settle date is superior to the valuation date&lt;br /&gt;&lt;/li&gt;&lt;li&gt;a list with a fees which settle date is inferior to the valuation date&lt;/li&gt;&lt;li&gt;the 4 possible combinations for the values of the pricing parameters&lt;/li&gt;&lt;/ul&gt;You can also notice that I use as the first parameter the originalNPV, which doesn't directly come from a generator but which would be the result of the original pricer with the other generated parameters (fees, valuation date, pricing parameters).&lt;br /&gt;&lt;span style="font-style: italic; font-weight: bold;"&gt;&lt;br /&gt;&lt;br /&gt;Conclusion&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;As a conclusion, and at the light of the 2 previous examples, I would like to enumerate the result of my recent experiments with Properties-Driven-Development:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;First of all is that &lt;span style="font-style: italic;"&gt;PDD is TDD&lt;/span&gt;, on steroids. In PDD, we also have data and assertions but data are generated and assertions are more general.&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;I don't believe that this replaces traditional TDD in all situations. There are situations where generating even 4 or 5 cases manually is easier and faster. Especially when we consider that making an exact oracle (the savant word for verification of test expectation) is sometimes tedious as in the camelCase function. In that situation developping the cases manually using the == method would have been much faster&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;PDD on the other hand allow the specify very clearly what is the rule. This is something that you would have to infer reading several examples when using TDD&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;On the other hand having several examples also facilitate the understanding of what's going on. "foo_bar" becomes "FooBar" is more easy to catch than "if a letter is preceded by,..."&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;PDD is very good at generating data you wouldn't think of: empty list, negative numbers, a string with underscores only,...&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;A tip: sometimes, it is useful to include in the generated parameters the result returned by the function you want to test. For example, in the second example, my parameters could be: (originalNPV, newNPV, fees, valuation date, pricing parameters). That way, when Scalacheck reports an error, it also reports the actual value you got when showing a counter-example&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;Sometimes the properties you want to check will almost mimic the implementation (as in example 2). I think that this is may be very often the case with business code if written properly or that this may show that your code is missing a key abstraction&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;It really gets some time to get your head wrap around finding properties. And soon you'll start thinking things like: "I know that property A, B and C characterize my function, but are they sufficient?" and you realize that you coming close to Programming == Theorem Proving&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;As a summary, PDD is not the long awaited &lt;a href="http://en.wikipedia.org/wiki/No_Silver_Bullet"&gt;Silver Bullet&lt;/a&gt; (sorry ;-) ,...) but it is indeed a wonderful tool to have in your toolbox. It will help you test much more thoroughly your programs while seeing them yet another way.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5336273-631259757342444568?l=etorreborre.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://etorreborre.blogspot.com/feeds/631259757342444568/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5336273&amp;postID=631259757342444568' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5336273/posts/default/631259757342444568'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5336273/posts/default/631259757342444568'/><link rel='alternate' type='text/html' href='http://etorreborre.blogspot.com/2008/03/pdd-properties-driven-development.html' title='PDD: Properties Driven Development'/><author><name>Eric</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://farm3.static.flickr.com/2203/2118752971_7becaf7476_t.jpg'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5336273.post-3414120003274311750</id><published>2008-02-25T22:04:00.014+09:00</published><updated>2008-03-01T10:01:49.958+09:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='specs'/><category scheme='http://www.blogger.com/atom/ns#' term='scala'/><category scheme='http://www.blogger.com/atom/ns#' term='jmock'/><title type='text'>Better mocks with jMock (and specs)</title><content type='html'>Now, I don't know how I managed to do without it. Now, I wonder how everyone can do without it. Now, I think that nothing big can be done without it.&lt;br /&gt;&lt;br /&gt;I'm not advertising a brand new toy, but I'm talking about &lt;a href="http://en.wikipedia.org/wiki/Mock_object"&gt;mock objects&lt;/a&gt;. Whether they are &lt;a href="http://www.martinfowler.com/articles/mocksArentStubs.html"&gt;real mocks or just stubs&lt;/a&gt;, it is almost impossible to unit test Java components without them. Not that you can't &lt;span style="font-weight: bold;"&gt;test &lt;/span&gt;them but if you really want to isolate a piece of code, mocks show up one way or the other.&lt;br /&gt;&lt;br /&gt;One of the best libraries for mock objects in Java is &lt;a href="http://www.jmock.org/"&gt;jMock&lt;/a&gt;. Yet, Java's verbosity makes it sometimes difficult to understand the intention of the mock expectations. Enter now jMocks with Scala!&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; font-weight: bold;"&gt;Some statistics about my blog&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Let's say I want to write another front-end to publish my posts to Blogger. I have encapsulated all Blogger functionalities in a Scala trait:&lt;br /&gt;&lt;code class="prettyprint"&gt;&lt;br /&gt;trait Blogger {&lt;br /&gt;  def allPosts: List[Post]&lt;br /&gt;  def todayPosts: List[Post]&lt;br /&gt;  def post(p: Post, tags: List[Tag]): Unit&lt;br /&gt;...&lt;br /&gt;}&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Now I want to test a "Statistics" component which will compute some stats about my posts:&lt;br /&gt;&lt;code class="prettyprint"&gt;&lt;br /&gt;object statsSpecification extends Specification with JMocker {&lt;br /&gt;  "a statistics component" should {&lt;br /&gt;    "return the number of posts for today" in {&lt;br /&gt;      val blogger = mock(classOf[Blogger])&lt;br /&gt;      val stats = new Statistics(blogger)&lt;br /&gt;      expect {&lt;br /&gt;        one(blogger).todayPosts will returnValue(List(Post("...")))&lt;br /&gt;      }&lt;br /&gt;      stats.numberOfPostsForToday&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;class Statistics(blogger: Blogger) {&lt;br /&gt;  def numberOfPostsForToday: Int = blogger.todayPosts.size&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;In that short specification we:&lt;br /&gt;&lt;ol&gt;&lt;li&gt;create a mock: &lt;code class="prettyprint"&gt;blogger = mock(classOf[Blogger])&lt;code&gt;. I would have preferred to write &lt;code class="prettyprint"&gt;blogger = mock[Blogger]&lt;code&gt; but there is no way in Scala to create an object from its type only&lt;/code&gt;&lt;/code&gt;&lt;/code&g
