Planet Scala

March 09, 2010

Coderspiel

Curried and Confused

The best post on the internet got some sweet linkage again this week. You know, the one where Scala is too complex because you can express the sum of a list of numbers like so:

def sum(nums: List[Int]) = (0 /: nums) { _ + _ }

In the intervening years since that expression prompted a ‘verdict’ that Scala was too difficult for us regular folks, a lot of us have written a heck of a lot of Scala. We had to make implicit.ly just to keep up with the flow of Scala software that people are publishing these days. The pump is primed and the machinery has finally come to life. It’s an exciting time! For some.

But since it’s come up, let’s review that old /: scarecrow. What makes him so perplexing to newcomers? Some have blamed the use of non-letters in the name. Okay then, how much clearer would Joe BeanFactory find this definition?

def sum(nums: List[Int]) = (nums foldLeft 0) { _ + _ }

Not much really! Well, maybe the problem is the infix notation?

def sum(nums: List[Int]) = nums.foldLeft(0) { _ + _ }

Yeah, I don’t know. At this point our rhetorical corporate programmer has gone out for coffee. Perhaps he will be back later to decide which programming languages we’re all supposed to use for his convenience. Maybe he would rather express things like 1 + 1 as 1.plus(1) —or, maybe he just doesn’t give a Dilbert about orthogonality.

But seriously guys, I don’t think it’s the infix notation or the permitting of symbolic characters in syntactic symbols that make applications of /: difficult to grok at first. It’s the blessed currying. Setting off the first function application with parentheses prevents one’s uninitiated mind from cheaply translating it to a familiar series of parameters. Instead you have to recognize that the first function application returns a second function, which is applied to another set of parameters. It’s trippy.

Truth be told, this perplexed me at the time. But instead of rejecting the language for supporting functional constructs beyond my experience, I decided to keep learning—and I’m happy with how that has worked out. So it’s interesting that two years later, this particular functional freak-out has been ‘cited’ to brand Scala as overly complex (Perl-y) while a second thread of spite bemoans Scala’s insufficient promotion of currying.

It’s simple, you see. The currying in Scala makes innocent enterprise developers cry in their frappuccinos, while similar encounters in OCaml inspire a rapturous session of functional mind-blowing.

Well. At least you can learn Scala at your desk.

March 09, 2010 05:01 AM

March 08, 2010

Coderspiel

"The problem with modern software development is that it’s all Phase 2. The ubiquitious availability..."

“The problem with modern software development is that it’s all Phase 2. The ubiquitious availability of nearly-right-but-not-quite libraries and frameworks-that-do-it-all-for-you-except-that-actually-they-don’t wipes out the exhilaration of Phase 1, but leaves us with even more of the drudgery of Phase 2.”

- Whatever happened to programming? (Would replace ‘modern’ with ‘mainstream,’ but otherwise YES.)

March 08, 2010 05:03 PM

James Iry

Robert Fischer Finally Admits that Scala is Functional

Robert Fischer saysIf your language doesn’t lead people to re-discover point free programming at least in the small, then the language really isn’t taking function manipulation and functional language type conceptions seriously.Thus Fischer has finally recognized that Scala is functional. val double : Int => Int = 2 * // point free function definition List(1,2,3) map double /* removes one point

by James Iry (noreply@blogger.com) at March 08, 2010 03:40 PM

scala-lang.org

Scala Days - Jam Packed!

Jam packed with a schedule of advanced research and technical talks by some of the most knowlegable experts in the industry. Jam packed with Scala community know-how, over 80 people already registered. And jam packed with opportunities to meet and talk to other Scala developers. Scala Days 2010, 15-16 April, EPFL, Switzerland.

The full schedule of sessions, presenters and abstracts is now available. It is the time to take a look, decide what to see and get organised to come! At Scala Days you'll have a ball! Learn about the latest research, see how smart people are applying it in serious commercial applications, talk over your ideas, your company projects with other top notch people, people like Chris Conrad (from LinkedIn), Nathan Hamblen (SPDE contributor), Jorge Ortiz (Lift web framework committer) , Kunle Olukotun (Director Stanford PPP lab) Miles Sabin (Eclipse PlugIn), David Copeland (Industry Consultant), Heiko Seeberger (ScalaModules), Jonas Bonér (AKKA) and Josh Suereth (Maven-scala-plugin and more), and of course the entire EPFL Scala research group with Martin Odersky.

by bagwell at March 08, 2010 02:10 PM

Coderspiel

"Because aside from the static typing, all the language features that Odersky trots out already exist..."

“Because aside from the static typing, all the language features that Odersky trots out already exist in Perl.”

- Scala: Only Different from Perl in Fundamental Ways

March 08, 2010 01:01 AM

March 07, 2010

Jesper Nordenberg

Scala Stream Fusion and Specialization

Updated: Fixed code links and added view and stream benchmarks.

Inspired by the successful results of Haskell stream fusion (see Evolving Faster Haskell Programs (now with LLVM!) 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.

The goal of stream fusion is essentially to optimize code like this:

def scalaLibrarySum(a : Array[Int]) = a.map(i => i * 3 + 7).filter(i => (i % 10) == 0).foldLeft(0)(_ + _)

into code like this:

def mapFilterSumLoop(a : Array[Int]) = {
var i = 0
var r = 0

while (i < a.length) {
val v = a(i) * 3 + 7

if ((v % 10) == 0)
r += v

i += 1
}

r
}

If you run the scalaLibrarySum method in Scala it will create two intermediate arrays with the results of the map and filter operations. This is totally unnecessary for this calculation as the functions passed to filter and map 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 mapFilterSumLoop method works.

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 #3148 and #3149). So, the code below contain some specialization done by hand. Hopefully these bugs will be fixed so that the code can be fully generalized.

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.

Here's the definitions of the specialized functions and iterators I use in the benchmark below:

// Specialized Function1
trait Fn1[@specialized I, @specialized O] {
def apply(a : I) : O
}

// Specialized Function2
trait Fn2[@specialized I1, @specialized I2, @specialized O] {
def apply(a1 : I1, a2 : I2) : O
}

// Specialized iterator
trait SIterator[@specialized T] {
def hasMore : Boolean
def current : T
def next()
}

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:

class IntArrayIterator(a : Array[Int], var index : Int, endIndex : Int) extends SIterator[Int] {
def next() = index += 1
def current = a(index)
def hasMore = index < endIndex
}

// Optimally this would be: class FilterIterator[@specialized T](iter : SIterator[T], pred : Fn1[T, Boolean]) extends SIterator[T]
class FilterIterator(iter : SIterator[Int], pred : Fn1[Int, Boolean]) extends SIterator[Int] {
def hasMore = iter.hasMore

def next() = {
iter.next()
findNext()
}

def findNext() = {
while (iter.hasMore && !pred(iter.current))
iter.next()
}

def current = iter.current

findNext()
}

// Optimally this would be: class MapIterator[@specialized U][@specialized T](iter : SIterator[T], fn : Fn1[T, U]) extends SIterator[U]
class MapIterator(iter : SIterator[Int], fn : Fn1[Int, Int]) extends SIterator[Int] {
def next() = iter.next()
def current = fn(iter.current)
def hasMore = iter.hasMore
}

The fold function is straightforward and generic:

def fold[@specialized T, @specialized U] (iter : SIterator[T], fn : Fn2[U, T, U], v : U) = {
var r = v

while (iter.hasMore) {
r = fn(r, iter.current)
iter.next()
}

r
}

The map-filter-sum function can now be written using iterators:

def mapFilterSum(a : Array[Int]) = {
val filter = new Fn1[Int, Boolean] {def apply(a : Int) = (a % 10) == 0}
val map = new Fn1[Int, Int] {def apply(a : Int) = a * 3 + 7}
val s = new FilterIterator(new MapIterator(new IntArrayIterator(a, 0, a.length), map), filter)
fold(s, new Fn2[Int, Int, Int] {def apply(a1 : Int, a2 : Int) = a1 + a2}, 0)
}

The full iterator code can be found here. 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.

I've benchmarked four different implementations of the map-filter-sum calculation:

  • The while loop shown above

  • The while loop split up into map, filter and fold functions with intermediate array results passed between them

  • The version using specialized iterators

  • The Scala library implementation shown above

  • Same as Scala library function but with a view instead

  • Same as Scala library function but with a stream instead


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 "build 17.0-b10") and the flags "-server -XX:CompileThreshold=100" I get the following results:

Loop: (4990,-423600172)
Loop with intermediate arrays: (6690,-423600172)
Specialized iterators: (5367,-423600172)
Scala array: (46444,-423600172)
Scala view: (39625,-423600172)
Scala stream: (63210,-423600172)

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.

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.

The full benchmark code can be found here.

by Jesper Nordenberg (noreply@blogger.com) at March 07, 2010 01:02 PM

scala-lang.org

Screencast: Get Started with Scala 2.8 Beta 1

Getting started with any programming language is a daunting task, let alone getting your workstation in a state to create a non-trivial application. I spent a few days reading and trying many Scala tools and libraries. I use some of my findings in this screencast.

This screencast covers how to create a Scala console-based project. I use the simple-build-tool (sbt) to compile, run, test and then eclipsify a project. I briefly cover where to put source code, tests and how to use Eclipse to edit and debug the project. Click here to view to screencast.

by mgutz at March 07, 2010 09:00 AM

March 06, 2010

Coderspiel

Dispatch 0.7.2 - implicit.ly

Dispatch 0.7.2 - implicit.ly:

  • Use defaultCharset charset established by >\ for post and put
  • Handler chaining with the >+> operator, to use more than one handler against the same request with overlapping scopes
  • Multipart response handling with the >--> operator in dispatch-mime, to support Riak link-walking and map-reduce

March 06, 2010 03:37 PM

" Use defaultCharset charset established by >\ for POST « and PUT «< Handler..."

  • Use defaultCharset charset established by >\ for POST « and PUT «<
  • Handler chaining with the >+> operator, to use more than one handler against the same request with overlapping scopes
  • Multipart response handling with the >--> operator in dispatch-mime, to support Riak link-walking and map-reduce


- Dispatch 0.7.2 - implicit.ly

March 06, 2010 03:34 PM

David Pollak

Burn your TransLink card

I am outraged at the SF Muni and their adoption of the TransLink card.  Soon, there are certain bus lines that will only accept the TransLink card during rush hour... you won't be able to ride the bus except with one of these evil devices.

So, what's my outrage?

I spent most of today trying to obtain a TrankLink card for my kids.  I read on the TransLink site that the youth cards had to be obtained in person rather than at the normal retail outlets.  So, I took my kids downtown figuring I could buy a card at a Muni station, but no dice.  Most of the folks in the Muni stations had no clue.  Finally, somebody pointed me to Golden Gate Transit in the Ferry Building, so off we went to the Ferry Building.

We find the right booth to buy the pass.  Now, my kids are in kindergarten.  There's no question that they are youths and are elegible for youth discounts.  In fact, most Muni drivers say, "These kids look too young to pay."  My response is, "Muni rules say that 5 year olds pay and we follow rules in our family."

So, I ask for 2 youth passes.  The woman behind the counter says, "give me their birth certificates and we'll give you the youth passes."  I say, "that's a joke, right?  Look at them, do they look anywhere near 16 or 18 or whatever the cutoff age is?"  She responds, "Our policy is that we only issue youth cards with a birth certificate."

We've had to show the kids' birth certificates for their social security cards and for entrance to school.  We did not have to produce birth certificates for library cards, health insurance, so I'm wondering what is going on here.  I ask for a supervisor.

I talk to the supervisor and he tells me, "It is our policy that you need a birth certificate to obtain a youth TransLink card."  Me: "Why?  Do these kids look like they're too old for the card?"  Him: "We bond the birth certificate to the card to avoid fraud."  Me: "You what?"  Him: "We make sure that the discount card is associated with the person."  Me: "So, you're telling me that in order to get a discount, you guys are going to track the movements of my kids throughout the TransLink system?  How is this supposed to reduce fraud?"  Him: "That's our policy, sir."  Me: "Your policy is Orwellian and there's no way I'm letting you associate my kids' public transportation usage with their birth certificate.  Instead of trying to buy cards for the kids, I'm going to burn mine."

So, we walk away.  The supervisor comes running after me.  "Sir, your kids look young enough that they don't need to pay a fair anyway."  Me: "First, they have to pay on the SF Muni and we follow the rules so we pay.  Second, giving me a 'free-be' today don't mitigate your Big Brother policy of tracking my kids where-abouts."  Him: "I'm just trying to be helpful."  Me: "If you want to be helpful, go back to your boss and explain to him that this is the United States and we move freely in this country."

My kids asked me why I was cranky.  I said, "Because the bad people want to know where you go."  Kids: "We're not going anywhere bad, so why don't we want people to know where we've gone?"  Me: "It is our right as Americans to come and go as we please.  It's our right to be private.  But more importantly, historically, once the government starts tracking their citizens, especially their children, they will find a way to use the information against you.  It's not that we have anything to hide, it's that any time people want to always know, they are up to no good... they are bad people."

So, we walk through the Ferry Building and I see a guy with an EFF hat.  Now, this is San Francisco, so it's not unexpected to see some guy sporting an EFF hat.  I go up to him and say, "Are you a member of the EFF?  I was one of the first donors to the EFF so don't get freaked."  He says, "Yes."  I say, "The TransLink folks want my kids' birth certificates in order to get a youth TransLink card and they bond the birth certificate to the card so they track the kid's movement throughout their system."  His jaw dropped to the ground.  We shared a brief moment of liberal outrage and my kids and I went on our way.

When I got home, I checked the TransLink site.  There is no mention of requiring a birth certificate to obtain a youth pass.  I called the TransLink customer service line and they did verify that a birth certificate is required.

First, TransLink is hiding the fact that a birth certificate is required.  It's not on their public web site although it is mentioned in a press release.  Why are they hiding this information?  Do they think that if they don't publish it on their web site that somehow it'll make it okay?

Second, TransLink is managed by the Metropolitan Transportation Commission, but this is not mentioned anywhere on the TransLink web site and is only mentioned once in the TransLink cardholder "license agreement".  Why is the MTC hiding?

Third, if the MTC is a governmental body, where was the public hearing and the public comment request relating to requiring a birth certificate and bonding that information to youth passes?

Forth, what great problem are they looking to solve by associating a card with a person?  What kind of failures is the current youth (and elderly) pass mechanism (a different colored pass) justify bonding my child's birth certificate to their transit card and track my children's comings and goings?

So, I am outraged.  I will burn my TransLink card and until this Orwellian, KGB-esq policy of requiring a birth certificate to get a youth TransLink, I will not in any way participate in the TransLink system.  I urge you to do the same.

by David Pollak (nospam@example.com) at March 06, 2010 06:06 AM

March 05, 2010

Coderspiel

"In Scala 2.8, using the builder pattern is no longer necessary (or the most optimal solution) in..."

“In Scala 2.8, using the builder pattern is no longer necessary (or the most optimal solution) in many cases, as Scala 2.8 adds support for named and default parameters.”

- Taking Advantage of Scala 2.8: Replacing the Builder

March 05, 2010 09:49 PM

Erkki Lindpere

Taking Advantage of Scala 2.8: Replacing the Builder

In Scala 2.8, using the builder pattern is no longer necessary (or the most optimal solution) in many cases, as Scala 2.8 adds support for named and default parameters.

I’ll give an example of this based on ScalaBox2D. 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.

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):

body {
  box(halfWidth, halfHeight) density 1 friction 0.3f restitution 0
  computeMassFromShapes
}

For the DSL implementation, I used simple builders which left values to defaults if not specified:

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
}

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.

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
)

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:

body {
  fixtures(
    fixture(box(halfWidth, halfHeight), density = 1, friction = 0.3f, restitution = 0)
  )
  computeMassFromShapes
}

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.

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.


by Erkki Lindpere at March 05, 2010 09:22 PM

Coderspiel

"At this stage, the Skinput armband contains a pico projector that displays the..."

“At this stage, the Skinput armband contains a pico projector that displays the ‘touchpad’ on the user’s hand or forearm, along with biosensors that recognize skin taps on corresponding locations of the body, based on bone and soft tissue variations.”

- Your body is just a big iPod touch

March 05, 2010 04:52 PM

March 04, 2010

Coderspiel

Lift Web Framework 2.0 Milestone 3

Lift Web Framework 2.0 Milestone 3:

  • Enhancements to LiftActors and LRU to support Goat Rodeo
  • Add CouchDB support (lift-couchdb)
  • … !

March 04, 2010 01:16 PM

March 03, 2010

Henrik Huttunen

Named arguments

edit at 3rd of March, 2010: Scala will have named arguments in 2.8. Great! http://www.scala-lang.org/sid/1#

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.

First, let's create the function that can handle homemade named arguments:

    def f(x: {val name: String; val age: Int}) = {


f takes one argument, that contains the actual parameters we want. The type is shorthand for Any{ ... }.

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.

    import x._


And now we can use the attributes as if they were f's real parameters:

    println(name + " is " + age + " years old")


How about from client's perspective? Well, client can just create a structural type that contains the needed attributes (actual arguments).

    f(new {val name = "Anthony"; val age = 5})


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:

We create a case class for each actual parameter.

    case class Name(name : String)
case class Age (age : Int)


Now we can make very clear function signature:

    def g(x: Name, y: Age) = {


Unfortunately we have to import all parameters one by one.

    import x._, y._
println(name + " is " + age + " years old")


The client can call the method in the following way:

    g(Name("Tim"), Age(2))


That's it. It's annoying that we have to create case classes, but it's tolerable in important cases.

Here's the full code:


object Test extends Application{
def f(x: {val name: String; val age: Int}) = {
import x._
println(name + " is " + age + " years old")
}
f(new {val name = "Anthony"; val age = 5})

case class Name(name : String)
case class Age (age : Int)

def g(x: Name, y: Age) = {
import x._, y._
println(name + " is " + age + " years old")
}
g(Name("Tim"), Age(2))
}

Run the code and you get as output:
Anthony is 5 years old
Tim is 2 years old

edit:

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:

val age = 8
val name = "Jim"
f(age, name)


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.

Then:

f(new {val age = 8; val name = "Jim"})
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:
f(new { val age = 8
val name = "Jim"
val hobby = "blogging" })


Named arguments are especially useful when constructing objects, because it's then when you have to pass lots of vaguely related arguments.

by Henrik Huttunen (henrik.huttunen@gmail.com) at March 03, 2010 06:47 AM

March 02, 2010

Coderspiel

Bevy of sbt plugins

Bevy of sbt plugins:

This repository contains various plugins for the SBT build tool:

  • The EditSource plugin provides methods that offer a similar substitution facility to the one available with an Ant’s filterset.
  • The IzPack plugin provides a method that will run the IzPack compiler, generating an installer jar for your application.
  • The Markdown plugin supplies methods to translate Markdown documents into HTML.

March 02, 2010 08:36 PM

Shocker: Apple is going to use those patents

Shocker: Apple is going to use those patents:

Remember how everybody dutifully assumed that Apple was patenting everything from Minority Report as a defense measure against, you know, ‘other’ patent trolls? Well it turns out that is not the case. They’re going to preemptively file suit to impede their competition, because they can. This is how things work.

March 02, 2010 05:32 PM

Richard Dallaway

This blog has moved

This blog has moved to http://richard.dallaway.com/ or http://richard.dallaway.com/rss.xml if want the feed URL. Sorry. Thanks.

by Richard (noreply@blogger.com) at March 02, 2010 10:17 AM

Coderspiel

Introducing GitHub Compare View

Introducing GitHub Compare View:

That feature you’ve been clicking around for on github exists, now!

March 02, 2010 01:27 AM

March 01, 2010

Coderspiel

The advance program for Scala Days 2010

The advance program for Scala Days 2010:

Click on their PDF link for beautiful aerial photos and session details!

March 01, 2010 06:01 PM

scala-lang.org

Cloud Computing with Scala

Nikita Ivanov, Founder and CEO of GridGain will be presenting "Cloud Computing with Scala and GridGain" in the "New Cloud!" track at the The ServerSide Java Symposium, March 17-19, 2010, Caesars Palace, Las Vegas. This will be the first showing of the GridGain v3.0 code base in a practical hands-on session where he will show you how to use Scala with GridGain’s cloud development platform to create simple and productive cloud computing platforms. TSSJS will have lots of informative sessions for you and the added bonus of being able to listen to James Gosling, the father of Java, keynote speech too.

by bagwell at March 01, 2010 02:41 PM

Coderspiel

"Dependency Injection is one of the techniques that I use regularly when I am programming in Java...."

“Dependency Injection is one of the techniques that I use regularly when I am programming in Java. … I have been programming more in Scala and Clojure and being exposed to many of the functional paradigms that they encourage and espouse … 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.”

- Dependency injection potentially sidelined by function currying

March 01, 2010 02:07 PM

Ruminations of a Programmer

Dependency Injection as Function Currying

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.

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.

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 - AssistedInject. 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 ..

public class RealPayment implements Payment {
  @Inject
  public RealPayment(
        CreditService creditService,  // injected
        AuthService authService,  // injected
        @Assisted Date startDate, // caller to provide
        @Assisted Money amount);  // aller to provide
  }
  ...
}


Then in the Guice module we bind a Provider<Factory> ..

bind(PaymentFactory.class).toProvider(
  FactoryProvider.newFactory(
    PaymentFactory.class, RealPayment.class));


The FactoryProvider maps the create() method's parameters to the corresponding @Assisted parameters in the implementation class' constructor. For the other constructor arguments, it asks the regular Injector to provide values.

So the basic issue that AssistedInject solves is to finalize (close) some of the parameters at the module level to be provided by the injector, while keeping the abstraction open for the rest to be provided by the caller.

On a functional note this sounds a lot like currying .. 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.

Consider the above abstraction modeled as a case class in Scala ..

trait CreditService
trait AuthService

case class RealPayment(creditService: CreditService,
                       authService: AuthService,
                       startDate: Date,
                       amount: Int)


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 ..

val rp = RealPayment( //..


is in fact a syntactic sugar for RealPayment.apply( //.. that gets called implicitly. But you know all that .. right ?

Now for a particular module , say I would like to finalize on PayPal as the CreditService 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 ..


scala> case class PayPal(provider: String) extends CreditService
defined class PayPal

scala> val paypalPayment = RealPayment(PayPal("bar"), _: AuthService, _: Date, _: Int)
paypalPayment: (AuthService, java.util.Date, Int) => RealPayment = <function>




Note how the Scala interpreter now treats paypalPayment as a function from (AuthService, java.util.Date, Int) => RealPayment. 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 CreditService 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 ?

Within the module I can now invoke paypalPayment with only the 3 parameters that are still open ..


scala> case class DefaultAuth(provider: String) extends AuthService
defined class DefaultAuth

scala> paypalPayment(DefaultAuth("foo"), java.util.Calendar.getInstance.getTime, 10000)
res0: RealPayment = RealPayment(PayPal(foo),DefaultAuth(foo),Sun Feb 28 15:22:01 IST 2010,10000)




Now suppose for some modules I would like to close the abstraction for the AuthService as well in addition to freezing PayPal as the CreditService. One alternative will be to define another abstraction as paypalPayment through partial application of RealPayment where we close both the parameters. A better option will be to reuse the paypalPayment abstraction and use explicit function currying. Like ..


scala> val paypalPaymentCurried = Function.curried(paypalPayment)
paypalPaymentCurried: (AuthService) => (java.util.Date) => (Int) => RealPayment = <function>




and closing it partially using the DefaultAuth implementation ..


scala> val paypalPaymentWithDefaultAuth = paypalPaymentCurried(DefaultAuth("foo"))
paypalPaymentWithDefaultAuth: (java.util.Date) => (Int) => RealPayment = <function>




The rest of the module can now treat this as an abstraction that uses PayPal for CreditService and DefaultAuth for AuthService. Like Guice we can have hierarchies of modules that injects these settings and publishes a more specialized abstraction to downstream clients.

by Debasish (ghosh.debasish@gmail.com) at March 01, 2010 05:59 AM

Paul Chiusano

Actors are not a good concurrency model

Actors are not a good concurrency model, and neither are Erlang processes.

Wait, what? Isn't Erlang the king of high-uptime distributed systems? If Erlang can do all this using the actor model (Erlang processes are identical to actors in the essential ways, discussed below), isn't there something good about it? Isn't there??

Well, yes. What's good about it is it's better than the dinosaur era alternative of shared-state preemptive multithreading. But so what? Just because the actor model is better than dinosaur era technology doesn't mean we should keep using it. Remember the rise of OO? Before OO, the alternative was programming in languages like C, with pretty much zero support for polymorphism. Any approach to polymorphism was better than nothing at all, so OO's concepts of classes and subtyping was a huge improvement (my opinion is that polymorphism was the real unfilled niche that OO filled). Now that we've learned a bit more, OO has started to seem less appealing to some - parametric polymorphism exists independent of OOP, and bounded polymorphism can be implemented with either typeclasses or a combination of first-class and higher-order modules. It's possible there are some other ideas from OO worth salvaging, but more likely I think OO is just an evolutionary dead-end.

And so it might be with actors. Though I don't know exactly what the replacement looks like yet (I'd like to look at some alternatives and explore ideas in a future post), I do know what's wrong with actors, and that's what I'd like to explore here. But going further, I'd like to use actors as an example to show what's also problematic with side effects and impure functions in general. In doing so I'll try to avoid the usual FP evangelizing and make this a more precise, technical argument.

So what's wrong with actors? The problem which dooms the actor model is that actors are fundamentally not composable. I'm going to leave that term undefined for now, except to say vaguely that entities are composable if we can easily and generally combine their behaviors in some way without having to modify the entities being combined. I think of composability as being the key ingredient necessary for acheiving reuse, and for achieving a combinatorial expansion of what is succinctly expressible in a programming model. (Also see this explanation by Tony Morris.) It goes without saying that code reusability is a very desireable property for programs to have, and many of the other virtues of good software engineering end up tying back to reusability - for instance, testability is nothing more than the ability to reuse a component in the context of a test.

So what makes actors not composable? Well, an actor (and an Erlang process) is constructed from a function A => Unit. That this function returns Unit rather than a meaningful type means that it must hardcode some action to take once the result (whose type is not even shown) is available. It is this unfortunate property that destroys any chance we have at combining actors in any sort of general way like we would in a typical combinator library.

As a simple example, consider the following two actors: 1) an actor that accepts lists of integers and turns the list into a min-heap. 2) An actor that accepts (heap,k) pairs and extracts the top k elements in order from the given heap. Can we write a function that accepts these two actors and returns an actor which accepts (list,k) pairs and pulls out the k minimum elements in sorted order? And generalizing a bit, can we write the more general combining function, the one that doesn't care whether the types are 'heap', 'int' and 'list' or A, B or C?

You just can't do this with actors since you can't make any assumptions about what the actor will do with the result - it might forward it to some other actor, write it to a file, extract from it the missile launch codes and launch the missile, etc. In fact, if you actually try to achieve the composability I'm talking about using actors, what you end up doing is having all actors return their result as a response to their sender, essentially recreating pure functions within the actor model, badly, losing type information in the process. What this should tell you is that actors, if worth anything, might be useful more as a tool for building some other higher-level abstraction. But if that's the case, perhaps we shouldn't even expose actors as a programming model and instead just program directly in the higher-level model.

In practice, I suspect actors are basically only used at the top level of a program, where the damage to composability is minimized, and pure functions (which are composable and work fine at any level of the program) are used everywhere else to achieve reuse. The problem with this approach is when you find out a few months later that what you thought was going to be the top level of your program actually is going to become a very small embedded component in some larger system, and this top level now contains a large amount of unreusable complex logic that must be gutted to work as an embedded component.

The problem I am talking about here is not in any way specific to actors. It applies to any functions with side effects. I claim the only way to achieve true composability is to program with pure functions, and this applies to concurrent programs or to any other programs you'd like to write. (Sometimes, you decide it's worth taking the composability hit and you write functions with side effects - of course I don't object to this in absolutely all cases - but I don't think the fundamental model underlying concurrent and distributed programming should have to take this hit.)

What makes pure functions composable is they are only the logic of the computation. A pure function makes no decisions about what actions to take with the result of the computation, and also makes no decisions about what actions to take before the computation is executed. By keeping these concerns separate, we can reuse the function elsewhere in places where we need to do something different with the result, or where we need to do something different before the computation is executed (for instance, in testing, to generate an instance of the input type rather than obtaining the input from some impure function).

In fact, you can think of any impure function as having three "steps": 1) An "input" side effect, Unit => B, a pure function (A,B) => C, and an "output" side effect C => Unit. It makes perfect software engineering sense to decouple these components - and that is exactly what is done in purely functional programming.

Let's look at a very simple example. Suppose I write a sort function, which sorts the input list in place. For this function, we have no input side effects. The pure core of this function is (conceptually) a sorting function that returns a new list. What is the output side effect? It is to rebind the name that the input list is bound to in the caller to the sorted version of the list. So if the caller were something like
def foo(list: ArrayList[Int]): Foo = { 
...
sort(list)
...
}
then sort will rebind the name list to the sorted version of that list. Since list is itself a parameter of foo, this rebinding will occur in the caller of foo, and possibly its caller, all the way out to the place where that list was originally declared. The question is, should the sort function really be making the decision about whether to do this? If we let the sort function make this decision, we're allowing it to make an assumption about the caller, namely, that the caller no longer intends to maintain references to the unsorted version of the list (an assumption which, incidentally, is not even tracked by the type system).

Of course, this example of an in-place sort isn't too terrible. The caller just needs to be aware of this side effect and has to adjust its calling convention accordingly. But the lack of uniformity in chaining and combining logic caused by side effects starts to add up very quickly. In practice, what actually occurs in large systems with impure functions sprinkled about is that many functions end up with so many assumptions about their callers (and these assumptions propagate to their callers, as above) that the call graph becomes very static, with functions often having only a single or small number of callers. This makes the system a lot more rigid, more difficult to test without elaborate mocking and dependency injection frameworks (which brings with it its own set of problems), and results in a lot of (often hidden) duplication of logic.

Other simple examples show more obvious destruction of composability. If I write a function that reads a bunch of things from a file and then performs some complex logic, we can't easily reuse that logic elsewhere (unless we want to populate a file first, which may not be what we want). If a function performs some complex logic and then launches the missile, we can't easily reuse that complex logic elsewhere in places where we don't want to launch the missile.

Taking a step back from all this bashing of actors and side effects, I do certainly agree that actors and side effects in general have a certain "intuitive" appeal, a rough analog to how the real world works. But that does not justify their usage as a programming model. The technical challenges of engineering large pieces of software mean that a good programming model may have to tradeoff intuitiveness for attributes like composability. But I suspect even this tradeoff is overplayed - intuitiveness is also often code language for "what system I am familiar with". Once you learn and internalize a different model your ability to reason within that model (and thus, I believe, its intuitiveness) is more a function of the features and formal structure of that model than of some inherent "intuitiveness" metric.

by Paul Chiusano (noreply@blogger.com) at March 01, 2010 04:04 AM

February 28, 2010

Coderspiel

"So what’s wrong with actors? The problem which dooms the actor model is that actors are..."

“So what’s wrong with actors? The problem which dooms the actor model is that actors are fundamentally not composable.”

- Bad actors

February 28, 2010 11:01 PM

"Silicon Valley emerged as the world capital of high-tech development … due largely to the..."

“Silicon Valley emerged as the world capital of high-tech development … due largely to the horizontal networks of informal and formal cooperation that developed among fledgling companies … and, perhaps just as important, beers after work.”

- Bowling Alone (Don’t worry, we’ve got New York covered.)

February 28, 2010 09:45 PM

Josh Suereth

Actor Styles... OO, Functional or Blended...

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 "Worker" class (which is similar to an Actor class). He made a comment about "being annoyed with writing all those case statements" 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 earlier blog about partial functions and inheritance. Let's take a bit to remember that solution to the issue.

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 "FinalMixinActor" class

trait BaseActor extends Actor {

def makeMessageHandler() : PartialFunction[Any,Unit] = {
case x => //Unhandled message, assume design flaw!
error("Unknown message: " + x)
}
}
trait FinalActor extends BaseActor[T] {
lazy val messageHandler = makeMessageHandler()
override def act() {
Actor.loop {
react messageHandler
}
}
}

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 "FinalActor" gets mixed in *last* in the inheritance linearization. This means that when FinalActor calls "makeMessageHandler", 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....

case class Ping()
case class Pong()

trait PingPongBehavior extends BaseActor {
def makeMessageHandler() : PartialFunction[Any,Unit] = {
val myBehavior : PartialFunction[Any,Unit] = {
case Ping() => sender ! Pong()
}
return myBehavior orElse super.makeMessageHandler()
}
}

This is our "PingPong" 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...


case class Shutdown()

trait RemoteControlBehavior extends BaseActor {
def makeMessageHandler() : PartialFunction[Any,Unit] = {
val myBehavior : PartialFunction[Any,Unit] = {
case Shutdown() => Actor.exit
}
return myBehavior orElse super.makeMessageHandler()
}
}

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.


val myActor = new PingPongBehavior with RemoteControlBehavior with FinalActor
myActor.start

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....

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...)


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 twitter-clone sample on goat-rodeo's wiki. I've used elipses to ignore implementation details of Goat Rodeo:


class UserWorker(...) extends WorkerImpl[..., UserMsg](...) {
/**
* handle the Follow message
*/
def doFollow(msg: Follow) {
...
}
}

As you can see, it's just a method. Goat-Rodeo looks for methods with "do" or "handle" in the name and constructs a PartialFunction for the actor-behavior. The
Follow
class must be a subclass of
UserMsg
as Goat-Rodeo is also strongly typed. Because
doFollow
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...

class UserWorkerPlus(...) extends UserWorker(...) {
/**
* handle the Follow message
*/
def doFollow(msg: Follow) {
println("ZOMG!!!!!!!!!!!!!!!!!!")
super.doFollow(msg)
}
}

That should be a lot more comfortable to OO developers. With some type trickery you could even create the "management" behavior traits and mix them into "workers" 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):

import scala.actors.Actor
import scala.actors.Channel

class ChanneledActor[T](f : PartialFunction[T,Unit]) extends Actor {
val typedChannel = new Channel[T](this)
def act() {
Actor.loop {
typedChannel react f
}
}
}

def makeActor[T, U](f : PartialFunction[T,U]) : Channel[T] = {
val a = new ChanneledActor(f andThen ( x => () )) //Ignore results...
a.start
a.typedChannel
}

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:

scala> makeActor[Int,Unit] { case x : Int => println("HAI") }                   
res3: scala.actors.Channel[Int] = scala.actors.Channel@154f77b

scala> res3 ! "HAI"
<console>:10: error: type mismatch;
found : java.lang.String("HAI")
required: Int
res3 ! "HAI"
^

scala> res3 ! 5

scala> HAI


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.

by J. Suereth (noreply@blogger.com) at February 28, 2010 03:47 PM

February 26, 2010

Coderspiel

"i will publish this book by hook or by crook. i’m now several hundred hours into the project..."

“i will publish this book by hook or by crook. i’m now several hundred hours into the project and my technical review team is also into the project by dozens of hours. i’m not just going to waste that time. To be clear, the book project is on github here. i’m committed to 3 pages per day (except between 19 Mar 2010 - 28 Mar 2010 where i will be on a Guitar Craft course and completely incommunicado) until the first draft is complete. You all can watch the progress.”

- Allegedly too deep Scala book being written on github

February 26, 2010 11:24 PM

"Giving them a patent on this idea is lunacy. But the whole idea of software patents is lunacy. We..."

“Giving them a patent on this idea is lunacy. But the whole idea of software patents is lunacy. We need to eliminate software patents and we need to do it now.”

- Facebook Patents News, Feeds

February 26, 2010 02:39 PM

Outside.in looking for Scala coders in New York

Outside.in looking for Scala coders in New York:

Or more fully “Ruby, Scala, Geo, NLP, Ad Serving & Optimization”. Are you their next “Superstar Software Engineer”???

February 26, 2010 01:42 PM

February 24, 2010

Coderspiel

Jack Cough

SBT and Test Arguments



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.

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 "test" command. This is a bit unfortunate, but there is a workaround (not a very good one, but at least it exists).

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.

Passing Args to ScalaTest from the Command Line


Let's say you have this little ScalaTest class that uses both params and tags:

import org.scalatest.fixture.FixtureFunSuite
import org.scalatest.Tag

class WickedCoolTest extends FixtureFunSuite{
type FixtureParam = Map[String,Any]
override def withFixture(test: OneArgTest) {
test(test.configMap)
}
test("1", Tag("dood1")){ conf => println("dood1: " + conf) }
test("2", Tag("dood2")){ conf => println("dood2: " + conf) }
test("3", Tag("dood3")){ conf => println("dood3: " + conf) }
}

To run all tests in in this class use:

test-only WickedCoolTest

To run only tests tagged with "dood1" use:

test-only WickedCoolTest -- -n dood1

To run tests tagged with dood1 or dood2, use:

test-only WickedCoolTest -- -n "dood1 dood2"

To run all tests except for tests tagged with dood2, run:

test-only WickedCoolTest -- -l dood2

To run all tests except for tests tagged with dood2 dood3, run:

test-only WickedCoolTest -- -l "dood2 dood3"

To pass configuration parameters to a test, use:

test-only WickedCoolTest -- -Danswer=42

To pass many config params, add more -D's:

test-only WickedCoolTest -- -Danswer=42 -DrealAnswer=54

Tags and parameters can be used in combination, like so:

test-only WickedCoolTest -- -n "dood dood2" -Dhey=you -Dm=f

This runs only tests tagged with dood or dood2 and produces the following output:

dood: Map(hey -> you, m -> f)
dood2: Map(hey -> you, m -> f)

Passing Args to ScalaCheck from the Command Line


I'll spare you the entire example and just get to the point here.

To set the minimum number of successful tests, use '-s'

> test-only *IntParallelArrayCheck -- -s 5000
...
[info] == scala.collection.parallel.mutable.IntParallelArrayCheck ==
[info] OK, passed 5000 tests.


The rest of the examples are very similar. Consult ScalaCheck itself for further documentation.

  • -s (minSuccessfulTests): Number of tests that must succeed in order to pass a property
  • -d (maxDiscardedTests): Number of tests that can be discarded before ScalaCheck stops testing a property
  • -n (minSize): Minimum data generation size
  • -x (maxSize): Maximum data generation size
  • -w (workers): Number of threads to execute in parallel for testing
  • -z (wrkSize): Amount of work each thread should do at a time

Default Arguments


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.

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:

override def testOptions =
super.testOptions ++
Seq(TestArgument(TestFrameworks.ScalaCheck, "-s", "5000"))


Or maybe you only ever want to run your fast ScalaTest tests.

override def testOptions =
super.testOptions ++
Seq(TestArgument(TestFrameworks.ScalaTest, "-n", "fast"))

Custom Test Tasks


Maybe you have some test that you run all the time. At the sbt command line, instead of saying > test-quick blah.blah.Blah, you just want to say: > blah.

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.

lazy val blah = singleTestTask("blah.blah.Blah")

private def singleTestTask(className: String) = task { args =>
defaultTestTask(TestFilter(_ == className) ::
testOptions.toList ::: ScalaTestArgs(args))
}

private def newScalaTestArg(l: String*) =
TestArgument(TestFrameworks.ScalaTest, l:_*)

private def ScalaTestArgs(args: Seq[String]): List[TestArgument] = {
def KVArgs(args: Seq[String]): TestArgument =
newScalaTestArg(args.map("-D" + _):_*)
def tagsFromArgs(tags: Seq[String]): List[TestArgument] = {
if (tags.isEmpty) Nil else
List(newScalaTestArg("-n", tags.mkString(" ")))
}
val (kvs, tags) = args.partition(_.contains("="))
KVArgs(kvs.toSeq) :: tagsFromArgs(tags.toSeq)
}

Subclasses


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 > test-fast, and > test-slow.

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 :)

lazy val testSlow =
runSubclassesOf("org.nlogo.util.SlowTest")
lazy val testFast =
runEverythingButSubclassesOf("org.nlogo.util.SlowTest")

private def runSubclassesOf(className: String) = {
val subclass: Boolean => Boolean = x => x
subclassTest(className, subclass)
}

private def runEverythingButSubclassesOf(className: String) = {
val notSubclass: Boolean => Boolean = x => ! x
subclassTest(className, notSubclass)
}

private def subclassTest(className: String,
subclassCheck: Boolean => Boolean) = task {
args =>
lazy val jars =
testClasspath.get.toList.map(_.asURL).toArray[java.net.URL]
lazy val loader =
new java.net.URLClassLoader(jars,buildScalaInstance.loader)
def clazz(name: String) = Class.forName(name, false, loader)
lazy val superClass = clazz(className)
def filter =
TestFilter(c =>
subclassCheck(superClass.isAssignableFrom(clazz(c))))
defaultTestTask
(filter :: testOptions.toList ::: ScalaTestArgs(args))
}


Yeah...


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.

by Jack Cough (noreply@blogger.com) at February 24, 2010 05:59 PM

Coderspiel

"It turns out that the International Intellectual Property Alliance, an umbrella group for..."

“It turns out that the International Intellectual Property Alliance, an umbrella group for organisations including the MPAA and RIAA, has requested with the US Trade Representative to consider countries like Indonesia, Brazil and India for its ‘Special 301 watchlist’ because they use open source software.”

- Free software challenges “intellectual property” construct

February 24, 2010 04:06 AM

February 23, 2010

Coderspiel

Web Development in the Scala World Outside of Lift - New York Scala Enthusiasts

Web Development in the Scala World Outside of Lift - New York Scala Enthusiasts:

Peter will talk about … The work he’s been doing to integrate Scala into The Play Framework … And his very own Pinky, a REST/MVC glue web framework built on top of Guice and Guice Servlet 2.0

February 23, 2010 09:13 PM

"It does not try to hide relational database concepts from the developer, on the contrary it exposes..."

“It does not try to hide relational database concepts from the developer, on the contrary it exposes them as first class citizens so they can be easily leveraged from within the Scala language.”

- Squeryl: a functional bridge between Scala objects and relational data

February 23, 2010 03:45 PM

iNanny cracks whip

Right after Apple made us ridicule their sorry quest for a mobile “bring-up” loser, Phil Schiller has gone and blabbed a bunch of 1984 crap to the Times, which he foreshadowed back in November. It’s awesome.

See, just over the last few weeks the world has gotten really Naughty. Schiller himself has blushed at

an increasing number of apps containing very objectionable content.

Oh my! Let’s just hope Schiller was not forced to evaluate each and every one, in private. Apparently this is a Unique Crisis:

It came to the point where we were getting customer complaints from women who found the content getting too degrading and objectionable, as well as parents who were upset with what their kids were able to see.

Now hold on a sec, Mr. Schiller. Women have been known to disagree with each other on many things, including what is too degrading (?) or objection-able. It’s a little iffy to completely ban products based on what you say the women say. And those parents, maybe you should tell them about the famous “parental controls” you now build into everything?

We obviously care about developers, but in the end have to put the needs of the kids and parents first.

Much like it has always been necessary to ban racy items from bookshelves, computers, and the internet because some anonymous prude in the employ of a private business was offended/aroused.

But what about Sports Illustrated’s beloved mainstream pr0n—which (some) women have been objecting to for ages—is there some kind of special, or double, standard in place to protect our big old-media brands?

The difference is this is a well-known company with previously published material available broadly in a well-accepted format.

Well that’s a relief. S.I. is just appropriately degrading to women.

On the other hand if you want a software platform that isn’t run like a bought-off nursery, there are alternatives.

February 23, 2010 02:21 PM

Quoi qu'il en soit

Dynamically create spring bean

I used this in a dbunit test to create a dummy service layer bean dynamically.

The rest of the beans were configured in xml files. I created this bean dynamically in the test so that I didn't have to pollute the xml config.

// Dynamically create a DummyServiceToMakeCodeTransactional bean and register with spring
DefaultListableBeanFactory autowireCapableBeanFactory = (DefaultListableBeanFactory) getApplicationContext().getAutowireCapableBeanFactory();
AbstractBeanDefinition beanDefinition =
BeanDefinitionBuilder.rootBeanDefinition(
DummyServiceToMakeCodeTransactional.class.getName()).getBeanDefinition();
autowireCapableBeanFactory.registerBeanDefinition("DummyService", beanDefinition);
DummyServiceToMakeCodeTransactional bean = (DummyServiceToMakeCodeTransactional)
getApplicationContext().getBean("DummyService");

by Tim Azzopardi (noreply@blogger.com) at February 23, 2010 12:08 PM

Coderspiel

God hires assistant

God hires assistant:

The job posting from the Cupertino, CA, company specifically calls for an engineering manager to handle “platform bring-up.” In the job description, Apple states that it’s looking for a manager “to lead a team focused on bring-up of iPhone OS on new platforms.” It seems like Apple wants some outside talent with very strong technical experience, especially when it comes to bringing software onto multiple hardware devices.

Whatever a “bring-up” is, this job posting is weird because Apple is supposed to have the next decade specked out already, if not in product testing. We (consumers) learn about the future of computing at Apple events, when they decide to tell us. Our 2015 portable computers will be descendants of Apple’s “iPad” invention, like those copy-cat props in Avatar and Minority Report.

But this job posting would imply that Apple is in a competitive marketplace where other companies have ideas, too. That open mobile platforms may be on the verge of a cambrian explosion of unexpected, unplanned, useful devices that will be difficult to fight off with a platform that is locked down in both hardware (old news!) and software (evil news!). That the world hasn’t been standing still in the two years that the iPhone has … grown. (Not that there’s anything wrong with that.)

February 23, 2010 01:17 AM

February 22, 2010

Ruminations of a Programmer

DSL : Grow your syntax on top of a clean semantic model

A DSL primarily has two components - a semantic model that abstracts the underlying domain and a linguistic abstraction 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 :-



It's interesting to observe that the two components of the stack evolve somewhat orthogonally.

The Semantic Model evolves Bottom Up

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.

Here's an example code snippet from my upcoming book DSLs In Action 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 ..

class TaxFeeRulesImpl extends TaxFeeRules {
  override def forTrade(trade: Trade): List[TaxFee] = {
    (forHKG orElse 
       forSGP orElse 
         forAll)(trade.market)
  }
   
  val forHKG: PartialFunction[Market, List[TaxFee]] = { 
    case HKG => 
      // in real life these can come from a database
      List(TradeTax, Commission, Surcharge)
  }
    
  val forSGP: PartialFunction[Market, List[TaxFee]] = {
    case SGP => 
      List(TradeTax, Commission, Surcharge, VAT)
  }
    
  val forAll: PartialFunction[Market, List[TaxFee]] = {
    case _ => List(TradeTax, Commission)
  }

  //..
}


The method forTrade clearly expresses the business rule, which reads almost as expressive as the English version ..

"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"

Note how Scala PartialFunction s can be chained together to give the above model an expressive yet succinct syntax.

The Language Interface evolves Top Down

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 DSL Facade that sits between your DSL script and the underlying domain model and acts as the glue.

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.

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.

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 minimalism, distillation, extensibility and composability.

by Debasish (ghosh.debasish@gmail.com) at February 22, 2010 05:29 AM

Coderspiel

February 21, 2010

Coderspiel

sqlshell: a Scala-based SQL command line tool

sqlshell: a Scala-based SQL command line tool:

similar in concept to tools like Oracle’s SQL*Plus, the PostgreSQL psql command, and MySQL’s mysql tool

February 21, 2010 09:22 PM

irb on Android

irb on Android:

Because… An Application may itself install or launch other executable code by any means, including without limitation through the use of a plug-in architecture, calling other frameworks, other APIs or otherwise. Any interpreted code may be downloaded and used in an Application.

February 21, 2010 06:05 PM

Eishay Smith

Parallelizing JUnit test runs

Original post at the kaChing engineering blog.

Test runs should be as fast as possible in order to allow a lean development cycle. One of the applications is a Continuous Deployment (see Lean Startup).

Using strong multi-core machines to run tests is not enough since most unit tests are using a single thread per test. Apart of reducing IO to minimum, having tests running in parallel is required to squeeze the juice of of the machine. Additional value from Parallelizing JUnit test runs is ensuring that tests have no dependency between each other.

The way we implemented parallel test runs is creating an ANT target with a parallel task containing N JUnit tasks where N == number of cores. For example:
<parallel>
      <junit printsummary="yes" haltonfailure="yes" fork="true" maxmemory="${maxmemory}" showoutput="yes">
        <jvmarg value="-XX:MaxPermSize=${permMem}"/>
        <jvmarg value="-Xms${minMem}"/>
        <jvmarg value="-Xmx${maxmemory}"/>
        <classpath refid="classpath.test" />
        <formatter type="xml" usefile="true" />
        <test name="com.kaching.GroupedTests$GroupA" todir="${testTargetJunit}" unless="testcase"/>
      </junit>
      <junit printsummary="yes" haltonfailure="yes" fork="true" maxmemory="${maxmemory}" showoutput="yes">
        <jvmarg value="-XX:MaxPermSize=${permMem}"/>
        <jvmarg value="-Xms${minMem}"/>
        <jvmarg value="-Xmx${maxmemory}"/>
        <classpath refid="classpath.test" />
        <formatter type="xml" usefile="true" />
        <test name="com.kaching.GroupedTests$GroupB" todir="${testTargetJunit}" unless="testcase"/>
      </junit>
  ...
     </parallel>
The GroupedTests$GroupX are classes extending TestCase with a public static Test suite(). The suite() method creates a JUnit test suite on the fly by loading all the tests in scope and filtering them out. If for example there are four cores, therefore you would like to have a group of four suites each running fourth of the tests. The suits are using a java.util.Random seeded with the commit revision number. Using the Random object we decide on placing test cases in suits.
random.nextInt(numOfTestSuites) == testSuiteId
Therefore a testcase goes to a single suite and they are evenly distributed between suites. Randomness in assigning test cases to suites (and therefore to processes) gives us some reassurance that there are no dependencies between tests.

As a result we have the tests running about twice as fast as they did before, in the range of 2 min, 20 sec for about 4.6k tests for one of our components, including code fetch from source repository, clean, build, and test suite setup time. It gives us a nice 100% test machine utilization and faster commit to production deployment cycle (about four minutes).

by Eishay Smith (noreply@blogger.com) at February 21, 2010 03:46 AM

February 20, 2010

Coderspiel

February 19, 2010

Coderspiel

scala-lang.org

Scala in Egypt

JDC-2010 takes place 27 February in Nasr City, Aswan, Egypt  and has been promoted by the EGJUG, one of the most active user groups in the Middle East. This year Scala, an "emerging language" is being presented by Hossam Karim, a Senior Technical Architect at ITWorx. As well as Scala, the JDC will be covering Rich Internet Application and Client (JSF, JavaFX, Adobe Flex, JBoss Rich Faces) and Enterprise Application Integration (OSGi, Spring Integration, Enterprise Service Bus, SOA, Business Process Management,Enterprise Messaging, Webservices).

by bagwell at February 19, 2010 01:08 PM

Use Infinispan with Scala

Infinispan 4.0.0.CR4 has just been announced with many new features and Amin Abbaspour, a member of the JBOSS community, has provided a tutorial on how to use this powerful data grid platform from Scala. Step by step, he explains how Scala can be used to create high performance, highly available Enterprise scale Infiniscan data grids.

by bagwell at February 19, 2010 09:49 AM

Scala at NFJS Symposiums

NFJS - No Fluff Just Stuff Software Symposiums are recognised to be world class events where you can learn about the latest technology, improve your skills and meet the experts. This year there are a series of local events centred around Enterprise Java, Agility, Spring, Groovy/Grails, Ajax, JSF and multiple language sessions on Scala.

Reflecting the growing commercial deployment of Scala each NFJS event now contain several sessions on various aspects of Scala given by leading industry consultants, people like Michael Nygard, Venkat Subramaniam and Ted Neward. If you are interested in staying up to date in the Java world and want to get up to speed with Scala then look out for an NFJS symposium near you; please read below.

by bagwell at February 19, 2010 08:35 AM