<?xml version="1.0"?>
<rss version="2.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:media="http://search.yahoo.com/mrss/" xmlns:yt="http://gdata.youtube.com/schemas/2007">
   <channel>
      <title>Planet Just-Scala</title>
      <description>Pipes Output</description>
      <link>http://pipes.yahoo.com/pipes/pipe.info?_id=skjzGlDl3RGCCO8fPxJ3AQ</link>
      <pubDate>Tue, 09 Mar 2010 18:46:25 -0800</pubDate>
      <generator>http://pipes.yahoo.com/pipes/</generator>
      <item>
         <title>Scala Stream Fusion and Specialization</title>
         <link>http://jnordenberg.blogspot.com/2010/03/scala-stream-fusion-and-specialization.html</link>
         <description>&lt;div&gt;&lt;b&gt;Updated:&lt;/b&gt; Fixed code links and added view and stream benchmarks.&lt;br/&gt;&lt;br/&gt;Inspired by the successful results of &lt;a rel=&quot;nofollow&quot; target=&quot;_blank&quot; href=&quot;http://www.cse.unsw.edu.au/~dons/streams.html&quot;&gt;Haskell stream fusion&lt;/a&gt; (see &lt;a rel=&quot;nofollow&quot; target=&quot;_blank&quot; href=&quot;http://donsbot.wordpress.com/2010/03/01/evolving-faster-haskell-programs-now-with-llvm/&quot;&gt;Evolving Faster Haskell Programs (now with LLVM!)&lt;/a&gt; for some impressive optimizations) I was thinking if a similar concept is applicable to Scala collections. It turns out that with a combination of iterators and specialization it's possible to achieve similar optimizations in Scala.&lt;br/&gt;&lt;br/&gt;The goal of stream fusion is essentially to optimize code like this:&lt;br/&gt;&lt;pre&gt;&lt;br/&gt; def scalaLibrarySum(a : Array[Int]) = a.map(i =&amp;gt; i * 3 + 7).filter(i =&amp;gt; (i % 10) == 0).foldLeft(0)(_ + _)&lt;br/&gt;&lt;/pre&gt;&lt;br/&gt;into code like this:&lt;br/&gt;&lt;pre&gt;&lt;br/&gt; def mapFilterSumLoop(a : Array[Int]) = {&lt;br/&gt; var i = 0&lt;br/&gt; var r = 0&lt;br/&gt;&lt;br/&gt; while (i &amp;lt; a.length) {&lt;br/&gt; val v = a(i) * 3 + 7&lt;br/&gt;&lt;br/&gt; if ((v % 10) == 0)&lt;br/&gt; r += v&lt;br/&gt;&lt;br/&gt; i += 1&lt;br/&gt; }&lt;br/&gt;&lt;br/&gt; r&lt;br/&gt; }&lt;br/&gt;&lt;/pre&gt;&lt;br/&gt;If you run the &lt;b&gt;scalaLibrarySum&lt;/b&gt; method in Scala it will create two intermediate arrays with the results of the &lt;b&gt;map&lt;/b&gt; and &lt;b&gt;filter&lt;/b&gt; operations. This is totally unnecessary for this calculation as the functions passed to &lt;b&gt;filter&lt;/b&gt; and &lt;b&gt;map&lt;/b&gt; are side effect free and thus the result of the function applications can be performed lazily just before the result is needed in the fold operation. This is basically how the &lt;b&gt;mapFilterSumLoop&lt;/b&gt; method works.&lt;br/&gt;&lt;br/&gt;Besides creating intermediate arrays, boxing of primitive values must be avoided if we want to have any chance of competitive performance (the Haskell libraries contain specialized instances to avoid boxing). Fortunately Scala supports specialization of type parameters in version 2.8, which enables us to avoid boxing while still writing generic code. Unfortunately this feature seems to be quite buggy at the moment, just by playing around with a simple example I encountered two bugs (tickets &lt;a rel=&quot;nofollow&quot; target=&quot;_blank&quot; href=&quot;https://lampsvn.epfl.ch/trac/scala/ticket/3148&quot;&gt;#3148&lt;/a&gt; and &lt;a rel=&quot;nofollow&quot; target=&quot;_blank&quot; href=&quot;https://lampsvn.epfl.ch/trac/scala/ticket/3149&quot;&gt;#3149&lt;/a&gt;). So, the code below contain some specialization done by hand. Hopefully these bugs will be fixed so that the code can be fully generalized.&lt;br/&gt;&lt;br/&gt;The biggest difference compared to stream fusion in Haskell is that I use impure iterators in the Scala code. This is not as nice as the pure stream code used in Haskell, but it's a fact that Hotspot isn't nearly as good at optimizing pure functional code as GHC. Hotspot works best if fed imperative style loops.&lt;br/&gt;&lt;br/&gt;Here's the definitions of the specialized functions and iterators I use in the benchmark below:&lt;br/&gt;&lt;pre&gt;&lt;br/&gt; // Specialized Function1&lt;br/&gt; trait Fn1[@specialized I, @specialized O] {&lt;br/&gt; def apply(a : I) : O&lt;br/&gt; }&lt;br/&gt;&lt;br/&gt; // Specialized Function2&lt;br/&gt; trait Fn2[@specialized I1, @specialized I2, @specialized O] {&lt;br/&gt; def apply(a1 : I1, a2 : I2) : O&lt;br/&gt; }&lt;br/&gt;&lt;br/&gt; // Specialized iterator&lt;br/&gt; trait SIterator[@specialized T] {&lt;br/&gt; def hasMore : Boolean&lt;br/&gt; def current : T&lt;br/&gt; def next()&lt;br/&gt; }&lt;br/&gt;&lt;/pre&gt;&lt;br/&gt;In addition to this I've defined array, filter and map iterators. Unfortunately these are not generic due to the problems with the specialize feature:&lt;br/&gt;&lt;pre&gt;&lt;br/&gt; class IntArrayIterator(a : Array[Int], var index : Int, endIndex : Int) extends SIterator[Int] {&lt;br/&gt; def next() = index += 1&lt;br/&gt; def current = a(index)&lt;br/&gt; def hasMore = index &amp;lt; endIndex&lt;br/&gt; }&lt;br/&gt;&lt;br/&gt; // Optimally this would be: class FilterIterator[@specialized T](iter : SIterator[T], pred : Fn1[T, Boolean]) extends SIterator[T]&lt;br/&gt; class FilterIterator(iter : SIterator[Int], pred : Fn1[Int, Boolean]) extends SIterator[Int] {&lt;br/&gt; def hasMore = iter.hasMore&lt;br/&gt;&lt;br/&gt; def next() = {&lt;br/&gt; iter.next()&lt;br/&gt; findNext()&lt;br/&gt; }&lt;br/&gt;&lt;br/&gt; def findNext() = {&lt;br/&gt; while (iter.hasMore &amp;amp;&amp;amp; !pred(iter.current))&lt;br/&gt; iter.next()&lt;br/&gt; }&lt;br/&gt;&lt;br/&gt; def current = iter.current&lt;br/&gt;&lt;br/&gt; findNext()&lt;br/&gt; }&lt;br/&gt;&lt;br/&gt; // Optimally this would be: class MapIterator[@specialized U][@specialized T](iter : SIterator[T], fn : Fn1[T, U]) extends SIterator[U]&lt;br/&gt; class MapIterator(iter : SIterator[Int], fn : Fn1[Int, Int]) extends SIterator[Int] {&lt;br/&gt; def next() = iter.next()&lt;br/&gt; def current = fn(iter.current)&lt;br/&gt; def hasMore = iter.hasMore&lt;br/&gt; }&lt;br/&gt;&lt;/pre&gt;&lt;br/&gt;The fold function is straightforward and generic:&lt;br/&gt;&lt;pre&gt;&lt;br/&gt; def fold[@specialized T, @specialized U] (iter : SIterator[T], fn : Fn2[U, T, U], v : U) = {&lt;br/&gt; var r = v&lt;br/&gt;&lt;br/&gt; while (iter.hasMore) {&lt;br/&gt; r = fn(r, iter.current)&lt;br/&gt; iter.next()&lt;br/&gt; }&lt;br/&gt;&lt;br/&gt; r&lt;br/&gt; }&lt;br/&gt;&lt;/pre&gt;&lt;br/&gt;The map-filter-sum function can now be written using iterators:&lt;br/&gt;&lt;pre&gt;&lt;br/&gt; def mapFilterSum(a : Array[Int]) = {&lt;br/&gt; val filter = new Fn1[Int, Boolean] {def apply(a : Int) = (a % 10) == 0}&lt;br/&gt; val map = new Fn1[Int, Int] {def apply(a : Int) = a * 3 + 7}&lt;br/&gt; val s = new FilterIterator(new MapIterator(new IntArrayIterator(a, 0, a.length), map), filter)&lt;br/&gt; fold(s, new Fn2[Int, Int, Int] {def apply(a1 : Int, a2 : Int) = a1 + a2}, 0)&lt;br/&gt; }&lt;br/&gt;&lt;/pre&gt;&lt;br/&gt;The full iterator code can be found &lt;a rel=&quot;nofollow&quot; target=&quot;_blank&quot; href=&quot;http://svn.assembla.com/svn/metascala/src/test/stream/SpecializedIterators.scala&quot;&gt;here&lt;/a&gt;. Compile the code using the latest Scala 2.8 build with the -Yspecialize flag. The optimize flag doesn't seem to have much effect on the performance.&lt;br/&gt;&lt;br/&gt;I've benchmarked four different implementations of the map-filter-sum calculation:&lt;br/&gt;&lt;ul&gt;&lt;br/&gt;&lt;li&gt;The while loop shown above&lt;/li&gt;&lt;br/&gt;&lt;li&gt;The while loop split up into map, filter and fold functions with intermediate array results passed between them&lt;/li&gt;&lt;br/&gt;&lt;li&gt;The version using specialized iterators&lt;/li&gt;&lt;br/&gt;&lt;li&gt;The Scala library implementation shown above&lt;/li&gt;&lt;br/&gt;&lt;li&gt;Same as Scala library function but with a view instead&lt;/li&gt;&lt;br/&gt;&lt;li&gt;Same as Scala library function but with a stream instead&lt;/li&gt;&lt;br/&gt;&lt;/ul&gt;&lt;br/&gt;The benchmark is performed by taking the minimum execution time of 200 runs of each of the functions on an array of 1 million integers. Running the application with latest OpenJDK 7 (Hotspot version &quot;build 17.0-b10&quot;) and the flags &quot;-server -XX:CompileThreshold=100&quot; I get the following results:&lt;br/&gt;&lt;pre&gt;&lt;br/&gt;Loop: (4990,-423600172)&lt;br/&gt;Loop with intermediate arrays: (6690,-423600172)&lt;br/&gt;Specialized iterators: (5367,-423600172)&lt;br/&gt;Scala array: (46444,-423600172)&lt;br/&gt;Scala view: (39625,-423600172)&lt;br/&gt;Scala stream: (63210,-423600172)&lt;br/&gt;&lt;/pre&gt;&lt;br/&gt;The first result value is the minimum execution time in microseconds, the second value is the result of the calculation. As you can see the method using specialized iterators is almost as fast as the single while loop. Hotspot has inlined all the iterator code, not bad! Using intermediate arrays is about 25% slower than specialized iterators. Using the Scala library is about 7-9 times slower! Clearly this bad result is a consequence of boxing taking place. Using a view is fastest here as it also avoids intermediate array creation.&lt;br/&gt;&lt;br/&gt;The conclusion from this simple experiment is that it's certainly possible to write collection libraries with a nice high level interface and at the same time have excellent performance. When Scala specialization support is improved hopefully this power be available to all Scala programmers.&lt;br/&gt;&lt;br/&gt;The full benchmark code can be found &lt;a rel=&quot;nofollow&quot; target=&quot;_blank&quot; href=&quot;http://svn.assembla.com/svn/metascala/src/test/stream/Main.scala&quot;&gt;here&lt;/a&gt;.&lt;div class=&quot;blogger-post-footer&quot;&gt;&lt;img alt=&quot;&quot; height=&quot;1&quot; src=&quot;https://blogger.googleusercontent.com/tracker/3083788237966827171-7390104310519482036?l=jnordenberg.blogspot.com&quot; width=&quot;1&quot;/&gt;&lt;/div&gt;&lt;/div&gt;</description>
         <author>Jesper Nordenberg</author>
         <guid isPermaLink="false">tag:blogger.com,1999:blog-3083788237966827171.post-7390104310519482036</guid>
         <pubDate>Sat, 06 Mar 2010 09:39:00 -0800</pubDate>
      </item>
      <item>
         <title>Taking Advantage of Scala 2.8: Replacing the Builder</title>
         <link>http://villane.wordpress.com/2010/03/05/taking-advantage-of-scala-2-8-replacing-the-builder/</link>
         <description>&lt;div&gt;&lt;p&gt;In Scala 2.8, using the &lt;a rel=&quot;nofollow&quot; target=&quot;_blank&quot; href=&quot;http://blog.rafaelferreira.net/2008/07/type-safe-builder-pattern-in-scala.html&quot;&gt;builder pattern&lt;/a&gt; is no longer necessary (or the most optimal solution) in many cases, as &lt;a rel=&quot;nofollow&quot; target=&quot;_blank&quot; href=&quot;http://www.scala-lang.org/node/4587&quot;&gt;Scala 2.8&lt;/a&gt; adds support for named and default parameters.&lt;/p&gt;
&lt;p&gt;I’ll give an example of this based on &lt;a rel=&quot;nofollow&quot; target=&quot;_blank&quot; href=&quot;http://github.com/Villane/scalabox2d/&quot;&gt;ScalaBox2D&lt;/a&gt;. In the Box2D object model, physics Bodies are defined mostly by a set of Fixtures, which in turn are defined by a Shape, density, friction, restitution and some other parameters.&lt;/p&gt;
&lt;p&gt;In the Scala 2.7 version of ScalaBox2D, there were mutable fixture definitions, which were used by the engine to create the actual fixtures. The user code only worked with definitions, through an internal DSL that looked something like this (note: all code examples are simplified for clarity):&lt;/p&gt;
&lt;pre&gt;body { box(halfWidth, halfHeight) density 1 friction 0.3f restitution 0 computeMassFromShapes
}
&lt;/pre&gt;
&lt;p&gt;For the DSL implementation, I used simple builders which left values to defaults if not specified:&lt;/p&gt;
&lt;pre&gt;def box(halfW: Float, halfH: Float) = new FixtureBuilder(FixtureDef(PolygonDef.box(halfW, halfH))) class FixtureBuilder(defn: FixtureDef) { def userData(userData: AnyRef) = { defn.userData = userData; this } def material(material: Material) = { defn.apply(material); this } def friction(friction: Float) = { defn.friction = friction; this } def restitution(restitution: Float) = { defn.restitution = restitution; this } def density(density: Float) = { defn.density = density; this } def filter(filter: FilterData) = { defn.filter = filter; this } def sensor(isSensor: Boolean) = { defn.isSensor = isSensor; this } def define = defn
}
&lt;/pre&gt;
&lt;p&gt;The FixtureDefs are mutable mostly to simplify the builder. In Scala 2.8, I can use named and default parameters, and drop some more lines of code by not having builders at all. As a bonus, I can easily make the definitions immutable.&lt;/p&gt;
&lt;pre&gt;def fixtures(fd: FixtureDef*) {...}
val fixture = FixtureDef // a shorthand to the companion object of FixtureDef
val box = BoxDef // a shorthand to a BoxDef object that creates PolygonDefs case class FixtureDef( shapeDef: ShapeDef, /** The friction coefficient, usually in the range [0,1]. */ friction: Float = 0.2f, /** The restitution (elasticity) usually in the range [0,1]. */ restitution: Float = 0f, /** The density, usually in kg/m^2. */ density: Float = 0f, /** A sensor collects contact information but never generates a collision response. */ isSensor: Boolean = false, /** Contact filtering data. */ filter: FilterData = FilterData.Default, /** Use this to store application specific fixture data. */ userData: AnyRef = null
)&lt;/pre&gt;
&lt;p&gt;As you can see, the FixtureDef is now a case class with immutable parameters that have default values. Previously it looked very similar, but had only one parameter (ShapeDef) and all fields were mutable. The usage of the “DSL” now becomes a little bit more verbose (maybe I shouldn’t even call it a DSL any more) but I think it also becomes easier to understand for someone who knows the language but not the library, due to less moving parts and using built-in features instead of more code:&lt;/p&gt;
&lt;pre&gt;body { fixtures( fixture(box(halfWidth, halfHeight), density = 1, friction = 0.3f, restitution = 0) ) computeMassFromShapes
}
&lt;/pre&gt;
&lt;p&gt;Deleting code while maintaining functionality always makes me glad and this is one of those cases. I guess there may be some more complex cases where builders may still work better, but for simple things like the above example, I really like the named and default arguments feature.&lt;/p&gt;
&lt;p&gt;Note: moving ScalaBox2D to Scala 2.8 is still a work in progress for me and there may be some further changes to this “DSL” as well.&lt;/p&gt;
&lt;br/&gt; &lt;a rel=&quot;nofollow&quot; target=&quot;_blank&quot; href=&quot;http://feeds.wordpress.com/1.0/gocomments/villane.wordpress.com/115/&quot;&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://feeds.wordpress.com/1.0/comments/villane.wordpress.com/115/&quot;/&gt;&lt;/a&gt; &lt;a rel=&quot;nofollow&quot; target=&quot;_blank&quot; href=&quot;http://feeds.wordpress.com/1.0/godelicious/villane.wordpress.com/115/&quot;&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://feeds.wordpress.com/1.0/delicious/villane.wordpress.com/115/&quot;/&gt;&lt;/a&gt; &lt;a rel=&quot;nofollow&quot; target=&quot;_blank&quot; href=&quot;http://feeds.wordpress.com/1.0/gostumble/villane.wordpress.com/115/&quot;&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://feeds.wordpress.com/1.0/stumble/villane.wordpress.com/115/&quot;/&gt;&lt;/a&gt; &lt;a rel=&quot;nofollow&quot; target=&quot;_blank&quot; href=&quot;http://feeds.wordpress.com/1.0/godigg/villane.wordpress.com/115/&quot;&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://feeds.wordpress.com/1.0/digg/villane.wordpress.com/115/&quot;/&gt;&lt;/a&gt; &lt;a rel=&quot;nofollow&quot; target=&quot;_blank&quot; href=&quot;http://feeds.wordpress.com/1.0/goreddit/villane.wordpress.com/115/&quot;&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://feeds.wordpress.com/1.0/reddit/villane.wordpress.com/115/&quot;/&gt;&lt;/a&gt; &lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://stats.wordpress.com/b.gif?host=villane.wordpress.com&amp;amp;blog=820948&amp;amp;post=115&amp;amp;subd=villane&amp;amp;ref=&amp;amp;feed=1&quot;/&gt;&lt;/div&gt;</description>
         <author>Erkki Lindpere</author>
         <guid isPermaLink="false">http://villane.wordpress.com/?p=115</guid>
         <pubDate>Fri, 05 Mar 2010 13:22:07 -0800</pubDate>
      </item>
      <item>
         <title>Named arguments</title>
         <link>http://scalada.blogspot.com/2008/04/named-arguments.html</link>
         <description>&lt;div&gt;&lt;div&gt;edit at 3rd of March, 2010: Scala will have named arguments in 2.8. Great! &lt;a rel=&quot;nofollow&quot; target=&quot;_blank&quot; href=&quot;http://www.scala-lang.org/sid/1&quot;&gt;http://www.scala-lang.org/sid/1#&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;br/&gt;&lt;/div&gt;Scala unfortunately doesn't have named arguments. I would really like to have them, because they make code much clearer in some cases, and it's harder to make mistakes with them. That's why I'll show one way to simulate them.&lt;br/&gt;&lt;br/&gt;First, let's create the function that can handle homemade named arguments:&lt;br/&gt;&lt;br/&gt;&lt;pre&gt; def f(x: {val name: String; val age: Int}) = {&lt;/pre&gt;&lt;br/&gt;&lt;br/&gt;f takes one argument, that contains the actual parameters we want. The type is shorthand for Any{ ... }.&lt;br/&gt;&lt;br/&gt;Next we have imported the contents of x to be visible inside method f. This is possible due to Scala's handling of objects as first class modules.&lt;br/&gt;&lt;br/&gt;&lt;pre&gt; import x._&lt;/pre&gt;&lt;br/&gt;&lt;br/&gt;And now we can use the attributes as if they were f's real parameters:&lt;br/&gt;&lt;br/&gt;&lt;pre&gt; println(name + &quot; is &quot; + age + &quot; years old&quot;)&lt;/pre&gt;&lt;br/&gt;&lt;br/&gt;How about from client's perspective? Well, client can just create a structural type that contains the needed attributes (actual arguments).&lt;br/&gt;&lt;br/&gt;&lt;pre&gt; f(new {val name = &quot;Anthony&quot;; val age = 5})&lt;/pre&gt;&lt;br/&gt;&lt;br/&gt;As you see, it's not really possible to make mistakes this way. But the calling has too much boilerplate, namely vals. So let's make it a bit better:&lt;br/&gt;&lt;br/&gt;We create a case class for each actual parameter.&lt;br/&gt;&lt;br/&gt;&lt;pre&gt; case class Name(name : String)&lt;br/&gt; case class Age (age : Int)&lt;br/&gt;&lt;/pre&gt;&lt;br/&gt;&lt;br/&gt;Now we can make very clear function signature:&lt;br/&gt;&lt;br/&gt;&lt;pre&gt; def g(x: Name, y: Age) = {&lt;/pre&gt;&lt;br/&gt;&lt;br/&gt;Unfortunately we have to import all parameters one by one.&lt;br/&gt;&lt;br/&gt;&lt;pre&gt; import x._, y._&lt;br/&gt; println(name + &quot; is &quot; + age + &quot; years old&quot;)&lt;/pre&gt;&lt;br/&gt;&lt;br/&gt;The client can call the method in the following way:&lt;br/&gt;&lt;br/&gt;&lt;pre&gt; g(Name(&quot;Tim&quot;), Age(2))&lt;/pre&gt;&lt;br/&gt;&lt;br/&gt;That's it. It's annoying that we have to create case classes, but it's tolerable in important cases.&lt;br/&gt;&lt;br/&gt;Here's the full code:&lt;br/&gt;&lt;br/&gt;&lt;pre&gt;&lt;br/&gt;object Test extends Application{&lt;br/&gt; def f(x: {val name: String; val age: Int}) = {&lt;br/&gt; import x._&lt;br/&gt; println(name + &quot; is &quot; + age + &quot; years old&quot;)&lt;br/&gt; }&lt;br/&gt; f(new {val name = &quot;Anthony&quot;; val age = 5})&lt;br/&gt;&lt;br/&gt; case class Name(name : String)&lt;br/&gt; case class Age (age : Int)&lt;br/&gt;&lt;br/&gt; def g(x: Name, y: Age) = {&lt;br/&gt; import x._, y._&lt;br/&gt; println(name + &quot; is &quot; + age + &quot; years old&quot;)&lt;br/&gt; }&lt;br/&gt; g(Name(&quot;Tim&quot;), Age(2))&lt;br/&gt;}&lt;br/&gt;&lt;/pre&gt;&lt;br/&gt;Run the code and you get as output:&lt;br/&gt;Anthony is 5 years old&lt;br/&gt;Tim is 2 years old&lt;br/&gt;&lt;br/&gt;edit:&lt;br/&gt;&lt;br/&gt;On the client side, often one passes some constant values. If you call just like f(value1, value2, ... valueN), it's hard to know later when you see the code what the meaning for each value was; so you have to go see the API (if there's no tool help). Instead you have to define vals before the call:&lt;br/&gt;&lt;br/&gt;&lt;pre&gt;val age = 8&lt;br/&gt;val name = &quot;Jim&quot;&lt;br/&gt;f(age, name)&lt;/pre&gt;&lt;br/&gt;&lt;br/&gt;But now there's the issue that you have to write the variables twice, and it would be nice to see immeaditely on the call what the value was.&lt;br/&gt;&lt;br/&gt;Then:&lt;br/&gt;&lt;br/&gt;&lt;pre&gt;f(new {val age = 8; val name = &quot;Jim&quot;})&lt;/pre&gt; isn't that bad at all. Neither the case class equivalent. If you have many arguments to pass, calling with single argument per line looks nice:&lt;br/&gt;&lt;pre&gt;f(new { val age = 8&lt;br/&gt; val name = &quot;Jim&quot;&lt;br/&gt; val hobby = &quot;blogging&quot; })&lt;/pre&gt;&lt;br/&gt;&lt;br/&gt;Named arguments are especially useful when constructing objects, because it's then when you have to pass lots of vaguely related arguments.&lt;div class=&quot;blogger-post-footer&quot;&gt;&lt;img alt=&quot;&quot; height=&quot;1&quot; src=&quot;https://blogger.googleusercontent.com/tracker/7480826096025842182-6734876225398370377?l=scalada.blogspot.com&quot; width=&quot;1&quot;/&gt;&lt;/div&gt;&lt;/div&gt;</description>
         <author>Henrik Huttunen</author>
         <guid isPermaLink="false">tag:blogger.com,1999:blog-7480826096025842182.post-6734876225398370377</guid>
         <pubDate>Sun, 13 Apr 2008 10:27:00 -0700</pubDate>
      </item>
      <item>
         <title>Dependency Injection as Function Currying</title>
         <link>http://debasishg.blogspot.com/2010/02/dependency-injection-as-function.html</link>
         <description>&lt;div&gt;Dependency Injection is one of the techniques that I use regularly when I am programming in Java. It's a nice way of making an application decoupled from concrete implementations and localize object creation logic within specific bootstrapping modules. Be it in the form of Spring XML or Guice Modules, the idea is to keep it configurable so that specific components of your application can choose to work with specific implementations of an abstraction.&lt;br/&gt;&lt;br/&gt;It so happens that these days possibly I have started looking at things a bit differently. I have been programming more in Scala and Clojure and being exposed to many of the functional paradigms that they encourage and espouse, it has stated manifesting in the way I think of programming. In this post I will look into dependency injection on a different note. At the end of it may be we will see that this is yet another instance of a pattern melding into the chores of a powerful language's idiomatic use.&lt;br/&gt;&lt;br/&gt;In one of my projects I have a class whose constructor has some of its parameters injected and the others manually provided by the application. Guice has a nice extension that does this for you - &lt;a rel=&quot;nofollow&quot; target=&quot;_blank&quot; href=&quot;http://code.google.com/p/google-guice/wiki/AssistedInject&quot;&gt;AssistedInject&lt;/a&gt;. It writes the boilerplate stuff by generating an implementation of the factory. You just need to annotate the implementation class' constructor and the fields that aren't known to the injector. Here's an example from the Guice page ..&lt;br/&gt;&lt;br/&gt;&lt;code&gt;&lt;span class=&quot;java_keyword&quot;&gt;public&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;java_keyword&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;RealPayment&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;java_keyword&quot;&gt;implements&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Payment&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;&lt;br/&gt;&lt;span class=&quot;java_plain&quot;&gt; @&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Inject&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;br/&gt;&lt;span class=&quot;java_plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;java_keyword&quot;&gt;public&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;RealPayment&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;br/&gt;&lt;span class=&quot;java_plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;CreditService&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt; creditService&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_comment&quot;&gt;// injected&lt;/span&gt;&lt;br/&gt;&lt;span class=&quot;java_plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;AuthService&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt; authService&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_comment&quot;&gt;// injected&lt;/span&gt;&lt;br/&gt;&lt;span class=&quot;java_plain&quot;&gt; @&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Assisted&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Date&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt; startDate&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_comment&quot;&gt;// caller to provide&lt;/span&gt;&lt;br/&gt;&lt;span class=&quot;java_plain&quot;&gt; @&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Assisted&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Money&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt; amount&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_comment&quot;&gt;// aller to provide&lt;/span&gt;&lt;br/&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;&lt;br/&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;&lt;br/&gt;&lt;span class=&quot;java_separator&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;br/&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;br/&gt;&lt;/code&gt;&lt;br/&gt;Then in the Guice module we bind a &lt;code&gt;Provider&amp;lt;Factory&amp;gt;&lt;/code&gt; ..&lt;br/&gt;&lt;br/&gt;&lt;code&gt;&lt;span class=&quot;java_plain&quot;&gt;bind&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;PaymentFactory&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;java_keyword&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;toProvider&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;br/&gt;&lt;span class=&quot;java_plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;FactoryProvider&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;newFactory&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;br/&gt;&lt;span class=&quot;java_plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;PaymentFactory&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;java_keyword&quot;&gt;class&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_type&quot;&gt;RealPayment&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;java_keyword&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;));&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;br/&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;br/&gt;&lt;/code&gt;&lt;br/&gt;The &lt;code&gt;FactoryProvider&lt;/code&gt; maps the &lt;code&gt;create()&lt;/code&gt; method's parameters to the corresponding &lt;code&gt;@Assisted&lt;/code&gt; parameters in the implementation class' constructor. For the other constructor arguments, it asks the regular &lt;code&gt;Injector&lt;/code&gt; to provide values.&lt;br/&gt;&lt;br/&gt;So the basic issue that &lt;code&gt;AssistedInject&lt;/code&gt; solves is to finalize (&lt;i&gt;close&lt;/i&gt;) some of the parameters at the module level to be provided by the injector, while keeping the abstraction &lt;i&gt;open&lt;/i&gt; for the rest to be provided by the caller.&lt;br/&gt;&lt;br/&gt;On a functional note this sounds a lot like &lt;a rel=&quot;nofollow&quot; target=&quot;_blank&quot; href=&quot;http://en.wikipedia.org/wiki/Currying&quot;&gt;currying&lt;/a&gt; .. The best rationale for currying is to allow for partial application of functions, which does the same thing as above in offering a flexible means of keeping parts of your abstraction open for later pluggability.&lt;br/&gt;&lt;br/&gt;Consider the above abstraction modeled as a case class in Scala ..&lt;br/&gt;&lt;br/&gt;&lt;code&gt;&lt;span class=&quot;java_keyword&quot;&gt;trait&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;CreditService&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;br/&gt;&lt;span class=&quot;java_keyword&quot;&gt;trait&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;AuthService&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;br/&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;br/&gt;&lt;span class=&quot;java_keyword&quot;&gt;case&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;java_keyword&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;RealPayment&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;creditService&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;span class=&quot;java_type&quot;&gt;CreditService&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;br/&gt;&lt;span class=&quot;java_plain&quot;&gt; authService&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;span class=&quot;java_type&quot;&gt;AuthService&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;br/&gt;&lt;span class=&quot;java_plain&quot;&gt; startDate&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;span class=&quot;java_type&quot;&gt;Date&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;br/&gt;&lt;span class=&quot;java_plain&quot;&gt; amount&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;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;br/&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;br/&gt;&lt;/code&gt;&lt;br/&gt;One of the features of a Scala case class is that it generates a companion object automatically along with an apply method that enables you to invoke the class constructor as a function object ..&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; rp &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;span class=&quot;java_type&quot;&gt;RealPayment&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_comment&quot;&gt;//..&lt;/span&gt;&lt;br/&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;br/&gt;&lt;/code&gt;&lt;br/&gt;is in fact a syntactic sugar for &lt;code&gt;RealPayment.apply( //&lt;/code&gt;.. that gets called implicitly. But you know all that .. right ?&lt;br/&gt;&lt;br/&gt;Now for a particular module , say I would like to finalize on &lt;code&gt;PayPal&lt;/code&gt; as the &lt;code&gt;CreditService&lt;/code&gt; implementation, so that the users don't have to pass this parameter repeatedly - just like the injector of your favorite dependency injection provider. I can do this as follows in a functional way and pass on a partially applied function to all users of the module .. &lt;br/&gt;&lt;br/&gt;&lt;div&gt;&lt;br/&gt;&lt;code&gt;&lt;span class=&quot;java_plain&quot;&gt;scala&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;span class=&quot;java_keyword&quot;&gt;case&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;java_keyword&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;PayPal&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;provider&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;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;span class=&quot;java_keyword&quot;&gt;extends&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;CreditService&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;br/&gt;&lt;span class=&quot;java_plain&quot;&gt;defined &lt;/span&gt;&lt;span class=&quot;java_keyword&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;PayPal&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;br/&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;br/&gt;&lt;span class=&quot;java_plain&quot;&gt;scala&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;span class=&quot;java_keyword&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt; paypalPayment &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;span class=&quot;java_type&quot;&gt;RealPayment&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;PayPal&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;java_literal&quot;&gt;&quot;bar&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;span class=&quot;java_operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;AuthService&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_operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Date&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_operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;java_plain&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;&lt;br/&gt;&lt;span class=&quot;java_plain&quot;&gt;paypalPayment&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;span class=&quot;java_separator&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;AuthService&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt; java&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;util&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Date&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_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;span class=&quot;java_operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;RealPayment&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt; &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;span class=&quot;java_operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;function&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;br/&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;br/&gt;&lt;/code&gt;&lt;br/&gt;&lt;/div&gt;&lt;br/&gt;&lt;br/&gt;Note how the Scala interpreter now treats &lt;code&gt;paypalPayment&lt;/code&gt; as a function from &lt;code&gt;(AuthService, java.util.Date, Int) =&amp;gt; RealPayment&lt;/code&gt;. The underscore acts as the placeholder that helps Scala create a new function object with only those parameters. In our case the new functional takes only three parameters for whom we used the placeholder syntax. From your application point of view what it means is that we have closed the abstraction partially by finalizing the provider for the &lt;code&gt;CreditService&lt;/code&gt; implementation and left the rest of it open. Isn't this precisely what the Guice injector was doing above injecting some of the objects at module startup ?&lt;br/&gt;&lt;br/&gt;Within the module I can now invoke &lt;code&gt;paypalPayment&lt;/code&gt; with only the 3 parameters that are still open ..&lt;br/&gt;&lt;br/&gt;&lt;div&gt;&lt;br/&gt;&lt;code&gt;&lt;span class=&quot;java_plain&quot;&gt;scala&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;span class=&quot;java_keyword&quot;&gt;case&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;java_keyword&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;DefaultAuth&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;provider&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;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;span class=&quot;java_keyword&quot;&gt;extends&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;AuthService&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;br/&gt;&lt;span class=&quot;java_plain&quot;&gt;defined &lt;/span&gt;&lt;span class=&quot;java_keyword&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;DefaultAuth&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;br/&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;br/&gt;&lt;span class=&quot;java_plain&quot;&gt;scala&lt;/span&gt;&lt;span class=&quot;java_operator&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt; paypalPayment&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;DefaultAuth&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;java_literal&quot;&gt;&quot;foo&quot;&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt; java&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;util&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Calendar&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;getInstance&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;getTime&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_literal&quot;&gt;10000&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;br/&gt;&lt;span class=&quot;java_plain&quot;&gt;res0&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;span class=&quot;java_type&quot;&gt;RealPayment&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt; &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;span class=&quot;java_type&quot;&gt;RealPayment&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;PayPal&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;foo&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;DefaultAuth&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;foo&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Sun&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Feb&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;java_literal&quot;&gt;28&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;java_literal&quot;&gt;15&lt;/span&gt;&lt;span class=&quot;java_operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;java_literal&quot;&gt;22&lt;/span&gt;&lt;span class=&quot;java_operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;java_literal&quot;&gt;01&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt; IST &lt;/span&gt;&lt;span class=&quot;java_literal&quot;&gt;2010&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;java_literal&quot;&gt;10000&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;br/&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;br/&gt;&lt;/code&gt;&lt;br/&gt;&lt;/div&gt;&lt;br/&gt;&lt;br/&gt;Now suppose for some modules I would like to close the abstraction for the &lt;code&gt;AuthService&lt;/code&gt; as well in addition to freezing &lt;code&gt;PayPal&lt;/code&gt; as the &lt;code&gt;CreditService&lt;/code&gt;. One alternative will be to define another abstraction as &lt;code&gt;paypalPayment&lt;/code&gt; through partial application of &lt;code&gt;RealPayment&lt;/code&gt; where we close both the parameters. A better option will be to reuse the &lt;code&gt;paypalPayment&lt;/code&gt; abstraction and use explicit function currying. Like ..&lt;br/&gt;&lt;br/&gt;&lt;div&gt;&lt;br/&gt;&lt;code&gt;&lt;span class=&quot;java_plain&quot;&gt;scala&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;span class=&quot;java_keyword&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt; paypalPaymentCurried &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;span class=&quot;java_type&quot;&gt;Function&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;curried&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;paypalPayment&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;br/&gt;&lt;span class=&quot;java_plain&quot;&gt;paypalPaymentCurried&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;span class=&quot;java_separator&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;AuthService&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_operator&quot;&gt;=&amp;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;java&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;util&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Date&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_operator&quot;&gt;=&amp;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_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;span class=&quot;java_operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;RealPayment&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt; &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;span class=&quot;java_operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;function&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;br/&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;br/&gt;&lt;/code&gt;&lt;br/&gt;&lt;/div&gt;&lt;br/&gt;&lt;br/&gt;and closing it partially using the &lt;code&gt;DefaultAuth&lt;/code&gt; implementation ..&lt;br/&gt; &lt;br/&gt;&lt;div&gt;&lt;br/&gt;&lt;code&gt;&lt;span class=&quot;java_plain&quot;&gt;scala&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;span class=&quot;java_keyword&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt; paypalPaymentWithDefaultAuth &lt;/span&gt;&lt;span class=&quot;java_operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt; paypalPaymentCurried&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;DefaultAuth&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;java_literal&quot;&gt;&quot;foo&quot;&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;br/&gt;&lt;span class=&quot;java_plain&quot;&gt;paypalPaymentWithDefaultAuth&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;span class=&quot;java_separator&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;java&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;util&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Date&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_operator&quot;&gt;=&amp;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_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;span class=&quot;java_operator&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;RealPayment&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt; &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;span class=&quot;java_operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;function&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;br/&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;br/&gt;&lt;/code&gt;&lt;br/&gt;&lt;/div&gt;&lt;br/&gt;&lt;br/&gt;The rest of the module can now treat this as an abstraction that uses &lt;code&gt;PayPal&lt;/code&gt; for &lt;code&gt;CreditService&lt;/code&gt; and &lt;code&gt;DefaultAuth&lt;/code&gt; for &lt;code&gt;AuthService&lt;/code&gt;. Like Guice we can have hierarchies of modules that injects these settings and publishes a more specialized abstraction to downstream clients.&lt;div class=&quot;blogger-post-footer&quot;&gt;&lt;img alt=&quot;&quot; height=&quot;1&quot; src=&quot;https://blogger.googleusercontent.com/tracker/22587889-5773558157295330139?l=debasishg.blogspot.com&quot; width=&quot;1&quot;/&gt;&lt;/div&gt;&lt;/div&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;</description>
         <author>Debasish</author>
         <guid isPermaLink="false">tag:blogger.com,1999:blog-22587889.post-5773558157295330139</guid>
         <pubDate>Sun, 28 Feb 2010 07:20:00 -0800</pubDate>
      </item>
      <item>
         <title>Actor Styles... OO, Functional or Blended...</title>
         <link>http://suereth.blogspot.com/2010/02/actor-styles-oo-functional-or-blended.html</link>
         <description>&lt;div&gt;I had the privilege of a free cup of coffee with David Pollak when I was in the bay area recently. He was describing his new goat-rodeo library when he showed me the interface for his &quot;Worker&quot; class (which is similar to an Actor class). He made a comment about &quot;being annoyed with writing all those case statements&quot; in actors, and so figured out a method using reflection and method overloading to define an actor that merely responds to all messages it receives. I'm sure there was more to it than this, but I believe he's partially solved an OO issue I complained about in an &lt;a rel=&quot;nofollow&quot; target=&quot;_blank&quot; href=&quot;http://suereth.blogspot.com/2009/04/partial-functions-inheritance.html&quot;&gt;earlier blog about partial functions and inheritance&lt;/a&gt;. Let's take a bit to remember that solution to the issue.&lt;br/&gt;&lt;br/&gt;The crux of the problem is the blend of OO and functional. Functions are composable. You can have one function take another and compose them all day long. Objects are also composable. One object can take another object and utilize it all day long. Methods (functions attached to objects) are not as composable. Scala traits give you means to compose with them, but all your composition is done at instantiation time (i.e. mixing in traits, inheritance, etc.). Now we come to the main issue. An Actor is an abstract class that you subclass. This leads one to compose behavior through traits (similar to other OO-style programs in Scala), but the method of doing so is a bit strange. We start with our &quot;FinalMixinActor&quot; class&lt;br/&gt;&lt;br/&gt;&lt;pre class=&quot;brush: scala&quot;&gt;trait BaseActor extends Actor {&lt;br/&gt;&lt;br/&gt; def makeMessageHandler() : PartialFunction[Any,Unit] = {&lt;br/&gt; case x =&amp;gt; //Unhandled message, assume design flaw!&lt;br/&gt; error(&quot;Unknown message: &quot; + x) &lt;br/&gt; }&lt;br/&gt;}&lt;br/&gt;trait FinalActor extends BaseActor[T] {&lt;br/&gt; lazy val messageHandler = makeMessageHandler()&lt;br/&gt; override def act() {&lt;br/&gt; Actor.loop {&lt;br/&gt; react messageHandler&lt;br/&gt; }&lt;br/&gt; }&lt;br/&gt;}&lt;br/&gt;&lt;/pre&gt;&lt;br/&gt;These two classes provide us with a way to compose actors. We make sure all behavior extends the BaseActor, and we make sure when creating an actor that &quot;FinalActor&quot; gets mixed in *last* in the inheritance linearization. This means that when FinalActor calls &quot;makeMessageHandler&quot;, all the standard OO inheritance has a chance to kick and pull the behaviors you've composed. Let's see an example of composing an actor with two functionalities....&lt;br/&gt;&lt;br/&gt;&lt;pre class=&quot;brush: scala&quot;&gt;case class Ping()&lt;br/&gt;case class Pong()&lt;br/&gt;&lt;br/&gt;trait PingPongBehavior extends BaseActor {&lt;br/&gt; def makeMessageHandler() : PartialFunction[Any,Unit] = {&lt;br/&gt; val myBehavior : PartialFunction[Any,Unit] = {&lt;br/&gt; case Ping() =&amp;gt; sender ! Pong()&lt;br/&gt; }&lt;br/&gt; return myBehavior orElse super.makeMessageHandler()&lt;br/&gt; }&lt;br/&gt;}&lt;br/&gt;&lt;/pre&gt;&lt;br/&gt;This is our &quot;PingPong&quot; behavior actor for sending/receiving Ping/Pong messages. As you can see in the makeMessageHandler... we're manually converting functional composition into inheritance composition. It does give us flexibility if we'd like our behavior to run last. Let's create our second piece of behavior...&lt;br/&gt;&lt;br/&gt;&lt;br/&gt;&lt;pre class=&quot;brush: scala&quot;&gt;case class Shutdown()&lt;br/&gt;&lt;br/&gt;trait RemoteControlBehavior extends BaseActor {&lt;br/&gt; def makeMessageHandler() : PartialFunction[Any,Unit] = {&lt;br/&gt; val myBehavior : PartialFunction[Any,Unit] = {&lt;br/&gt; case Shutdown() =&amp;gt; Actor.exit&lt;br/&gt; }&lt;br/&gt; return myBehavior orElse super.makeMessageHandler()&lt;br/&gt; }&lt;br/&gt;}&lt;br/&gt;&lt;/pre&gt;&lt;br/&gt;This is a simple actor that just lets us send a shutdown command to an actor instead of relying on the actor knowing when to shut itself down (or linking it). Not necessarily useful, but it will help illustrate our next point. Let's create an actor that mixes in both these functionalities.&lt;br/&gt;&lt;br/&gt;&lt;br/&gt;&lt;pre class=&quot;brush: scala&quot;&gt;val myActor = new PingPongBehavior with RemoteControlBehavior with FinalActor&lt;br/&gt;myActor.start&lt;br/&gt;&lt;/pre&gt;&lt;br/&gt;Notice how we *have* to mix in FinalActor last so the composition of makeMessageHandler is done correctly. This also prevents other actors from adding behavior in their act methods....&lt;br/&gt;&lt;br/&gt;Anyway, it's a decent method of allowing mixin behavior of actors. It doesn't have much type safety, but the Scala standard actors library actually makes you jump through some hoops to get type-safety anyway (plus all their samples are against Any... so it seems they tend to encourage this...)&lt;br/&gt;&lt;br/&gt;&lt;br/&gt;Now for a look at how Goat-Rodeo helps solve this. First, Goat-Rodeo uses a type-safe message passing API, including knowing whether a message is allowed a response, and the type of that response. Let's take a look at a boiled down version of the &lt;a rel=&quot;nofollow&quot; target=&quot;_blank&quot; href=&quot;https://www.assembla.com/wiki/show/goat_rodeo/Modeling_a_Skitter_User&quot;&gt;twitter-clone sample on goat-rodeo's wiki&lt;/a&gt;. I've used elipses to ignore implementation details of Goat Rodeo:&lt;br/&gt;&lt;br/&gt;&lt;br/&gt;&lt;pre class=&quot;brush: scala&quot;&gt;class UserWorker(...) extends WorkerImpl[..., UserMsg](...) {&lt;br/&gt; /**&lt;br/&gt; * handle the Follow message&lt;br/&gt; */&lt;br/&gt; def doFollow(msg: Follow) {&lt;br/&gt; ...&lt;br/&gt; }&lt;br/&gt;}&lt;br/&gt;&lt;/pre&gt;&lt;br/&gt;As you can see, it's just a method. Goat-Rodeo looks for methods with &quot;do&quot; or &quot;handle&quot; in the name and constructs a PartialFunction for the actor-behavior. The &lt;pre&gt;Follow&lt;/pre&gt;class must be a subclass of &lt;pre&gt;UserMsg&lt;/pre&gt;as Goat-Rodeo is also strongly typed. Because &lt;pre&gt;doFollow&lt;/pre&gt;returns Unit, goat-rodeo knows that there is no return expected with this message. If you'd like to handle different messages, simple use method overloading. The best part, if we'd like to use OO inheritance to compose behavior, we can do so directly now...&lt;br/&gt;&lt;br/&gt;&lt;pre class=&quot;brush: scala&quot;&gt;class UserWorkerPlus(...) extends UserWorker(...) {&lt;br/&gt; /**&lt;br/&gt; * handle the Follow message&lt;br/&gt; */&lt;br/&gt; def doFollow(msg: Follow) {&lt;br/&gt; println(&quot;ZOMG!!!!!!!!!!!!!!!!!!&quot;)&lt;br/&gt; super.doFollow(msg)&lt;br/&gt; }&lt;br/&gt;}&lt;br/&gt;&lt;/pre&gt;&lt;br/&gt;That should be a lot more comfortable to OO developers. With some type trickery you could even create the &quot;management&quot; behavior traits and mix them into &quot;workers&quot; as needed. It still leaves some things to be desired. e.g. when I compose partial functions in a purely functional sense, I can have the type-checker automatically infer what the new signature of a partial function should be after an orElse. With both of these methods, that is not possible. For the pure functional users, I'd recommend creating actors using something akin to this function (or you should just use scalaz, as their actors library is very well done):&lt;br/&gt;&lt;br/&gt;&lt;pre class=&quot;brush: scala&quot;&gt;import scala.actors.Actor&lt;br/&gt;import scala.actors.Channel&lt;br/&gt;&lt;br/&gt;class ChanneledActor[T](f : PartialFunction[T,Unit]) extends Actor {&lt;br/&gt; val typedChannel = new Channel[T](this)&lt;br/&gt; def act() {&lt;br/&gt; Actor.loop {&lt;br/&gt; typedChannel react f&lt;br/&gt; }&lt;br/&gt; }&lt;br/&gt;}&lt;br/&gt;&lt;br/&gt;def makeActor[T, U](f : PartialFunction[T,U]) : Channel[T] = {&lt;br/&gt; val a = new ChanneledActor(f andThen ( x =&amp;gt; () )) //Ignore results...&lt;br/&gt; a.start&lt;br/&gt; a.typedChannel&lt;br/&gt;}&lt;br/&gt;&lt;/pre&gt;&lt;br/&gt;This will let you compose partial functions in a functional style and let the type-checker figure out the signature. Finally when you create an Actor with the makeActor method, it will return the type-safe channel to send messages with. Here's an example interpreter session:&lt;br/&gt;&lt;br/&gt;&lt;pre class=&quot;brush: scala&quot;&gt;scala&amp;gt; makeActor[Int,Unit] { case x : Int =&amp;gt; println(&quot;HAI&quot;) } &lt;br/&gt;res3: scala.actors.Channel[Int] = scala.actors.Channel@154f77b&lt;br/&gt;&lt;br/&gt;scala&amp;gt; res3 ! &quot;HAI&quot;&lt;br/&gt;&amp;lt;console&amp;gt;:10: error: type mismatch;&lt;br/&gt; found : java.lang.String(&quot;HAI&quot;)&lt;br/&gt; required: Int&lt;br/&gt; res3 ! &quot;HAI&quot;&lt;br/&gt; ^&lt;br/&gt;&lt;br/&gt;scala&amp;gt; res3 ! 5 &lt;br/&gt;&lt;br/&gt;scala&amp;gt; HAI&lt;br/&gt;&lt;/pre&gt;&lt;br/&gt;&lt;br/&gt;Anyway, if you truly desire a functional approach to actors, you should look into Scalaz, as they've done it right. If you desire a hybrid or an OO approach, I hope I've helped outline the details for you. I'm very interested to see how all these libraries evolve over time, and kudos to David Pollak for goat-rodeo.&lt;div class=&quot;blogger-post-footer&quot;&gt;&lt;img alt=&quot;&quot; height=&quot;1&quot; src=&quot;https://blogger.googleusercontent.com/tracker/1673113361032868171-735483212096690274?l=suereth.blogspot.com&quot; width=&quot;1&quot;/&gt;&lt;/div&gt;&lt;/div&gt;</description>
         <author>J. Suereth</author>
         <guid isPermaLink="false">tag:blogger.com,1999:blog-1673113361032868171.post-735483212096690274</guid>
         <pubDate>Sun, 28 Feb 2010 07:12:00 -0800</pubDate>
      </item>
      <item>
         <title>SBT and Test Arguments</title>
         <link>http://jackcoughonsoftware.blogspot.com/2010/02/sbt-and-test-arguments.html</link>
         <description>&lt;div&gt;&lt;br/&gt;&lt;br/&gt;If you want to pass arguments to tests in SBT, you've come to the right place. First know this - it only works in certain situations, it's a bit crufty, and its subject to change. But I'll try to keep this page up to date if it does change.&lt;br/&gt;&lt;br/&gt;Another important note, if you want to pass arguments at the sbt command line, you can only do it for test-quick, and test-only (I think). It doesn't work for the default &quot;test&quot; command. This is a bit unfortunate, but there is a workaround (not a very good one, but at least it exists).&lt;br/&gt;&lt;br/&gt;A final note: it works with ScalaTest and ScalaCheck. I haven't tested any of this with Specs, and not sure if Eric has implemented argument handling.&lt;br/&gt;&lt;br/&gt;&lt;h4&gt;Passing Args to ScalaTest from the Command Line&lt;/h4&gt;&lt;br/&gt;Let's say you have this little ScalaTest class that uses both params and tags:&lt;br/&gt;&lt;pre&gt;&lt;br/&gt;import org.scalatest.fixture.FixtureFunSuite&lt;br/&gt;import org.scalatest.Tag&lt;br/&gt;&lt;br/&gt;class WickedCoolTest extends FixtureFunSuite{&lt;br/&gt; type FixtureParam = Map[String,Any]&lt;br/&gt; override def withFixture(test: OneArgTest) {&lt;br/&gt; test(test.configMap)&lt;br/&gt; }&lt;br/&gt; test(&quot;1&quot;, Tag(&quot;dood1&quot;)){ conf =&amp;gt; println(&quot;dood1: &quot; + conf) }&lt;br/&gt; test(&quot;2&quot;, Tag(&quot;dood2&quot;)){ conf =&amp;gt; println(&quot;dood2: &quot; + conf) }&lt;br/&gt; test(&quot;3&quot;, Tag(&quot;dood3&quot;)){ conf =&amp;gt; println(&quot;dood3: &quot; + conf) }&lt;br/&gt;}&lt;/pre&gt;&lt;br/&gt;To run all tests in in this class use:&lt;br/&gt;&lt;pre&gt;&lt;br/&gt;test-only WickedCoolTest&lt;br/&gt;&lt;/pre&gt;&lt;br/&gt;To run only tests tagged with &quot;dood1&quot; use:&lt;br/&gt;&lt;pre&gt;&lt;br/&gt;test-only WickedCoolTest -- -n dood1&lt;br/&gt;&lt;/pre&gt;&lt;br/&gt;To run tests tagged with dood1 or dood2, use:&lt;br/&gt;&lt;pre&gt;&lt;br/&gt;test-only WickedCoolTest -- -n &quot;dood1 dood2&quot;&lt;br/&gt;&lt;/pre&gt;&lt;br/&gt;To run all tests except for tests tagged with dood2, run:&lt;br/&gt;&lt;pre&gt;&lt;br/&gt;test-only WickedCoolTest -- -l dood2&lt;br/&gt;&lt;/pre&gt;&lt;br/&gt;To run all tests except for tests tagged with dood2 dood3, run:&lt;br/&gt;&lt;pre&gt;&lt;br/&gt;test-only WickedCoolTest -- -l &quot;dood2 dood3&quot;&lt;br/&gt;&lt;/pre&gt;&lt;br/&gt;To pass configuration parameters to a test, use:&lt;br/&gt;&lt;pre&gt;&lt;br/&gt;test-only WickedCoolTest -- -Danswer=42&lt;br/&gt;&lt;/pre&gt;&lt;br/&gt;To pass many config params, add more -D's:&lt;br/&gt;&lt;pre&gt;&lt;br/&gt;test-only WickedCoolTest -- -Danswer=42 -DrealAnswer=54&lt;br/&gt;&lt;/pre&gt;&lt;br/&gt;Tags and parameters can be used in combination, like so:&lt;br/&gt;&lt;pre&gt;&lt;br/&gt;test-only WickedCoolTest -- -n &quot;dood dood2&quot; -Dhey=you -Dm=f&lt;br/&gt;&lt;/pre&gt;&lt;br/&gt;This runs only tests tagged with dood or dood2 and produces the following output:&lt;br/&gt;&lt;br/&gt;dood: Map(hey -&amp;gt; you, m -&amp;gt; f)&lt;br/&gt;dood2: Map(hey -&amp;gt; you, m -&amp;gt; f)&lt;br/&gt;&lt;br/&gt;&lt;h4&gt;Passing Args to ScalaCheck from the Command Line&lt;/h4&gt;&lt;br/&gt;I'll spare you the entire example and just get to the point here.&lt;br/&gt;&lt;br/&gt;To set the minimum number of successful tests, use '-s'&lt;br/&gt;&lt;pre&gt;&lt;br/&gt;&amp;gt; test-only *IntParallelArrayCheck -- -s 5000&lt;br/&gt;...&lt;br/&gt;[info] == scala.collection.parallel.mutable.IntParallelArrayCheck ==&lt;br/&gt;[info] OK, passed 5000 tests.&lt;br/&gt;&lt;/pre&gt;&lt;br/&gt;&lt;br/&gt;The rest of the examples are very similar. Consult ScalaCheck itself for further documentation. &lt;br/&gt;&lt;ul&gt;&lt;br/&gt;&lt;li&gt;-s (minSuccessfulTests): Number of tests that must succeed in order to pass a property&lt;br/&gt;&lt;/li&gt;&lt;li&gt;-d (maxDiscardedTests): Number of tests that can be discarded before ScalaCheck stops testing a property&lt;br/&gt;&lt;/li&gt;&lt;li&gt;-n (minSize): Minimum data generation size&lt;br/&gt;&lt;/li&gt;&lt;li&gt;-x (maxSize): Maximum data generation size&lt;br/&gt;&lt;/li&gt;&lt;li&gt;-w (workers): Number of threads to execute in parallel for testing&lt;br/&gt;&lt;/li&gt;&lt;li&gt;-z (wrkSize): Amount of work each thread should do at a time&lt;br/&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br/&gt;&lt;h4&gt;Default Arguments&lt;/h4&gt;&lt;br/&gt;Maybe you want to pass default arguments to your test framework of choice. That is, you want to pass the same arguments every time you run your tests (and still be able to pass more dynamically, if you wish). You can do this too, by adding elements to 'testOptions'. Doing so will take effect when you run sbt test, sbt test-only, sbt test-quick, etc. &lt;br/&gt;&lt;br/&gt;Let's say you want the minimum number of ScalaCheck passing tests to be 5000 every time you run ScalaCheck. Here is how you do that: &lt;br/&gt;&lt;pre&gt;&lt;br/&gt;override def testOptions = &lt;br/&gt; super.testOptions ++ &lt;br/&gt; Seq(TestArgument(TestFrameworks.ScalaCheck, &quot;-s&quot;, &quot;5000&quot;)) &lt;br/&gt;&lt;/pre&gt;&lt;br/&gt;&lt;br/&gt;Or maybe you only ever want to run your fast ScalaTest tests.&lt;br/&gt;&lt;pre&gt;&lt;br/&gt;override def testOptions = &lt;br/&gt; super.testOptions ++ &lt;br/&gt; Seq(TestArgument(TestFrameworks.ScalaTest, &quot;-n&quot;, &quot;fast&quot;)) &lt;br/&gt;&lt;/pre&gt;&lt;br/&gt;&lt;h4&gt;Custom Test Tasks&lt;/h4&gt;&lt;br/&gt;Maybe you have some test that you run all the time. At the sbt command line, instead of saying &amp;gt; test-quick blah.blah.Blah, you just want to say: &amp;gt; blah.&lt;br/&gt;&lt;br/&gt;You can do that too, and you can pass args to it dynamically, and you get the default arguments as well. You'll have to take the following code and put it into your sbt file. It's ugly, I know, but the results are nice. &lt;br/&gt;&lt;pre&gt;&lt;br/&gt;lazy val blah = singleTestTask(&quot;blah.blah.Blah&quot;)&lt;br/&gt; &lt;br/&gt;private def singleTestTask(className: String) = task { args =&amp;gt;&lt;br/&gt; defaultTestTask(TestFilter(_ == className) :: &lt;br/&gt; testOptions.toList ::: ScalaTestArgs(args))&lt;br/&gt;}&lt;br/&gt;&lt;br/&gt;private def newScalaTestArg(l: String*) = &lt;br/&gt; TestArgument(TestFrameworks.ScalaTest, l:_*)&lt;br/&gt;&lt;br/&gt;private def ScalaTestArgs(args: Seq[String]): List[TestArgument] = {&lt;br/&gt; def KVArgs(args: Seq[String]): TestArgument = &lt;br/&gt; newScalaTestArg(args.map(&quot;-D&quot; + _):_*)&lt;br/&gt; def tagsFromArgs(tags: Seq[String]): List[TestArgument] = {&lt;br/&gt; if (tags.isEmpty) Nil else &lt;br/&gt; List(newScalaTestArg(&quot;-n&quot;, tags.mkString(&quot; &quot;)))&lt;br/&gt; }&lt;br/&gt; val (kvs, tags) = args.partition(_.contains(&quot;=&quot;))&lt;br/&gt; KVArgs(kvs.toSeq) :: tagsFromArgs(tags.toSeq)&lt;br/&gt;}&lt;/pre&gt;&lt;br/&gt;&lt;h4&gt;Subclasses&lt;/h4&gt;&lt;br/&gt;Finally, maybe you want to set up tasks like 'test-fast' and 'test-slow' which only run your fast and slow tests. Let's imagine that you've created a trait called blah.blah.SlowTest and all of your slow tests extend that trait. Tests that don't extend SlowTest are considered to be in the fast group. With the code below, at the sbt command line you'll be able to say &amp;gt; test-fast, and &amp;gt; test-slow. &lt;br/&gt;&lt;br/&gt;Again, it's a bit ugly to put this stuff in your sbt project file, but it works for now, until I come up with a better plan :)&lt;br/&gt;&lt;pre&gt;&lt;br/&gt;lazy val testSlow = &lt;br/&gt; runSubclassesOf(&quot;org.nlogo.util.SlowTest&quot;)&lt;br/&gt;lazy val testFast = &lt;br/&gt; runEverythingButSubclassesOf(&quot;org.nlogo.util.SlowTest&quot;)&lt;br/&gt;&lt;br/&gt;private def runSubclassesOf(className: String) = {&lt;br/&gt; val subclass: Boolean =&amp;gt; Boolean = x =&amp;gt; x&lt;br/&gt; subclassTest(className, subclass)&lt;br/&gt;}&lt;br/&gt; &lt;br/&gt;private def runEverythingButSubclassesOf(className: String) = {&lt;br/&gt; val notSubclass: Boolean =&amp;gt; Boolean = x =&amp;gt; ! x&lt;br/&gt; subclassTest(className, notSubclass)&lt;br/&gt;}&lt;br/&gt;&lt;br/&gt;private def subclassTest(className: String, &lt;br/&gt; subclassCheck: Boolean =&amp;gt; Boolean) = task { &lt;br/&gt; args =&amp;gt;&lt;br/&gt; lazy val jars = &lt;br/&gt; testClasspath.get.toList.map(_.asURL).toArray[java.net.URL]&lt;br/&gt; lazy val loader = &lt;br/&gt; new java.net.URLClassLoader(jars,buildScalaInstance.loader)&lt;br/&gt; def clazz(name: String) = Class.forName(name, false, loader)&lt;br/&gt; lazy val superClass = clazz(className)&lt;br/&gt; def filter = &lt;br/&gt; TestFilter(c =&amp;gt; &lt;br/&gt; subclassCheck(superClass.isAssignableFrom(clazz(c))))&lt;br/&gt; defaultTestTask&lt;br/&gt; (filter :: testOptions.toList ::: ScalaTestArgs(args))&lt;br/&gt;}&lt;/pre&gt;&lt;br/&gt;&lt;br/&gt;&lt;h4&gt;Yeah...&lt;/h4&gt;&lt;br/&gt;Let me know if you have any issues with any of this, I'll be happy to help. I put it together pretty quickly. If there are any glaring errors or omissions, please tell.&lt;div class=&quot;blogger-post-footer&quot;&gt;&lt;img alt=&quot;&quot; height=&quot;1&quot; src=&quot;https://blogger.googleusercontent.com/tracker/34927953-2392208901439516075?l=jackcoughonsoftware.blogspot.com&quot; width=&quot;1&quot;/&gt;&lt;/div&gt;&lt;/div&gt;</description>
         <author>Jack Cough</author>
         <guid isPermaLink="false">tag:blogger.com,1999:blog-34927953.post-2392208901439516075</guid>
         <pubDate>Wed, 24 Feb 2010 09:01:00 -0800</pubDate>
      </item>
      <item>
         <title>DSL : Grow your syntax on top of a clean semantic model</title>
         <link>http://debasishg.blogspot.com/2010/02/dsl-grow-your-syntax-on-top-of-clean.html</link>
         <description>&lt;div&gt;A DSL primarily has two components - &lt;i&gt;a semantic model&lt;/i&gt; that abstracts the underlying domain and a &lt;i&gt;linguistic abstraction&lt;/i&gt; on top that speaks the dialect of the user. The semantic model is the model of the domain where you can apply all the principles of DDD that Eric Evans espouses. And the linguistic abstraction is a thin veneer on top of the underlying model. The more well abstracted your model is, easier will be the construction of the layer on top of it. Here's a general architecture of a DSL engineering stack :-&lt;br/&gt;&lt;br/&gt;&lt;a rel=&quot;nofollow&quot; target=&quot;_blank&quot; href=&quot;http://2.bp.blogspot.com/_r-NJO1NMiu4/S4IJvohPhGI/AAAAAAAAAKc/Xx_hy4ZBCXI/s1600-h/ext_dsl_10.gif&quot;&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; id=&quot;BLOGGER_PHOTO_ID_5440922013752329314&quot; src=&quot;http://2.bp.blogspot.com/_r-NJO1NMiu4/S4IJvohPhGI/AAAAAAAAAKc/Xx_hy4ZBCXI/s400/ext_dsl_10.gif&quot; style=&quot;display:block;margin:0px auto 10px;text-align:center;cursor:pointer;cursor:hand;width:400px;height:300px;&quot;/&gt;&lt;/a&gt;&lt;br/&gt;&lt;br/&gt;It's interesting to observe that the two components of the stack evolve somewhat orthogonally.&lt;br/&gt;&lt;br/&gt;&lt;b&gt;The Semantic Model evolves Bottom Up&lt;/b&gt;&lt;br/&gt;&lt;br/&gt;The semantic model usually evolves in a bottom up fashion - larger abstractions are formed from smaller abstractions using principles of composition. It can be through composition of traits or objects or it can be through composition of functions as well. How beautiful your compositions can be depends a lot on the language you use. But it's important that the semantic model also speaks the language of the domain. &lt;br/&gt;&lt;br/&gt;Here's an example code snippet from my upcoming book &lt;a rel=&quot;nofollow&quot; target=&quot;_blank&quot; href=&quot;http://www.manning.com/ghosh&quot;&gt;DSLs In Action&lt;/a&gt; that models the business rules for a trading DSL. When you do a trade on a stock exchange you get charged a list of tax and fee components depending on the market where you execute the trade. The following snippet models a business rule using Scala that finds out the list of applicable tax/fee heads for a trade ..&lt;br/&gt;&lt;br/&gt;&lt;code&gt;&lt;span class=&quot;java_keyword&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;TaxFeeRulesImpl&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;java_keyword&quot;&gt;extends&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;TaxFeeRules&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;&lt;br/&gt;&lt;span class=&quot;java_plain&quot;&gt; override &lt;/span&gt;&lt;span class=&quot;java_keyword&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt; forTrade&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;trade&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;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_operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;java_plain&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;TaxFee&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_operator&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;&lt;br/&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;forHKG orElse &lt;/span&gt;&lt;br/&gt;&lt;span class=&quot;java_plain&quot;&gt; forSGP orElse &lt;/span&gt;&lt;br/&gt;&lt;span class=&quot;java_plain&quot;&gt; forAll&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;java_plain&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;market&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;br/&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;&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; &lt;/span&gt;&lt;span class=&quot;java_keyword&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt; forHKG&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;span class=&quot;java_type&quot;&gt;PartialFunction&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Market&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_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;TaxFee&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_operator&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; &lt;/span&gt;&lt;br/&gt;&lt;span class=&quot;java_plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;java_keyword&quot;&gt;case&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt; HKG &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; &lt;/span&gt;&lt;span class=&quot;java_comment&quot;&gt;// in real life these can come from a database&lt;/span&gt;&lt;br/&gt;&lt;span class=&quot;java_plain&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;TradeTax&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_type&quot;&gt;Commission&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_type&quot;&gt;Surcharge&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;br/&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;&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; &lt;/span&gt;&lt;span class=&quot;java_keyword&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt; forSGP&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;span class=&quot;java_type&quot;&gt;PartialFunction&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Market&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_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;TaxFee&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_operator&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;&lt;br/&gt;&lt;span class=&quot;java_plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;java_keyword&quot;&gt;case&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt; SGP &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; &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;TradeTax&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_type&quot;&gt;Commission&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_type&quot;&gt;Surcharge&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt; VAT&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;br/&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;&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; &lt;/span&gt;&lt;span class=&quot;java_keyword&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt; forAll&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;span class=&quot;java_type&quot;&gt;PartialFunction&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;java_type&quot;&gt;Market&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_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;TaxFee&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_operator&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;&lt;br/&gt;&lt;span class=&quot;java_plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;java_keyword&quot;&gt;case&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt; _ &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;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;TradeTax&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_type&quot;&gt;Commission&lt;/span&gt;&lt;span class=&quot;java_separator&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;br/&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;&lt;br/&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;br/&gt;&lt;span class=&quot;java_plain&quot;&gt; &lt;/span&gt;&lt;span class=&quot;java_comment&quot;&gt;//..&lt;/span&gt;&lt;br/&gt;&lt;span class=&quot;java_separator&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;br/&gt;&lt;span class=&quot;java_plain&quot;&gt;&lt;br/&gt;&lt;/code&gt;&lt;br/&gt;The method &lt;code&gt;forTrade&lt;/code&gt; clearly expresses the business rule, which reads almost as expressive as the English version ..&lt;br/&gt;&lt;br/&gt;&lt;i&gt;&quot;Get the Hong Kong specific list for trades executed on the Hong Kong market OR Get the Singapore specific list for trades executed on the Singapore market OR Get the most generic list valid for all other markets&quot;&lt;/i&gt;&lt;br/&gt;&lt;br/&gt;Note how Scala &lt;code&gt;PartialFunction&lt;/code&gt; s can be chained together to give the above model an expressive yet succinct syntax.&lt;br/&gt;&lt;br/&gt;&lt;b&gt;The Language Interface evolves Top Down&lt;/b&gt;&lt;br/&gt;&lt;br/&gt;Here you start with the domain user. What dialect does he use on the trading desk ? And then you try to build an interpreter around that which uses the services that the semantic model publishes. I call this thin layer of abstraction a &lt;b&gt;DSL Facade&lt;/b&gt; that sits between your DSL script and the underlying domain model and acts as the glue.&lt;br/&gt;&lt;br/&gt;It also depends a lot on the host language as to how you would like to implement the facade. With a language like Lisp, macros can come in very handy in designing an interpreter layer for the facade. And with macros you do bottom up programming, bending the host language to speak your dialect.&lt;br/&gt;&lt;br/&gt;When you are developing an external DSL, the EBNF rules that you specify act as the DSL Facade for growing your syntax. Within the rules you can use foreign code embedding to interact with your semantic model. &lt;br/&gt;&lt;br/&gt;In summary, when you design a DSL, the semantic model is as important as the dialect that it speaks. Having a well designed semantic model is an exercise in designing well-engineered abstractions. And as I mention in my book, the four qualities of good abstractions are &lt;i&gt;minimalism&lt;/i&gt;, &lt;i&gt;distillation&lt;/i&gt;, &lt;i&gt;extensibility&lt;/i&gt; and &lt;i&gt;composability&lt;/i&gt;.&lt;div class=&quot;blogger-post-footer&quot;&gt;&lt;img alt=&quot;&quot; height=&quot;1&quot; src=&quot;https://blogger.googleusercontent.com/tracker/22587889-8752276955215978661?l=debasishg.blogspot.com&quot; width=&quot;1&quot;/&gt;&lt;/div&gt;&lt;/div&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;</description>
         <author>Debasish</author>
         <guid isPermaLink="false">tag:blogger.com,1999:blog-22587889.post-8752276955215978661</guid>
         <pubDate>Sun, 21 Feb 2010 20:35:00 -0800</pubDate>
      </item>
   </channel>
</rss>
<!-- fe6.pipes.sp1.yahoo.com uncompressed/chunked Tue Mar  9 18:46:23 PST 2010 -->
