<?xml version="1.0"?>
<rss version="2.0">

<channel>
	<title>Planet Scala</title>
	<link>http://www.drmaciver.com/planetscala/</link>
	<language>en</language>
	<description>Planet Scala - http://www.drmaciver.com/planetscala/</description>

<item>
	<title>Nikolaj Lindberg: Book: Real World Haskell (not much real world so far :)</title>
	<guid>tag:blogger.com,1999:blog-3840687515615686738.post-8058980771109106022</guid>
	<link>http://nikolajlindberg.blogspot.com/2009/01/book-real-world-haskell-no-much-real.html</link>
	<description>I've just started to read &lt;a href=&quot;http://www.realworldhaskell.org/&quot;&gt;Real World Haskell&lt;/a&gt; (the paper book).  It seems like a nice book (except for a few irritating and confusing typos/mistakes at the start of the book).&lt;br /&gt;&lt;br /&gt;However, I've read more than 100 pages so far, and still not a sign of any of the &quot;real world&quot; stuff promised by the title. I still don't know much or anything about IDE:s, how to compile the code, scripting, any practical details on how to structure your code into modules, or anything in that direction. So far, mostly (sometimes rather long-wined) discussions on specific (list) functions. One of the examples, end up in a conclusion that might be paraphrased as &quot;by the way, don't use the function we've discussed the last few pages; in real world settings it doesn't work too well&quot;.&lt;br /&gt;&lt;br /&gt;In the real world, you run into both needles and haystacks , occasionally, but that doesn't help making sense of&lt;pre&gt;isInAny3 needle haystack = any (isInfixOf needle) haystack&lt;/pre&gt;And one more real world example of the kind &lt;code&gt;zip3foobar &quot;quux&quot;&lt;/code&gt; and I may start losing interest... or just start screaming.&lt;br /&gt;&lt;br /&gt;Well, the upcoming chapters have promising names, so I guess I've just have to keep on reading.  And I guess you have to start with the basics. Still, over 100 pages, and mostly foobars so far...&lt;br /&gt;&lt;br /&gt;The book is available &lt;a href=&quot;http://book.realworldhaskell.org/read/&quot;&gt;on-line&lt;/a&gt;.</description>
	<pubDate>Wed, 07 Jan 2009 02:00:18 +0000</pubDate>
	<author>noreply@blogger.com (Nikolaj Lindberg)</author>
</item>
<item>
	<title>Eric Torreborre: Doing my homework</title>
	<guid>tag:blogger.com,1999:blog-5336273.post-2650246717230365328</guid>
	<link>http://etorreborre.blogspot.com/2009/01/doing-my-homework.html</link>
	<description>&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;&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 &quot;mapFilter&quot;. Here is an example of what I expected:&lt;br /&gt;&lt;br /&gt;&lt;code class=&quot;prettyprint&quot;&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 &quot;map&quot; and &quot;filter&quot; to do the trick:&lt;br /&gt;&lt;code class=&quot;prettyprint&quot;&gt;&lt;br /&gt;&lt;/code&gt;&lt;div&gt;&lt;code class=&quot;prettyprint&quot;&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=&quot;prettyprint&quot;&gt;  l.map(f(_)).filter(_.isDefined).map(_.get)&lt;/code&gt;&lt;/div&gt;&lt;div&gt;&lt;code class=&quot;prettyprint&quot;&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=&quot;Apple-style-span&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;&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=&quot;prettyprint&quot;&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=&quot;prettyprint&quot;&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=&quot;prettyprint&quot;&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=&quot;prettyprint&quot;&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=&quot;Apple-style-span&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;&gt;Monads again,...&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;What about the &quot;Monadic&quot; 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=&quot;http://code.google.com/p/scalaz/&quot;&gt;here&lt;/a&gt;.  The package I'm exploring today is the &quot;control&quot; package. &lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;&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=&quot;prettyprint&quot;&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=&quot;prettyprint&quot;&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=&quot;prettyprint&quot;&gt;&lt;br /&gt;&lt;/code&gt;&lt;/div&gt;&lt;div&gt;&lt;code class=&quot;prettyprint&quot;&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=&quot;prettyprint&quot;&gt;&lt;br /&gt;&lt;/code&gt;&lt;/div&gt;&lt;div&gt;&lt;code class=&quot;prettyprint&quot;&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=&quot;Apple-style-span&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;&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&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 &quot;pure&quot; function, put it in a &quot;Container&quot; of type P, called the &quot;environment&quot; here. I guess that the idea is to say that once the value is well protected and controlled in its container, it is &quot;pure&quot;. 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 &quot;pure&quot;:&lt;br /&gt;&lt;br /&gt;&lt;code class=&quot;prettyprint&quot;&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 &quot;a&quot; inside the &quot;Some&quot; container. So if you think of Monads as &quot;Containers&quot; for computation then &quot;pure&quot; is what creates the container with a value inside.&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;Apple-style-span&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;&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=&quot;prettyprint&quot;&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 &quot;environment&quot; again. This &quot;environment&quot; or &quot;Container&quot; 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 &quot;bind&quot; 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=&quot;prettyprint&quot; for=&quot;&quot;&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=&quot;prettyprint&quot;&gt;&lt;br /&gt;val f = x =&gt; Some(x + 2)&lt;/code&gt;&lt;div&gt;&lt;code class=&quot;prettyprint&quot;&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. &quot;flatten&quot; the inner result by removing the inner &quot;box&quot;&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. &quot;bind&quot; for a monad is indeed the &quot;flatMap&quot; 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 &quot;Container&quot; 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 (&quot;I suggest you do not use the available subversion of the List.flatMap type signature from which to learn&quot;...)&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;&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 &quot;bind&quot; operation? If I want to get Some(6) as a result, something like &quot;map&quot; should be enough. True. And this even has a name, this is the &quot;fmap&quot; operation of the Functor trait (also mixed in by Monad):&lt;br /&gt;&lt;code class=&quot;prettyprint&quot;&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 &quot;fmap&quot; 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=&quot;prettyprint&quot;&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=&quot;prettyprint&quot;&gt;&lt;/code&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;But I can compose them using &quot;bind&quot;:&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;code class=&quot;prettyprint&quot;&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=&quot;Apple-style-span&quot;&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=&quot;prettyprint&quot;&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=&quot;prettyprint&quot;&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=&quot;prettyprint&quot;&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 &quot;Applicative&quot; 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=&quot;http://lucdup.blogspot.com/2008/01/tmp_2968.html&quot;&gt;here&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;Apple-style-span&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;&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 &quot;Container&quot; (named &quot;Environment&quot; here):&lt;br /&gt;&lt;code class=&quot;prettyprint&quot;&gt;&lt;br /&gt;&lt;/code&gt;&lt;/div&gt;&lt;code class=&quot;prettyprint&quot;&gt;&lt;/code&gt;&lt;div&gt;&lt;code class=&quot;prettyprint&quot;&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 &quot;append&quot; or &quot;merge&quot; 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=&quot;prettyprint&quot;&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=&quot;Apple-style-span&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;&gt;The real academic mapFilter function ;-)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot;&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=&quot;prettyprint&quot;&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=&quot;prettyprint&quot;&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=&quot;prettyprint&quot;&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=&quot;prettyprint&quot;&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=&quot;prettyprint&quot;&gt;  m1.bind(f, m) &lt;/code&gt;&lt;/div&gt;&lt;div&gt;&lt;code class=&quot;prettyprint&quot;&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;And that's understable with the List example because we're using &quot;flatMap&quot; 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=&quot;Apple-style-span&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;&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=&quot;prettyprint&quot;&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=&quot;prettyprint&quot;&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=&quot;Apple-style-span&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;&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 &quot;bind&quot; 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 &quot;models of programming&quot;: 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;</description>
	<pubDate>Wed, 07 Jan 2009 00:40:47 +0000</pubDate>
	<author>noreply@blogger.com (Eric)</author>
</item>
<item>
	<title>Joakim Ohlrogge: SWT scala screenshot</title>
	<guid>http://johlrogge.wordpress.com/?p=131</guid>
	<link>http://johlrogge.wordpress.com/2009/01/06/making-swt-shine-with-scala/</link>
	<description>&lt;div class=&quot;snap_preview&quot;&gt;&lt;br /&gt;&lt;p&gt;At Agile 2008 I was calmly coding together with &lt;a href=&quot;http://danielbrolund.wordpress.com/&quot; target=&quot;_blank&quot;&gt;Daniel Brolund&lt;/a&gt; in the open space area. We were interrupted by a guy that asked us if we did any coding in Ruby and if we did he will hold a lightning-talk about a framework he had put together to create SWT-gui:s using JRuby. Interestingly enough I had thought about how GUI-coding could probably be improved in a language such as Ruby so I reluctantly stopped what I was doing and watched the lightning talk. This was the first time I ever saw &lt;a href=&quot;http://www.eclipse.org/glimmer/&quot; target=&quot;_blank&quot;&gt;Glimmer&lt;/a&gt; and it totally blew me away!&lt;/p&gt;
&lt;p&gt;&lt;span id=&quot;more-131&quot;&gt;&lt;/span&gt;&lt;/p&gt;
&lt;h3&gt;It must be possible to do in Scala!&lt;/h3&gt;
&lt;p&gt;I had already started playing with Scala and I thought it would be a cool experiment to see if I could do something similar in Scala. After all, scala is a pretty expressive language.&lt;/p&gt;
&lt;p&gt;I made the first attempt a few months ago and I think that qualifies as a failure. I got something to work but all the mutable state and redundant classes I had to create bothered me big time.&lt;/p&gt;
&lt;p&gt;I showed what I did to colleague and I got some pointers on how to make my code more function-oriented and hence get rid of some redundancy and mutable state. But my lacking knowledge of Scala and FP prevented me from finishing the task back then.&lt;/p&gt;
&lt;h3&gt;Success!&lt;/h3&gt;
&lt;p&gt;Yesterday I gave it another shot and it took me only an evening to make this code:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;

        shell(
            text(&amp;quot;User Profile&amp;quot;),
            composite (
                gridLayout(2, false),
                group (
                    text(&amp;quot;Name&amp;quot;),
                    gridLayout(2, false),
                    gridData(FILL, FILL, true, true),
                    label(text(&amp;quot;First&amp;quot;)), edit(text(&amp;quot;Bullet&amp;quot;)),
                    label(text(&amp;quot;Last&amp;quot;)), edit(text(&amp;quot;Tooth&amp;quot;))
                ),
                group (
                    gridData(FILL, FILL, true, true),
                    text(&amp;quot;Gender&amp;quot;),
                    radioButton (text (&amp;quot;Male&amp;quot;), selected),
                    radioButton (text (&amp;quot;Female&amp;quot;))
                ),
                group (
                    gridData(FILL, FILL, true, true),
                    text(&amp;quot;Role&amp;quot;),
                    checkBox (text(&amp;quot;Student&amp;quot;), selected),
                    checkBox (text(&amp;quot;Employee&amp;quot;), selected)
                ),
                group (
                    text(&amp;quot;Experience&amp;quot;),
                    rowLayout,
                    gridData(FILL, FILL, true, true),
                    spinner(selection(5)), label {text (&amp;quot;years&amp;quot;)}
                ),
                button (
                    text(&amp;quot;save&amp;quot;),
                    gridData(RIGHT, CENTER, true, true)
                ),
                button (
                    text(&amp;quot;close&amp;quot;),
                    gridData(LEFT, CENTER, true, true)
                )
           )
        )
&lt;/pre&gt;
&lt;p&gt;Display this:&lt;/p&gt;
&lt;p&gt;&lt;img class=&quot;alignnone size-full wp-image-138&quot; title=&quot;SWT scala screenshot&quot; src=&quot;http://johlrogge.files.wordpress.com/2009/01/scalascreenshot.png?w=341&amp;h=228&quot; alt=&quot;SWT scala screenshot&quot; width=&quot;341&quot; height=&quot;228&quot; /&gt;&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;Pretty cool! I will admit that I have gracefully stolen most of the ideas from Glimmer. Even the example above is a slightly modified Glimmer-example. I will probably go over it a couple of times to see what I can improve from a Scala perspective. I particulary dislike the &lt;em&gt;gridData &lt;/em&gt;functions. I think there is room for improvement there.&lt;/p&gt;
&lt;h3&gt;How does it work?&lt;/h3&gt;
&lt;p&gt;The general pattern is simple. Take a look a the group element in the code above. Group is implemented like this:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;

def group(setups:(Group =&amp;gt; Unit)*)(parent:Composite) = {
    val group = new Group(parent, SWT.NONE)
    rowLayout(group);
    setups.foreach(setup =&amp;gt; setup(group))
}
&lt;/pre&gt;
&lt;p&gt;If you, like me when I saw my first Scala examples, have some dificulty with the double parameterlist notation it is a shorthand in Scala for returning a function. So the above is a function that takes a variable number of functions that take a &lt;em&gt;Group&lt;/em&gt; as the only parameter.&lt;em&gt; Unit &lt;/em&gt;in Scala is equivalent to &lt;em&gt;void&lt;/em&gt; in java. The group-function will return a function that takes a &lt;em&gt;Composite&lt;/em&gt;, parent, as it&amp;#8217;s only parameter.&lt;/p&gt;
&lt;p&gt;The cool thing about this is that you can pass the first set of parameters (the variable number of functions that takes a Group and return Unit) and save the returned function for later evaluation. When the composite &lt;em&gt;parent&lt;/em&gt; is later passed what will happen is the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A new Group widget will be created with &lt;em&gt;parent&lt;/em&gt; as parent&lt;/li&gt;
&lt;li&gt;as a default the layout of the group will be set to &lt;em&gt;row layout&lt;/em&gt;, this may be overridden in the next step.&lt;/li&gt;
&lt;li&gt;Each function in setup will be applied to the newly created group.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Remember, the setup-functions take a group as a parameter so they can perform whatever action on the Group. You have full freedom to do whatever can be done with a group in any of those functions.&lt;/p&gt;
&lt;p&gt;The variable length argument is one of the missing pieces I needed to solve the puzzle, I failed to find information about variable arguments via google. Ok, I did not spend an awful lot of time trying&amp;#8230;&lt;/p&gt;
&lt;h3&gt;What does one of those setup-methods look like then?&lt;/h3&gt;
&lt;p&gt;Let&amp;#8217;s start with something simple, the label:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;

 def label(setups:(Label =&amp;gt; Unit)*)(parent:Composite) = {
    val label = new Label(parent, SWT.NONE)
    setups.foreach(setup =&amp;gt; setup(label))
}
&lt;/pre&gt;
&lt;p&gt;The pattern repeats itself, lazy creation of the label onto the parent composite (for instance a &lt;em&gt;Group&lt;/em&gt;) and the posibility to furter customize the label. Now comes the next secret I had to learn in order to solve the puzzle. Take a look at the text-function:&lt;/p&gt;
&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;

def text[T &amp;lt;: {def setText(txt:String)}](txt:String)(subject:T) = {
    subject.setText(txt)
}
&lt;/pre&gt;
&lt;p&gt;Pretty straight forward right? Look again at the generic type &lt;em&gt;T. &lt;/em&gt;I don&amp;#8217;t know what that construct is called, I call it declarative duck-typing, you know &lt;em&gt;if it walks like a duck and quacks like a duck it&amp;#8217;s a duck!&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Here I declare that T can be any type that has a function named &lt;em&gt;setText&lt;/em&gt; that takes a &lt;em&gt;String&lt;/em&gt; as an argument and returns nothing. If I did not declare this then the &lt;em&gt;subject.setText(txt) &lt;/em&gt;line would not compile. But I&amp;#8217;m getting ahead of myself. Why did I need to find this construct?&lt;/p&gt;
&lt;p&gt;Several different widgets in SWT has a &lt;em&gt;setText-&lt;/em&gt;method, for instance, shell has a setText, label has a setText etc but some controls do not. There is no common interface between all different widgets that has a setText-method. That makes things a bit tricky, my first attempt was to overload different setText-functions, one for label, one for shell etc&amp;#8230; This was not only tedious, it didn&amp;#8217;t work since the difference between all of those overloaded methods is in the parameterlist of the returned function. Luckily, this little inconvenience forced me to discover this NEAT feature of the Scala-type system. I&amp;#8217;m happy I did.&lt;/p&gt;
&lt;h3&gt;In conclusion&lt;/h3&gt;
&lt;p&gt;I was able to make a DSL that is very comparable to glimmer which was my original goal. I was able to make the whole thing initialize lazily. Basically I just build a tree of constructing functions and then I sort of light the fuse by passing a shell to the root of that tree and the GUI is created. I did not have to introduce any mutable state other than what is already mutable in SWT. I have a feeling that this approach is very extendable. Maybe the GUI is a good place to introduce Scala in Java-projects? My feeling is that Scala would slowly eat it&amp;#8217;s way in to other parts of the system from there &lt;img src=&quot;http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif&quot; alt=&quot;:)&quot; class=&quot;wp-smiley&quot; /&gt; &lt;/p&gt;
&lt;p&gt;Perhaps this is a good candidate for an open source project? Would you want to use it?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;a rel=&quot;nofollow&quot; href=&quot;http://feeds.wordpress.com/1.0/gocomments/johlrogge.wordpress.com/131/&quot;&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://feeds.wordpress.com/1.0/comments/johlrogge.wordpress.com/131/&quot; /&gt;&lt;/a&gt; &lt;a rel=&quot;nofollow&quot; href=&quot;http://feeds.wordpress.com/1.0/godelicious/johlrogge.wordpress.com/131/&quot;&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://feeds.wordpress.com/1.0/delicious/johlrogge.wordpress.com/131/&quot; /&gt;&lt;/a&gt; &lt;a rel=&quot;nofollow&quot; href=&quot;http://feeds.wordpress.com/1.0/gostumble/johlrogge.wordpress.com/131/&quot;&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://feeds.wordpress.com/1.0/stumble/johlrogge.wordpress.com/131/&quot; /&gt;&lt;/a&gt; &lt;a rel=&quot;nofollow&quot; href=&quot;http://feeds.wordpress.com/1.0/godigg/johlrogge.wordpress.com/131/&quot;&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://feeds.wordpress.com/1.0/digg/johlrogge.wordpress.com/131/&quot; /&gt;&lt;/a&gt; &lt;a rel=&quot;nofollow&quot; href=&quot;http://feeds.wordpress.com/1.0/goreddit/johlrogge.wordpress.com/131/&quot;&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://feeds.wordpress.com/1.0/reddit/johlrogge.wordpress.com/131/&quot; /&gt;&lt;/a&gt; &lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://stats.wordpress.com/b.gif?host=johlrogge.wordpress.com&amp;blog=708994&amp;post=131&amp;subd=johlrogge&amp;ref=&amp;feed=1&quot; /&gt;&lt;/div&gt;</description>
	<pubDate>Tue, 06 Jan 2009 20:48:48 +0000</pubDate>
</item>
<item>
	<title>Nikolaj Lindberg: Scala for small throw-away scripting tasks</title>
	<guid>tag:blogger.com,1999:blog-3840687515615686738.post-2803042169827784845</guid>
	<link>http://nikolajlindberg.blogspot.com/2008/12/scala-for-small-throw-away-scripting.html</link>
	<description>I've come to use Scala for tiny scripts to be thrown away after doing some small task. Typically this involves processing a few files, comparing some textual data, maybe extracting some fields of tab-separated files, etc. The kind of things that Perl used to be the obvious choice for.&lt;br /&gt;&lt;br /&gt;Although lacking Perl's simplified syntax for iterating over all lines in files, Scala works quite nicely for small tasks.&lt;br /&gt;&lt;br /&gt;For example, today I had to extract from a file all lines of four or more characters including only upper-case characters, and capitalize the output:&lt;pre&gt;scala.io.Source.fromFile(args(0))&lt;br /&gt;.getLines.map(_.stripLineEnd).filter(_.matches(&quot;[A-Z]{4,}&quot;))&lt;br /&gt;.map(_.toLowerCase.capitalize).foreach(println)&lt;/pre&gt;Not exactly a thing of beauty, but it only took a minute and it works. And it reminds me a bit of a classic Unix command line pipeline.&lt;br /&gt;&lt;br /&gt;A few things on my wish-list to make Scala even better for small scripts:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;A nicer way of setting the output character encoding (currently you have to do something like &lt;code&gt;Console.setOut(new java.io.PrintStream(Console.out,true,&quot;UTF8&quot;))&lt;/code&gt;)&lt;/li&gt;&lt;li&gt; It would be great if &lt;code&gt;Source.getLines&lt;/code&gt; could remove the new line character of each line&lt;/li&gt;&lt;li&gt;A better name for &lt;code&gt;RichString.stripLineEnd&lt;/code&gt; (for some reason, it is totally impossible for me to remember the name of this method)&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Maybe scripting support in the Scala Netbeans plugin? (Currently, I think the plugin wants you to put your code in a class/object)&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;</description>
	<pubDate>Tue, 06 Jan 2009 16:51:27 +0000</pubDate>
	<author>noreply@blogger.com (Nikolaj Lindberg)</author>
</item>
<item>
	<title>Ruminations of a Programmer: Higher Order abstractions in Scala with Type Constructor Polymorphism</title>
	<guid>tag:blogger.com,1999:blog-22587889.post-4924208956799352292</guid>
	<link>http://debasishg.blogspot.com/2009/01/higher-order-abstractions-in-scala-with.html</link>
	<description>Abstractions at a higher level through type constructor polymorphism. Good type systems are expressive enough to conceal the implementation complexity, and expose *only* what it matters to the developer. People often cringe about the complexity of Scala's type system and how it serves as a barrier to the entry point in mainstream programming. As Michael Feathers recently &lt;a href=&quot;http://twitter.com/mfeathers/status/1088198974&quot;&gt;noted&lt;/a&gt; on Tweeter, the unfortunate fact is that people often jump at the esoteric parts of a language before looking at the simpler subset, which he will be using 90% of the time. And, I think Scala has that sweet spot, where you need not mess around too much with variances, implicits and existentials and yet come up with a nice, concise and functional codebase. &lt;br /&gt;&lt;br /&gt;In this post, I discuss the already introduced intimidating phrase &quot;Type Constructor Polymorphism&quot; through a series of code snippets ranging from toys to some real-world stuff. The idea is, once again, not to evangelize type theory intricacies, but share some of the experiences of how this feature in Scala's type system can help write idiomatic code, while staying away from the complexities of its underlying implementation.&lt;br /&gt;&lt;br /&gt;Jump on .. &lt;br /&gt;&lt;br /&gt;We have a list of &lt;code&gt;Option[String]&lt;/code&gt; that we need to abstract over and compute some value. Say, for the sake of keeping the example simple, we will calculate the sum of lengths of all the strings present ..&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;span class=&quot;java_keyword&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;employees&lt;/span&gt;&lt;span class=&quot;java_operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Option&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;]]&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Some&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;java_literal&quot;&gt;&amp;quot;dave&amp;quot;&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Some&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;java_literal&quot;&gt;&amp;quot;john&amp;quot;&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Some&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;java_literal&quot;&gt;&amp;quot;sam&amp;quot;&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;java_keyword&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;n&lt;/span&gt;&lt;span class=&quot;java_operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&amp;nbsp;employees&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;map&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;x&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;x&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_keyword&quot;&gt;match&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_keyword&quot;&gt;case&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Some&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;name&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;length&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_keyword&quot;&gt;case&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_literal&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;}.&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;elements&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;reduceLeft&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;](&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;_&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;_&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;java_plain&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Let us take another problem that needs to abstract over a different &lt;code&gt;List&lt;/code&gt; structure, a &lt;code&gt;List&lt;/code&gt; of &lt;code&gt;List&lt;/code&gt; of &lt;code&gt;String&lt;/code&gt;s, and compute the same result as before, i.e. the sum of lengths of all the strings encountered in the collection ..&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;span class=&quot;java_keyword&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;brs&lt;/span&gt;&lt;span class=&quot;java_operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;]]&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;java_literal&quot;&gt;&amp;quot;dave&amp;quot;&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_literal&quot;&gt;&amp;quot;john&amp;quot;&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_literal&quot;&gt;&amp;quot;sam&amp;quot;&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;java_literal&quot;&gt;&amp;quot;peter&amp;quot;&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_literal&quot;&gt;&amp;quot;robin&amp;quot;&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_literal&quot;&gt;&amp;quot;david&amp;quot;&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;java_literal&quot;&gt;&amp;quot;pras&amp;quot;&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_literal&quot;&gt;&amp;quot;srim&amp;quot;&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;java_keyword&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;m&lt;/span&gt;&lt;span class=&quot;java_operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&amp;nbsp;brs&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;flatMap&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;x&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;x&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;map&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;}}&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;elements&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;reduceLeft&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;](&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;_&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;_&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;java_plain&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Do you see any commonality in the solution structure in the above snippets ? After all, the problem space has a common generic structure ..&lt;br /&gt;&lt;ol&gt;&lt;br /&gt;  &lt;li&gt;we have a &lt;code&gt;List&lt;/code&gt; with some &lt;code&gt;String&lt;/code&gt; values abstracted in different forms&lt;/li&gt;&lt;br /&gt;  &lt;li&gt;need to iterate over the list&lt;/li&gt;&lt;br /&gt;  &lt;li&gt;do some stuff with elements in the list and compute an &lt;code&gt;Int&lt;/code&gt; value&lt;/li&gt;&lt;br /&gt;&lt;/ol&gt;&lt;br /&gt;Unfortunately the actual solution structures look quite different and have to deal a lot digging into the abstractions of the underlying representations within the collection itself. And this is because, we cannot abstract over the type constructor (the List in this case) that takes another type constructor as an argument (&lt;code&gt;Option[String]&lt;/code&gt; in the first case and &lt;code&gt;List[String]&lt;/code&gt; in the second case).&lt;br /&gt;&lt;br /&gt;Enter type constructor polymorphism. &lt;br /&gt;&lt;br /&gt;Sounds intimidating ? Maybe, but ask the Haskellers .. they have been using typeclasses ever since so successfully in comprehensions, parser combinators and embedded DSLs and programming at a different level of abstraction.&lt;br /&gt;&lt;br /&gt;Scala supports type constructor polymorphism since 2.5, and the details have been discussed in a recent &lt;a href=&quot;http://www.cs.kuleuven.be/~adriaan/?q=genericshk&quot;&gt;paper&lt;/a&gt; by Adriaan Moors et al in OOPSLA 2008.&lt;br /&gt;&lt;br /&gt;Here is a snippet of the Scala code that works seamlessly for both of the above cases ..&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;span class=&quot;java_keyword&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;l&lt;/span&gt;&lt;span class=&quot;java_operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;employees&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;flatMapTo&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;]{&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;}}&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;java_keyword&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;sum&lt;/span&gt;&lt;span class=&quot;java_operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;l&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;elements&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;reduceLeft&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;](&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;_&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;_&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;java_plain&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;  &lt;br /&gt;The above code abstracts over &lt;code&gt;List&lt;/code&gt; through higher order parametric polymorphism, i.e. independent of whether the List parameter is an &lt;code&gt;Option[]&lt;/code&gt; or another &lt;code&gt;List[]&lt;/code&gt;. Incidentally both of them (&lt;code&gt;List&lt;/code&gt; and &lt;code&gt;Option&lt;/code&gt;) are monads and flatMapTo abstracts a monadic computation, hiding all details of type constructor polymorphism from the developer.&lt;br /&gt;&lt;br /&gt;Now here is some real life example (elided for simplicity) ..&lt;br /&gt;&lt;br /&gt;Here are the simple domain models for &lt;code&gt;Customer&lt;/code&gt;, &lt;code&gt;Instrument&lt;/code&gt; and &lt;code&gt;Trade&lt;/code&gt;, used for modeling a use case where a &lt;code&gt;Customer&lt;/code&gt; can order for the &lt;code&gt;Trade&lt;/code&gt; of an &lt;code&gt;Instrument&lt;/code&gt; in an exchange.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;span class=&quot;java_keyword&quot;&gt;case&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_keyword&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Customer&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;java_operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;name&lt;/span&gt;&lt;span class=&quot;java_operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;java_keyword&quot;&gt;case&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_keyword&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;nomura&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_keyword&quot;&gt;extends&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Customer&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;java_literal&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_literal&quot;&gt;&amp;quot;NOMURA&amp;quot;&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;java_keyword&quot;&gt;case&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_keyword&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;meryll&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_keyword&quot;&gt;extends&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Customer&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;java_literal&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_literal&quot;&gt;&amp;quot;MERYLL&amp;quot;&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;java_keyword&quot;&gt;case&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_keyword&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;lehman&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_keyword&quot;&gt;extends&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Customer&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;java_literal&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_literal&quot;&gt;&amp;quot;LEHMAN&amp;quot;&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;java_keyword&quot;&gt;case&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_keyword&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Instrument&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;java_operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;java_keyword&quot;&gt;case&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_keyword&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;ibm&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_keyword&quot;&gt;extends&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Instrument&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;java_literal&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;java_keyword&quot;&gt;case&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_keyword&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;sun&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_keyword&quot;&gt;extends&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Instrument&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;java_literal&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;java_keyword&quot;&gt;case&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_keyword&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;google&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_keyword&quot;&gt;extends&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Instrument&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;java_literal&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;java_keyword&quot;&gt;case&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_keyword&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Trade&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;ref&lt;/span&gt;&lt;span class=&quot;java_operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;ins&lt;/span&gt;&lt;span class=&quot;java_operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Instrument&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;qty&lt;/span&gt;&lt;span class=&quot;java_operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;price&lt;/span&gt;&lt;span class=&quot;java_operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;And we fetch the following list through a query from the database. It is a &lt;code&gt;List&lt;/code&gt; of tuples where each tuple consists of a &lt;code&gt;Customer&lt;/code&gt; and a trade that has been executed based on the &lt;code&gt;Order&lt;/code&gt; he placed at the exchange. And here is the snippet of the code that computes the sum total of the values of all trades executed in the day for all customers.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;span class=&quot;java_keyword&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;trades&lt;/span&gt;&lt;span class=&quot;java_operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Customer&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Option&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Trade&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;])]&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;nomura&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Some&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Trade&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;java_literal&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;ibm&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_literal&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_literal&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;))),&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;meryll&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;lehman&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Some&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Trade&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;java_literal&quot;&gt;200&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;google&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_literal&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_literal&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;))))&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;java_keyword&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;ts&lt;/span&gt;&lt;span class=&quot;java_operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Option&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Trade&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;]]&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;trades&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;_2&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;java_keyword&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;t&lt;/span&gt;&lt;span class=&quot;java_operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;ts&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;flatMapTo&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;]{&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;x&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;x&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;qty&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;x&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;price&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;}}&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;java_keyword&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;value&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;t&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;elements&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;reduceLeft&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;](&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;_&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;_&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;java_plain&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Really not much different from the above simple cases where we were dealing with toy examples - isn't it ? The structure of the solution is the same irrespective of the complexity of data being stored within the collections. The iteration is being done at a much higher level of abstraction, independent of the types stored within the container. And as I mentioned above, &lt;code&gt;flatMapTo&lt;/code&gt; is the secret sauce in the above solution structure that abstracts the building of the new container hiding the inner details. To get more into the innards of &lt;code&gt;flatMapTo&lt;/code&gt; and similar higher order abstractions, including the new form of &lt;code&gt;Iterable[+T]&lt;/code&gt;, have a look at the OOPSLA paper referenced above.&lt;br /&gt;&lt;br /&gt;&lt;i&gt;&lt;b&gt;Postscript:&lt;/b&gt;&lt;/i&gt; In all the snippets above, I have been explicit about all type signatures, just for the sake of easier understanding of my passionate reader. In reality, almost all of these will be inferred by Scala.</description>
	<pubDate>Mon, 05 Jan 2009 12:25:08 +0000</pubDate>
	<author>ghosh.debasish@gmail.com (Debasish)</author>
</item>
<item>
	<title>Code Commit: Joint Compilation of Scala and Java Sources</title>
	<guid>http://www.codecommit.com/blog/scala/joint-compilation-of-scala-and-java-sources</guid>
	<link>http://www.codecommit.com/blog/scala/joint-compilation-of-scala-and-java-sources</link>
	<description>&lt;p&gt;One of the features that the Groovy people like to flaunt is the joint compilation of &lt;code&gt;.groovy&lt;/code&gt; and &lt;code&gt;.java&lt;/code&gt; files.&amp;nbsp; This is a fantastically powerful concept which (among other things) allows for circular dependencies between Java, Groovy and back again.&amp;nbsp; Thus, you can have a Groovy class which extends a Java class which in turn extends another Groovy class.&lt;/p&gt;
&lt;p&gt;All this is old news, but what you may not know is the fact that Scala is capable of the same thing.&amp;nbsp; The Scala/Java joint compilation mode is new in Scala 2.7.2, but despite the fact that this release has been out for more than two months, there is still a remarkable lack of tutorials and documentation regarding its usage.&amp;nbsp; Hence, this post&amp;#8230;&lt;/p&gt;
&lt;h3&gt;Concepts&lt;/h3&gt;
&lt;p&gt;For starters, you need to know a little bit about how joint compilation works, both in Groovy and in Scala.&amp;nbsp; Our motivating example will be the following stimulating snippet:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;line_numbers&quot;&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre class=&quot;scala&quot;&gt;&lt;span&gt;// foo.scala&lt;/span&gt;
&lt;span&gt;class&lt;/span&gt; Foo
&amp;nbsp;
&lt;span&gt;class&lt;/span&gt; Baz &lt;span&gt;extends&lt;/span&gt; Bar&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&amp;#8230;and the Java class:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;line_numbers&quot;&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre class=&quot;java5&quot;&gt;&lt;span&gt;// Bar.java&lt;/span&gt;
&lt;span&gt;public&lt;/span&gt; &lt;span&gt;class&lt;/span&gt; Bar &lt;span&gt;extends&lt;/span&gt; Foo &lt;span&gt;&amp;#123;&lt;/span&gt;&lt;span&gt;&amp;#125;&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;If we try to compile &lt;code&gt;foo.scala&lt;/code&gt; before &lt;code&gt;Bar.java&lt;/code&gt;, the Scala compiler will issue a type error complaining that class &lt;code&gt;Bar&lt;/code&gt; does not exist.&amp;nbsp; Similarly, if we attempt the to compile &lt;code&gt;Bar.java&lt;/code&gt; first, the Java compiler will whine about the lack of a &lt;code&gt;Foo&lt;/code&gt; class.&amp;nbsp; Now, there is actually a way to resolve this &lt;em&gt;particular&lt;/em&gt; case (by splitting &lt;code&gt;foo.scala&lt;/code&gt; into two separate files), but it&amp;#8217;s easy to imagine other examples where the circular dependency is impossible to linearize.&amp;nbsp; For the sake of example, let&amp;#8217;s just assume that this circular dependency is a problem and cannot be handled piece-meal.&lt;/p&gt;
&lt;p&gt;In order for this to work, either the Scala compiler will need to know about class &lt;code&gt;Bar&lt;/code&gt; before its compilation, or vice versa.&amp;nbsp; This implies that one of the compilers will need to be able to analyze sources which target the other.&amp;nbsp; Since Scala is the language in question, it only makes sense that it be the accommodating one (rather than &lt;code&gt;javac&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;What &lt;code&gt;scalac&lt;/code&gt; has to do is literally parse and analyze all of the Scala sources it is given in addition to any Java sources which may also be supplied.&amp;nbsp; It doesn&amp;#8217;t need to be a full fledged Java compiler, but it does have to know enough about the Java language to be able to produce an annotated structural AST for any Java source file.&amp;nbsp; Once this AST is available, circular dependencies may be handled in exactly the same way as circular dependencies internal to Scala sources (because all Scala &lt;em&gt;and&lt;/em&gt; all Java classes are available simultaneously to the compiler).&lt;/p&gt;
&lt;p&gt;Once the analysis phase of &lt;code&gt;scalac&lt;/code&gt; has blessed the Scala AST, all of the Java nodes may be discarded.&amp;nbsp; At this point, circular dependencies have been resolved and all type errors have been handled.&amp;nbsp; Thus, there is no need to carry around useless class information.&amp;nbsp; Once &lt;code&gt;scalac&lt;/code&gt; is done, both the &lt;code&gt;Foo&lt;/code&gt; and the &lt;code&gt;Baz&lt;/code&gt; classes will have produced resultant &lt;code&gt;Foo.class&lt;/code&gt; and &lt;code&gt;Baz.class&lt;/code&gt; output files.&lt;/p&gt;
&lt;p&gt;However, we&amp;#8217;re still not quite done yet.&amp;nbsp; Compilation has successfully completed, but if we try to run the application, we will receive a &lt;code&gt;NoClassDefFoundError&lt;/code&gt; due to the fact that the &lt;code&gt;Bar&lt;/code&gt; class has not actually been compiled.&amp;nbsp; Remember, &lt;code&gt;scalac&lt;/code&gt; only &lt;em&gt;analyzed&lt;/em&gt; it for the sake of the type checker, no actual bytecode was produced.&amp;nbsp; &lt;code&gt;Bar&lt;/code&gt; may even suffer from a compile error of some sort, as long as this error is within the method definitions, &lt;code&gt;scalac&lt;/code&gt; isn&amp;#8217;t going to catch it.&lt;/p&gt;
&lt;p&gt;The final step is to invoke &lt;code&gt;javac&lt;/code&gt; against the &lt;code&gt;.java&lt;/code&gt; source files (the same ones we passed to &lt;code&gt;scalac&lt;/code&gt;) adding &lt;code&gt;scalac&lt;/code&gt;&amp;#8217;s output directory to &lt;code&gt;javac&lt;/code&gt;&amp;#8217;s classpath.&amp;nbsp; Thus, &lt;code&gt;javac&lt;/code&gt; will be able to find the &lt;code&gt;Foo&lt;/code&gt; class that we just compiled so as to successfully (hopefully) compile the &lt;code&gt;Bar&lt;/code&gt; class.&amp;nbsp; If all goes well, the final result will be three separate files: &lt;code&gt;Foo.class&lt;/code&gt;, &lt;code&gt;Bar.class&lt;/code&gt; and &lt;code&gt;Baz.class&lt;/code&gt;.&lt;/p&gt;
&lt;h3&gt;Usage&lt;/h3&gt;
&lt;p&gt;Although the concepts are identical, Scala&amp;#8217;s joint compilation works slightly differently from Groovy&amp;#8217;s from a usage standpoint.&amp;nbsp; More specifically: &lt;code&gt;scalac&lt;/code&gt; does &lt;em&gt;not&lt;/em&gt; automatically invoke &lt;code&gt;javac&lt;/code&gt; on the specified &lt;code&gt;.java&lt;/code&gt; sources.&amp;nbsp; This means that you can perform &amp;#8220;joint compilation&amp;#8221; using &lt;code&gt;scalac&lt;/code&gt;, but without invoking &lt;code&gt;javac&lt;/code&gt; you will only receive the compiled Scala classes, the Java classes will be ignored (except by the type checker).&amp;nbsp; This design has some nice benefits, but it does mean that we usually need at least one extra command in our compilation process.&lt;/p&gt;
&lt;p&gt;All of the following usage examples assume that you have defined the earlier example in the following hierarchy:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;src&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;main&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;java&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;Bar.java&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;scala&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;foo.scala&lt;/li&gt;
&lt;/ul&gt;
&lt;/ul&gt;
&lt;/ul&gt;
&lt;li&gt;target&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;classes&lt;/li&gt;
&lt;/ul&gt;
&lt;/ul&gt;
&lt;h3&gt;Command Line&lt;/h3&gt;
&lt;pre&gt;# include both .scala AND .java files
scalac -d target/classes src/main/scala/*.scala src/main/java/*.java

javac -d target/classes 
      -classpath $SCALA_HOME/lib/scala-library.jar:target/classes 
       src/main/java/*.java&lt;/pre&gt;
&lt;h3&gt;Ant&lt;/h3&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;line_numbers&quot;&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre class=&quot;xml&quot;&gt;&lt;span class=&quot;sc3&quot;&gt;&lt;span&gt;&amp;lt;target&lt;/span&gt; &lt;span&gt;name=&lt;/span&gt;&lt;span&gt;&amp;quot;build&amp;quot;&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;sc3&quot;&gt;&lt;span&gt;&amp;lt;scalac&lt;/span&gt; &lt;span&gt;srcdir=&lt;/span&gt;&lt;span&gt;&amp;quot;src/main&amp;quot;&lt;/span&gt; &lt;span&gt;destdir=&lt;/span&gt;&lt;span&gt;&amp;quot;target/classes&amp;quot;&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
        &lt;span class=&quot;sc3&quot;&gt;&lt;span&gt;&amp;lt;include&lt;/span&gt; &lt;span&gt;name=&lt;/span&gt;&lt;span&gt;&amp;quot;scala/**/*.scala&amp;quot;&lt;/span&gt;&lt;span&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
        &lt;span class=&quot;sc3&quot;&gt;&lt;span&gt;&amp;lt;include&lt;/span&gt; &lt;span&gt;name=&lt;/span&gt;&lt;span&gt;&amp;quot;scala/**/*.java&amp;quot;&lt;/span&gt;&lt;span&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;sc3&quot;&gt;&lt;span&gt;&amp;lt;/scalac&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&amp;nbsp;
    &lt;span class=&quot;sc3&quot;&gt;&lt;span&gt;&amp;lt;javac&lt;/span&gt; &lt;span&gt;srcdir=&lt;/span&gt;&lt;span&gt;&amp;quot;src/main/java&amp;quot;&lt;/span&gt; &lt;span&gt;destdir=&lt;/span&gt;&lt;span&gt;&amp;quot;${scala.library}:target/classes&amp;quot;&lt;/span&gt; 
           &lt;span&gt;classpath=&lt;/span&gt;&lt;span&gt;&amp;quot;target/classes&amp;quot;&lt;/span&gt;&lt;span&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;sc3&quot;&gt;&lt;span&gt;&amp;lt;/target&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;Maven&lt;/h3&gt;
&lt;p&gt;One thing you gotta love about Maven: it&amp;#8217;s fairly low on configuration for certain common tasks.&amp;nbsp; Given the above directory structure and the most recent version of the &lt;code&gt;maven-scala-plugin&lt;/code&gt;, the following command should be sufficient for joint compilation:&lt;/p&gt;
&lt;pre&gt;mvn compile&lt;/pre&gt;
&lt;p&gt;Unfortunately, there &lt;a href=&quot;http://www.nabble.com/forum/ViewPost.jtp?post=20845683&amp;framed=y&quot;&gt;have been some problems&lt;/a&gt; reported with the default configuration and complex inter-dependencies between Scala and Java (and back again).&amp;nbsp; I&amp;#8217;m not a Maven&amp;#8230;maven, so I can&amp;#8217;t help too much, but as I understand things, this POM fragment seems to work well:&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;line_numbers&quot;&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre class=&quot;xml&quot;&gt;&lt;span class=&quot;sc3&quot;&gt;&lt;span&gt;&amp;lt;plugin&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;sc3&quot;&gt;&lt;span&gt;&amp;lt;groupId&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;org.scala-tools&lt;span class=&quot;sc3&quot;&gt;&lt;span&gt;&amp;lt;/groupId&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;sc3&quot;&gt;&lt;span&gt;&amp;lt;artifactId&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;maven-scala-plugin&lt;span class=&quot;sc3&quot;&gt;&lt;span&gt;&amp;lt;/artifactId&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&amp;nbsp;
    &lt;span class=&quot;sc3&quot;&gt;&lt;span&gt;&amp;lt;executions&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
        &lt;span class=&quot;sc3&quot;&gt;&lt;span&gt;&amp;lt;execution&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
            &lt;span class=&quot;sc3&quot;&gt;&lt;span&gt;&amp;lt;id&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;compile&lt;span class=&quot;sc3&quot;&gt;&lt;span&gt;&amp;lt;/id&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
            &lt;span class=&quot;sc3&quot;&gt;&lt;span&gt;&amp;lt;goals&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
            &lt;span class=&quot;sc3&quot;&gt;&lt;span&gt;&amp;lt;goal&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;compile&lt;span class=&quot;sc3&quot;&gt;&lt;span&gt;&amp;lt;/goal&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
            &lt;span class=&quot;sc3&quot;&gt;&lt;span&gt;&amp;lt;/goals&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
            &lt;span class=&quot;sc3&quot;&gt;&lt;span&gt;&amp;lt;phase&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;compile&lt;span class=&quot;sc3&quot;&gt;&lt;span&gt;&amp;lt;/phase&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
        &lt;span class=&quot;sc3&quot;&gt;&lt;span&gt;&amp;lt;/execution&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&amp;nbsp;
        &lt;span class=&quot;sc3&quot;&gt;&lt;span&gt;&amp;lt;execution&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
            &lt;span class=&quot;sc3&quot;&gt;&lt;span&gt;&amp;lt;id&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;test-compile&lt;span class=&quot;sc3&quot;&gt;&lt;span&gt;&amp;lt;/id&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
            &lt;span class=&quot;sc3&quot;&gt;&lt;span&gt;&amp;lt;goals&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
            &lt;span class=&quot;sc3&quot;&gt;&lt;span&gt;&amp;lt;goal&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;testCompile&lt;span class=&quot;sc3&quot;&gt;&lt;span&gt;&amp;lt;/goal&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
            &lt;span class=&quot;sc3&quot;&gt;&lt;span&gt;&amp;lt;/goals&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
            &lt;span class=&quot;sc3&quot;&gt;&lt;span&gt;&amp;lt;phase&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;test-compile&lt;span class=&quot;sc3&quot;&gt;&lt;span&gt;&amp;lt;/phase&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
        &lt;span class=&quot;sc3&quot;&gt;&lt;span&gt;&amp;lt;/execution&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&amp;nbsp;
        &lt;span class=&quot;sc3&quot;&gt;&lt;span&gt;&amp;lt;execution&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
            &lt;span class=&quot;sc3&quot;&gt;&lt;span&gt;&amp;lt;phase&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;process-resources&lt;span class=&quot;sc3&quot;&gt;&lt;span&gt;&amp;lt;/phase&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
            &lt;span class=&quot;sc3&quot;&gt;&lt;span&gt;&amp;lt;goals&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
            &lt;span class=&quot;sc3&quot;&gt;&lt;span&gt;&amp;lt;goal&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;compile&lt;span class=&quot;sc3&quot;&gt;&lt;span&gt;&amp;lt;/goal&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
            &lt;span class=&quot;sc3&quot;&gt;&lt;span&gt;&amp;lt;/goals&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
        &lt;span class=&quot;sc3&quot;&gt;&lt;span&gt;&amp;lt;/execution&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;sc3&quot;&gt;&lt;span&gt;&amp;lt;/executions&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;sc3&quot;&gt;&lt;span&gt;&amp;lt;/plugin&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&amp;nbsp;
&lt;span class=&quot;sc3&quot;&gt;&lt;span&gt;&amp;lt;plugin&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;sc3&quot;&gt;&lt;span&gt;&amp;lt;artifactId&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;maven-compiler-plugin&lt;span class=&quot;sc3&quot;&gt;&lt;span&gt;&amp;lt;/artifactId&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;sc3&quot;&gt;&lt;span&gt;&amp;lt;configuration&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
        &lt;span class=&quot;sc3&quot;&gt;&lt;span&gt;&amp;lt;source&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;1.5&lt;span class=&quot;sc3&quot;&gt;&lt;span&gt;&amp;lt;/source&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
        &lt;span class=&quot;sc3&quot;&gt;&lt;span&gt;&amp;lt;target&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;1.5&lt;span class=&quot;sc3&quot;&gt;&lt;span&gt;&amp;lt;/target&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;sc3&quot;&gt;&lt;span&gt;&amp;lt;/configuration&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;sc3&quot;&gt;&lt;span&gt;&amp;lt;/plugin&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;You can find more information on &lt;a href=&quot;http://www.nabble.com/forum/ViewPost.jtp?post=20806619&amp;framed=y&quot;&gt;the mailing-list thread&lt;/a&gt;.&lt;/p&gt;
&lt;h3&gt;Buildr&lt;/h3&gt;
&lt;p&gt;Joint compilation for mixed Scala / Java projects has been &lt;a href=&quot;http://issues.apache.org/jira/browse/BUILDR-136&quot;&gt;a long-standing request of mine&lt;/a&gt; in Buildr&amp;#8217;s JIRA.&amp;nbsp; However, because it&amp;#8217;s not a high priority issue, the developers were never able to address it themselves.&amp;nbsp; Of course, that doesn&amp;#8217;t stop the rest of us from pitching in!&lt;/p&gt;
&lt;p&gt;I had a little free time yesterday afternoon, so I decided to blow it by hacking out a quick implementation of joint Scala compilation in Buildr, based on its pre-existing support for joint compilation in Groovy projects.&amp;nbsp; All of my work is available in &lt;a href=&quot;http://github.com/djspiewak/buildr&quot;&gt;my Buildr fork on GitHub&lt;/a&gt;.&amp;nbsp; This also includes some other unfinished goodies, so if you want only the joint compilation, clone just the &lt;code&gt;scala-joint-compilation&lt;/code&gt; branch.&lt;/p&gt;
&lt;p&gt;Once you have Buildr&amp;#8217;s full sources, &lt;code&gt;cd&lt;/code&gt; into the directory and enter the following command:&lt;/p&gt;
&lt;pre&gt;rake setup install&lt;/pre&gt;
&lt;p&gt;You may need to &lt;code&gt;gem install&lt;/code&gt; a few packages.&amp;nbsp; Further, the exact steps required may be slightly different on different platforms.&amp;nbsp; You can find more details &lt;a href=&quot;http://incubator.apache.org/buildr/contributing.html#working_with_source_code&quot;&gt;on Buildr&amp;#8217;s project page&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;With this highly-unstable version of Buildr installed on your unsuspecting system, you should now be able to make the following addition to your &lt;code&gt;buildfile&lt;/code&gt; (assuming the directory structure given earlier):&lt;/p&gt;

&lt;div class=&quot;wp_syntax&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;line_numbers&quot;&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre class=&quot;ruby&quot;&gt;&lt;span&gt;require&lt;/span&gt; &lt;span&gt;'buildr/scala'&lt;/span&gt;
&amp;nbsp;
&lt;span&gt;# rest of the file...&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Just like Buildr&amp;#8217;s joint compilation for Groovy, you must explicitly &lt;code&gt;require&lt;/code&gt; the language, otherwise important things will break.&amp;nbsp; With this slight modification, you should be able to build your project as per normal:&lt;/p&gt;
&lt;pre&gt;buildr&lt;/pre&gt;
&lt;p&gt;This support is so bleeding-edge, I don&amp;#8217;t even think that it&amp;#8217;s safe to call it &amp;#8220;pre-alpha&amp;#8221;.&amp;nbsp; If you run into any problems, feel free to &lt;a href=&quot;mailto:djspiewak@gmail.com&quot;&gt;shoot me an email&lt;/a&gt; or &lt;a href=&quot;http://issues.apache.org/jira/browse/BUILDR-136&quot;&gt;comment on the issue&lt;/a&gt;.&lt;/p&gt;
&lt;h3&gt;Conclusion&lt;/h3&gt;
&lt;p&gt;Joint compilation of Java and Scala sources is a profound addition to the Scala feature list, making it significantly easier to use Scala alongside Java in pre-existing or future projects.&amp;nbsp; With this support, it is finally possible to use Scala as a truly drop-in replacement for Java without modifying the existing infrastructure beyond the CLASSPATH.&amp;nbsp; Hopefully this article has served to bring slightly more exposure to this feature, as well as provide some much-needed documentation on its use.&lt;/p&gt;</description>
	<pubDate>Mon, 05 Jan 2009 08:00:00 +0000</pubDate>
</item>
<item>
	<title>Caoyuan Deng: A Case Study of Scalability Related &quot;Out of memory&quot; Crash in Erlang</title>
	<guid>http://blogtrader.net/page/dcaoyuan/entry/a_case_study_of_scalable</guid>
	<link>http://blogtrader.net/page/dcaoyuan/entry/a_case_study_of_scalable</link>
	<description>&lt;p&gt;
We are building a platform for message switching, in Erlang. Everything looks OK on stability and features. It actually has run more than half year with zero down. We tested its performance on our 2-core CPU machine before, and got about 140 transactions/second, it's good enough.
&lt;p&gt;
Then, we got a 8-core CPU machine several weeks ago, and we did same performance testing on it, to see the scalability. Since Erlang is almost perfect on scalability, you can image the result, yes, about 700 transactions/second now, scaled almost linear. Until it crashed with &quot;out of memory&quot; when million hits processed.
&lt;p&gt;
It left a very big &quot;erl_crash.dump&quot; file there, I had to dig the issue. My first guess was, were some remote requests (access db, access remote web service etc) timeout but the process itself was not timeout yet, and cause more and more processes kept in VM?
&lt;p&gt;
A quick &lt;b&gt;grep &quot;=proc:&quot; erl_crash.dump&lt;/b&gt; showed that the total number of processes was about 980, which was reasonable for our case.
&lt;p&gt;
So, which process ate so many memory? A quick &lt;b&gt;grep &quot;Stack+head&quot; erl_crash.dump&lt;/b&gt; showed that there was indeed a process with 285082125 size of Stack+head there.
&lt;p&gt;
Following this clue, I caught this process:
&lt;pre&gt;
=proc:0.4.0&gt;
State: Garbing
Name: error_logger
Spawned as: proc_lib:init_p/5
Last scheduled in for: io_lib_format:pad_char/2
Spawned by: 0.1.0&gt;
Started: Sun Apr  1 01:21:50 2012
Message queue length: 2086029
Number of heap fragments: 1234053
Heap fragment data: 281266956
Link list: [0.27.0&gt;, 0.0.0&gt;, {from,0.42.0&gt;,#Ref0.0.0.88&gt;}]
Reductions: 72745575
Stack+heap: 285082125
OldHeap: 47828850
Heap unused: 121777661
OldHeap unused: 47828850
Program counter: 0x0764c66c (io_lib_format:pad_char/2 + 4)
CP: 0x0764c1b4 (io_lib_format:collect_cseq/2 + 124)
&lt;/pre&gt; 
&lt;p&gt;
This process was error_logger, which is from OTP/Erlang standard lib: &lt;b&gt;error_logger&lt;/b&gt;, writing received messages to log file or tty. The typical usage is:
&lt;pre class=&quot;sh_erlang&quot;&gt;
error_logger:info_msg(&quot;~p:~p &quot; ++ Format, [?MODULE, ?LINE] ++ Data))
&lt;/pre&gt;
&lt;p&gt;
Which will format Data to a &lt;b&gt;String&lt;/b&gt; according to the Format string, and write it to tty or log file.
&lt;p&gt; 
The above case showed the message queue length of process &quot;error_logger&quot; had reached 1234053, and the Stack+heap was 285082125, about 272M size. 
&lt;p&gt;
So the cause may be, that the message queue could not be processed in time, the messages were crowded in error_logger's process and finally caused &quot;out of memory&quot;. The bottle-neck was that when error_logger tried to format the message to String, Erlang VM was weak on processing them, which seemed to need a lot of CPU cycles.
&lt;p&gt; 
In my previous blog, I talked about Erlang is bad on massive text processing. Erlang processes String/Text via List, which is obvious bottle-neck in Erlang now, with Erlang is getting much and much popular and more and more Erlang applications are written.
&lt;p&gt;
But, why this did not happen on our 2-core CPU machine? It's an interesting scalability related problem:
&lt;p&gt;
&quot;error_logger&quot; module will registered one and only one process to receive and handle all log messages. But Erlang VM's scheduler can not distribute &lt;b&gt;ONE&lt;/b&gt; process to use multiple CPUs' computing ability. In our 2-core machine, the whole ability is about 140 transactions/second, the one process of &quot;error_logger&quot; just happened to have the power to handle corresponding log messages in time. Under 8-core CPUs machine, our platform scales to handle 700 transactions/second, but there is still only one process of &quot;error_logger&quot;, which can not use 8-core CPUs' ability at all, and finally fail on it.
&lt;p&gt;
Erlang treats every process fairly (although you can change the priority manually), we can do a simple/quick evaluation:
&lt;p&gt;
1. 2-Core machine, keeping hits at 140 trans/second:&lt;br /&gt;
The number of simultaneous processes will be about 200, each process shares the CPU cycles: 1/200 * 2 Core = 1%
&lt;p&gt;
2. 8-Core machine, keeping hits at 700 trans/second:&lt;br /&gt;
The number of simultaneous processes will be about 980, each process shares the CPU cycles: 1/980 * 8 Core = 0.82%
&lt;p&gt;
So, the CPU cycles shared by error_logger process actually not increases.
&lt;p&gt;
BTW, I think error_logger should cut its message queue when can not process them in time (disk IO may also be slower than receiving messages).
&lt;p&gt;&lt;/p&gt;&lt;/p&gt;&lt;/p&gt;&lt;/p&gt;&lt;/p&gt;&lt;/p&gt;&lt;/p&gt;&lt;/p&gt;&lt;/p&gt;&lt;/p&gt;&lt;/p&gt;&lt;/p&gt;&lt;/p&gt;&lt;/p&gt;&lt;/p&gt;&lt;/p&gt;&lt;/p&gt;&lt;/p&gt;&lt;/p&gt;</description>
	<pubDate>Sun, 04 Jan 2009 02:59:59 +0000</pubDate>
</item>
<item>
	<title>Rants, Raves and Ridicule: Initial thoughts on JavaFX</title>
	<guid>tag:blogger.com,1999:blog-1673113361032868171.post-387712346887103607</guid>
	<link>http://suereth.blogspot.com/2009/01/initial-thoughts-on-javafx.html</link>
	<description>So recently I've been reading a little bit about the new &quot;JavaFX&quot; platform Sun has released.  Specifically I've been looking into the Fx Script language.  When I first heard about it, I kept thinking &quot;OH no, *another* JVM language.  I hope they don't make it too similar to java!&quot;.   However, I've been pleasantly surprised by the features so far.&lt;br /&gt;&lt;br /&gt;Based on a few tutorials, JavaFX script appears to have chosen nice features from languages like Scala and Javascript, while adding one languages feature that truly distinguishes it (and IMHO makes it a worthwhile language to learn/use for its domain).  Here's a list of features I saw in the tutorials that I believe make the core of the Fx Script language:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Declarative Constructors (like javascript) - &lt;pre&gt;{ fieldName: value }&lt;/pre&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Truly Useful Type inferences (with types on the right) [like scala] - &lt;pre&gt;var radius = 5.0&lt;/pre&gt;&lt;i&gt;Note: types can be specified&lt;/i&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Three main types of constructs (similar to scala) - mutable references, immutable reference and functions&lt;pre&gt;var mutableValue = 5.0&lt;br /&gt;def someStaticValue = 2.0&lt;br /&gt;function doSomething() {}&lt;br /&gt;&lt;/pre&gt;  These made me think scala's def=&gt; Fx's function, Scala's var=&gt; Fx's var and Scala's val=&gt; Fx's def&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Value Binding - (this is a truly compelling feature for JavaFX)&lt;pre&gt;var x = 0;&lt;br /&gt;def y = bind x;&lt;br /&gt;&lt;/pre&gt; The above code means that x and y will always have the same value, even though y is immutable.  This is useful for synching your model to your view.  I'm excited to start toying with this.  I've only shown on possible syntax, there are actually a few ways to bind variables. &lt;/li&gt;&lt;br /&gt;&lt;li&gt;Bound functions - &lt;pre&gt;var x = 0;&lt;br /&gt;def foo = bound function(...) {...};&lt;br /&gt;&lt;/pre&gt; The above code means that the function foo will see any changes made to &quot;x&quot;.  I.e. foo is no longer a closure in the sense that it retains the value of x during its call.&lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;Anyway, I'm pleasantly surprised with JavaFX and the Fx Script language.  I think a few good ideas from other JVM languages are finally really starting to take hold.   Most importantly, I'm glad Scala isn't the only viable JVM language with really useful type-inference.   I'm hoping the other statically typed languages will follow suit (This means you java!).</description>
	<pubDate>Sat, 03 Jan 2009 12:44:34 +0000</pubDate>
	<author>noreply@blogger.com (J. Suereth)</author>
</item>
<item>
	<title>Ricky Clarkson: The Typeclass Pattern</title>
	<guid>tag:blogger.com,1999:blog-8678405.post-4262556225309513082</guid>
	<link>http://rickyclarkson.blogspot.com/2009/01/typeclass-pattern.html</link>
	<description>The Typeclass Pattern
&lt;p&gt;
This pattern applies to C# and Java, and possibly some other typed languages.  It doesn't apply to C++, which does generics via templates.
&lt;p&gt;
A simple problem to demonstrate it with is a generic Pythagoras method that takes two numbers a and b and computes sqrt(a * a + b * b) for them.  In both C# and Java this can't be written in the most straightforward way:&lt;pre&gt;&lt;code&gt;public T Pythagoras&amp;lt;T&amp;gt;(T a, T b)
{
    return Math.sqrt(a * a + b * b);
}&lt;/code&gt;&lt;/pre&gt;There's more than one reason, but the 'innermost' is that T doesn't have the operator *, and there's no way to restrict T so that it does.
&lt;p&gt;
The equivalent C++ will work, because C++ generates code rather than using constraints.
&lt;p&gt;
So the missing information in Pythagoras is:
&lt;p&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;* How to multiply two Ts.  (let's assume T * T gives T)
&lt;p&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;* How to add two Ts.  (let's assume T + T gives T)
&lt;p&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;* How to find the square root of a T.&lt;pre&gt;&lt;code&gt;
interface Num&amp;lt;T&amp;gt;
{
    T Add(T one, T two);
    T Multiply(T one, T two);
    T Sqrt(T t);
}&lt;/code&gt;&lt;/pre&gt;Now Pythagoras can be written with an extra parameter, a Num:&lt;pre&gt;&lt;code&gt;public T Pythagoras&amp;lt;T&amp;gt;(T a, T b, Num&amp;lt;T&amp;gt; num)
{
    return num.Sqrt(num.Add(num.Multiply(a, a), num.Multiply(b, b)));
}&lt;/code&gt;&lt;/pre&gt;Clearly, some language support wouldn't go amiss for this, at least in
the arithmetic case.  What's more of a problem is that your method gets a
'surprising' extra parameter, the Num.  It's one of those things that
makes sense in isolation, but is really an unnecessary detail when
reading the code to gain an understanding of it.  Scala offers an
interesting solution to that.
&lt;p&gt;
In Scala, you might write Pythagoras as:&lt;pre&gt;&lt;code&gt;def pythagoras[T](a: T, b: T)(implicit num: Num[T]) = num.sqrt(num.add(num.multiply(a, a), num.multiply(b, b)))&lt;/code&gt;&lt;/pre&gt;Then calling it would look like:&lt;pre&gt;&lt;code&gt;pythagoras(3, 5)&lt;/code&gt;&lt;/pre&gt;The second parameter list for pythagoras is an 'implicit' parameter list, meaning that in compilation the compiler looks for a Num[T] in
scope that it can use for num.  You can specify it explicitly, e.g., &lt;code&gt;pythagoras(3, 5)(Num.integer)&lt;/code&gt;, or you can just import Num.integer at some point.
&lt;p&gt;
As with all patterns, though, all it takes is a language to support it
directly, and then the pattern disappears from user code.  In Haskell,
Num is a built in type class and Int, Double, etc. are types that are
instances, or members, of Num.  +, -, *, / are defined as methods that
are part of the Num typeclass.  Let's omit the sqrt part for a moment:&lt;pre&gt;&lt;code&gt;Prelude&gt; let pythagorasSquared a b = a * a + b * b
Prelude&gt; pythagorasSquared 3 4
25
Prelude&gt; :type pythagorasSquared
pythagorasSquared :: (Num t) =&gt; t -&gt; t -&gt; t&lt;/code&gt;&lt;/pre&gt;I hope the first two commands I gave to the Haskell interpreter are
self-explanatory.  The third asks what type pythagorasSquared is.
That's like asking what signature a method has.
&lt;p&gt;
pythagorasSquared is a function with one type parameter called
t.  t is an instance, or member, of the Num type class.
pythagorasSquared takes two values of this type t, and returns a value
of the same type.  E.g., given two Doubles it will yield a Double.
This usually works internally by passing around a Num implicitly, like in the
Scala solution, but can work quite like the C++ code generation way,
depending entirely on the compiler.
&lt;p&gt;
The reason that I omitted the sqrt is that in Haskell, sqrt is defined
on Floating, another type class, rather than Num (giving the sqrt of an Int as an Int is perhaps not useful).
&lt;p&gt;
At some point, some readers will have stopped understanding the terms,
but if you grasped the concept, this wasn't a waste of time!&lt;/p&gt;&lt;/p&gt;&lt;/p&gt;&lt;/p&gt;&lt;/p&gt;&lt;/p&gt;&lt;/p&gt;&lt;/p&gt;&lt;/p&gt;&lt;/p&gt;&lt;/p&gt;&lt;/p&gt;</description>
	<pubDate>Fri, 02 Jan 2009 15:02:03 +0000</pubDate>
	<author>noreply@blogger.com (Ricky Clarkson)</author>
</item>
<item>
	<title>Caoyuan Deng: The Year That Will Be</title>
	<guid>http://blogtrader.net/page/dcaoyuan/entry/the_year_that_will_be</guid>
	<link>http://blogtrader.net/page/dcaoyuan/entry/the_year_that_will_be</link>
	<description>It's 2009 now, in Beijing.

&lt;h2&gt;1==0.999999999......&lt;/h2&gt;
I met Erlang 2 years ago, which finally brings me to Scala. I learnt a lot from Erlang, and I entered the Scala world with Erlang atmosphere surrounding me. The FP, the Pattern Match, the Actor/Process, I found these familiar friends in Scala everywhere.
&lt;p&gt;
Scala has extra bonus, to me, static types and OO/FP. The domains I face are usually with a lot of business logic, or, the worlds I try to describe are not only messages, they are, models I don't think are suitable to describe in Function only. 
&lt;p&gt;
The world itself is OO/FP mixed, like Martin's quote: Two sides of coin. It's something like the Particle/Wave in Quantum. The world is an infinite whole, but the reason of Human Being is always finite, we are using out finite reason to measure the infinite world, it's an unsolvable contradiction: Infinity vs Finite. We have to read our world in OO and, in FP, in snapshot and in continuation.
&lt;p&gt;
There won't be &quot;Super Hero&quot; in computer languages, the world is getting self-organization and harmony, so do the languages. Each language is living in an eco-system, born, growing via interacting with environment, disappear ...

&lt;h2&gt;The Economy&lt;/h2&gt;
It was bad in 2008. I tried to do some computing on stock market based on my neural network. What I can say is it will be swing in the next half-year, no big drop, no big rise. The Shanghai Stock Index will swing between 1200 and 3000. At least, no big worse any more.

&lt;h2&gt;My Self&lt;/h2&gt;
I need to make some big decisions in this a year.
&lt;p&gt;&lt;/p&gt;&lt;/p&gt;&lt;/p&gt;&lt;/p&gt;</description>
	<pubDate>Thu, 01 Jan 2009 19:39:00 +0000</pubDate>
</item>
<item>
	<title>Caoyuan Deng: Thinking in Scala vs Erlang</title>
	<guid>http://blogtrader.net/page/dcaoyuan/entry/thinking_in_the_scala_vs</guid>
	<link>http://blogtrader.net/page/dcaoyuan/entry/thinking_in_the_scala_vs</link>
	<description>Keeping Erlang in mind, I've coded two months in Scala, I'm thinking something called &quot;Scala vs Erlang&quot;, I wrote some benchmark code to prove me (the code and result may be available someday), and I'd like to do some gradually summary on it in practical aspect. These opinions may be or not be correct currently due to lacking of deep experience and understanding, but, anyway, I need to record them now and correct myself with more experiences and understanding got on both Scala and Erlang.

&lt;h2&gt;Part I. Syntax&lt;/h2&gt;

&lt;h3&gt;List comprehension&lt;/h3&gt;

&lt;p&gt;Erlang:
&lt;pre class=&quot;sh_erlang&quot;&gt;
Lst = [1,2,3,4],
[X + 1 || X - Lst],
lists:map(fun(X) -&gt; X + 1 end, Lst)
&lt;/pre&gt;

&lt;p&gt;Scala:
&lt;pre class=&quot;sh_scala&quot;&gt;
val lst = List(1,2,3,4) 
for (x - lst) yield x + 1
lst.map{x =&gt; x + 1}
lst.map{_ + 1} // or place holder
&lt;/pre&gt;

&lt;h3&gt;Pattern match&lt;/h3&gt;

&lt;p&gt;Erlang:
&lt;pre class=&quot;sh_erlang&quot;&gt;
case X of
   {A, B} when is_integer(A), A &gt; 1 -&gt; ok;
   _ -&gt; error
end,

{ok, [{A, B} = H|T]} = my_function(X)
&lt;/pre&gt;

&lt;p&gt;Scala:
&lt;pre class=&quot;sh_scala&quot;&gt;
x match {
   case (a:Int, b:_) if a &gt; 1 =&gt; OK // can match type
   case _ =&gt; ERROR
}

val (&quot;ok&quot;, (h@(a, b)) :: t) = my_function(x)
&lt;/pre&gt;

&lt;h3&gt;List, Tuple, Array, Map, Binary, Bit&lt;/h3&gt;

&lt;p&gt;Erlang:
&lt;pre class=&quot;sh_erlang&quot;&gt;
Lst = [1, 2, 3] %% List
[0 | Lst]  %% List concat
{1, 2, 3}  %% Tuple
1, 2, “abc”&gt;&gt;  %% Binary
%% no Array, Map syntax
&lt;/pre&gt;

&lt;p&gt;Scala:
&lt;pre class=&quot;sh_scala&quot;&gt;
val lst = List(1, 2, 3)  // List
0 :: lst  // List concat
(1, 2, 3) // Tuple
Array(1, 2, 3) // Array
Map(“a” -&gt; 1, “b” -&gt; 2) // Map
// no Binary, Bit syntax
&lt;/pre&gt;

&lt;h3&gt;Process, Actor&lt;/h3&gt;
&lt;p&gt;Erlang:
&lt;pre class=&quot;sh_erlang&quot;&gt;
the_actor(X) -&gt; 
   receive 
      ok -&gt; io:format(“~p~n”, [X]);
      I -&gt; the_actor(X + I) %% needs to explicitly continue loop
   end.
P = spawn(mymodule, the_actor, [0])
P ! 1
P ! ok
&lt;/pre&gt;

&lt;p&gt;Scala I:
&lt;pre class=&quot;sh_scala&quot;&gt;
class TheActor(x:Int) extends Actor { 
   def act = loop {
      react {
         case “ok” =&gt; println(x); exit // needs to explicitly exit loop
         case i:Int =&gt; x += i
      }
   }
}
val a = new TheActor(0)
a ! 1
a ! “ok”
&lt;/pre&gt;
&lt;p&gt;Scala II:
&lt;pre class=&quot;sh_scala&quot;&gt;
val a = actor { 
   def loop(x:Int) = {
      react {
         case &quot;ok&quot; =&gt; println(x)
         case i:Int =&gt; loop(x + i)
      }
   }
   loop(0)
}
a ! 1
a ! &quot;ok&quot;
&lt;/pre&gt;

&lt;p&gt;
&lt;h2&gt;Part II. Processes vs Actors&lt;/h2&gt;

&lt;h3&gt;Something I&lt;/h3&gt;
&lt;p&gt;Erlang:
&lt;ul&gt;
&lt;li&gt;Lightweight processes&lt;/li&gt;
&lt;li&gt;You can always (almost) create a new process for each new comer&lt;/li&gt;
&lt;li&gt;Scheduler treats all processes fairly&lt;/li&gt;
&lt;li&gt;Share nothing between processes&lt;/li&gt;
&lt;li&gt;Lightweight context switch between processes&lt;/li&gt;
&lt;li&gt;IO has been carefully delegated to independent processes&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Scala:
&lt;ul&gt;
&lt;li&gt;Active actor is delegated to JVM thread, actor /= thread&lt;/li&gt;
&lt;li&gt;You can create a new actor for each new comer&lt;/li&gt;
&lt;li&gt;But the amount of real workers (threads) is dynamically adjusted according to the processing time&lt;/li&gt;
&lt;li&gt;The later comers may be in wait list for further processing until a spare thread is available&lt;/li&gt;
&lt;li&gt;Share nothing or share something upon you decision&lt;/li&gt;
&lt;li&gt;Heavy context switch between working threads&lt;/li&gt;
&lt;li&gt;IO block is still pain unless good NIO framework (Grizzly?)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;Something II&lt;/h3&gt;
&lt;p&gt;Erlang:
&lt;ul&gt;
&lt;li&gt;Try to service everyone simultaneously&lt;/li&gt;
&lt;li&gt;But may loss service quality when the work is heavy, may time out (out of service)&lt;/li&gt;
&lt;li&gt;Ideal when processing cost is comparable to context switching cost&lt;/li&gt;
&lt;li&gt;Ideal for small message processing in soft real-time&lt;/li&gt;
&lt;li&gt;Bad for massive data processing, and cpu-heavy work&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Scala:
&lt;ul&gt;
&lt;li&gt;Try to service limited number of customers best first&lt;/li&gt;
&lt;li&gt;If can not service all, the later comers will be put in waiting list and may time out (out of service)&lt;/li&gt;
&lt;li&gt;It's difficult for soft real-time on all coming concurrent customers&lt;/li&gt;
&lt;li&gt;Ideal when processing cost is far more than context switching cost (context switch time is in ns on modern JVM)&lt;/li&gt;
&lt;li&gt;When will there be perfect NIO + Actor library?&lt;/li&gt;
&lt;/ul&gt;&lt;/p&gt;&lt;/p&gt;&lt;/p&gt;&lt;/p&gt;&lt;/p&gt;&lt;/p&gt;&lt;/p&gt;&lt;/p&gt;&lt;/p&gt;&lt;/p&gt;&lt;/p&gt;&lt;/p&gt;&lt;/p&gt;&lt;/p&gt;</description>
	<pubDate>Tue, 30 Dec 2008 18:15:17 +0000</pubDate>
</item>
<item>
	<title>Ruminations of a Programmer: Partial Applications with binds in Scala and Haskell</title>
	<guid>tag:blogger.com,1999:blog-22587889.post-8468968857524730759</guid>
	<link>http://debasishg.blogspot.com/2008/12/partial-applications-with-binds-in.html</link>
	<description>One of the fun parts of learning a new language is the involuntary urge of pitching its features against your favorite language of the same paradigm. I have been a Scala enthusiast for quite some time now, and have been trying to learn Haskell .. it's no wonder that I have been passing through many of such &quot;oh! it's flatMap in Scala&quot; like rants, realizations and enlightenments. This post is about one such session .. it was special since I thought at the end of it, that sometimes some language can beat Haskell in its own forte of concise elegance.&lt;br /&gt;&lt;br /&gt;I was going through &lt;a href=&quot;http://book.realworldhaskell.org/read/monads.html&quot;&gt;Chapter 14&lt;/a&gt; of the &lt;a href=&quot;http://book.realworldhaskell.org/&quot;&gt;Real World Haskell&lt;/a&gt; book, that discusses and explains monads. Here is an example straight from the book itself ..&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;span class=&quot;java_keyword&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;qualified&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Data&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Map&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_keyword&quot;&gt;as&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;M&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;java_keyword&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;PersonName&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;java_keyword&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;PhoneNumber&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;java_keyword&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;BillingAddress&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;java_keyword&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;MobileCarrier&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Honest_Bobs_Phone_Network&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_operator&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Morrisas_Marvelous_Mobiles&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_operator&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Petes_Plutocratic_Phones&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_keyword&quot;&gt;deriving&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Eq&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Ord&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;java_plain&quot;&gt;findCarrierBillingAddress&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_operator&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;PersonName&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_operator&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;M&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Map&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;PersonName&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;PhoneNumber&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_operator&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;M&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Map&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;PhoneNumber&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;MobileCarrier&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_operator&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;M&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Map&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;MobileCarrier&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;BillingAddress&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_operator&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_keyword&quot;&gt;Maybe&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;BillingAddress&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;and one of it's implementations, the most concise one (name changed) ..&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;span class=&quot;java_plain&quot;&gt;findCarrierBillingAddress&amp;nbsp;person&amp;nbsp;phoneMap&amp;nbsp;carrierMap&amp;nbsp;addressMap&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;lookup&amp;nbsp;phoneMap&amp;nbsp;person&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_operator&quot;&gt;&amp;gt;&amp;gt;=&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;lookup&amp;nbsp;carrierMap&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_operator&quot;&gt;&amp;gt;&amp;gt;=&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;lookup&amp;nbsp;addressMap&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_keyword&quot;&gt;where&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;lookup&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_keyword&quot;&gt;flip&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;M&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;lookup&lt;/span&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;The problem is to find the address of mobile carriers used by a person, hopping through a few cascaded lookups in mapped information.&lt;br /&gt;&lt;br /&gt;The method uses the chaining function (&lt;code&gt;&gt;&gt;=&lt;/code&gt;) of monads, &quot;&lt;code&gt;bind&lt;/code&gt;&quot;, in Haskell, that binds the result of the computation on the left to the parameter of the one on the right. More specifically, the result of the computation on the left gets applied as a parameter to the already &lt;i&gt;partially applied&lt;/i&gt; function on the right. &lt;br /&gt;&lt;br /&gt;Cool stuff .. bread and butter to an experienced Haskeller. But I do not fall into that category, and succumbed immediately to the urge of comparing it with how it will be implemented in Scala ..&lt;br /&gt;&lt;br /&gt;Here is the meat of the monadic code in Scala ..&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;span class=&quot;java_keyword&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;find_carrier_billing_address&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;java_operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;phone_map&lt;/span&gt;&lt;span class=&quot;java_operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Map&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;],&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;carrier_map&lt;/span&gt;&lt;span class=&quot;java_operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Map&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;MobileCarrier&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;],&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;address_map&lt;/span&gt;&lt;span class=&quot;java_operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Map&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;MobileCarrier&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;])&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;java_plain&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;phone_map&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;flatMap&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;carrier_map&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;)).&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;flatMap&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;address_map&lt;/span&gt;&lt;span class=&quot;java_separator