Scala

Scala is a programming language that runs on the Java platform and can freely interoperate with Java code.

Learning Scala

Here are some places to learn Scala.


The definitive book on Scala, by the creators of the language. Covers all aspects of the language thoroughly, in an easy-to-read way. Highly recommended given the general lack of good Scala learning resources on the Web.
A Tour of Scala
From the official Scala web site, one of the more useful guides to the Scala programming language.
The busy Java developer's guide to Scala: Functional programming for the object oriented
Ted Neward introduction to and overview of Scala

Which of these features of Scala is most important to you?

Presentations

"A Scalable Language" by Martin Odersky the father of the Scala language (via SlideShare)

"Why Scala?" and "Why Scala for Web 2.0?" Alex Payne explains why Twitter chose Scala. (via SlideShare)

"Pragmatic Real-World Scala (short version)" by Jonas Boner (via Slideshare)

Tips and Tricks

Pimping Libraries

An idiom for cleaner pimping of libraries in Scala

In dynamic languages like JavaScript and Ruby you can modify existing classes, including system classes, with extra methods. This can be a very handy, if dangerous, technique.

Scala, being a static language does not allow this same technique. But it does allow the use of implicit functions to get the same effect. For example the standard idiom to add sine and cosine methods to the Double class is

class PimpedDouble(x:Double){
  def sin = Math.sin(x)
  def cos = Math.cos(x)
}
implicit def pimpDouble(x:Double) = new PimpedDouble(x)

This has the effect of adding sin and cos methods to Double, though what is actually happening is that Double values are automatically converted to PimpedDouble values when doing so would correctly compile.

This is very clever and very handy, but the idiom is a little clumsy, compared to most of Scala's elegant features. It is annoying to have to invent two names PimpedDouble and pimpDouble that are never referred to anywhere else in the code.

Can we do better? Yes we can. We can make the extra class be an anonymous class, which leaves only one arbitrary unreferenced name and also reduces the size of the code.

implicit def pimpDouble(x:Double) = new {
  def sin = Math.sin(x)
  def cos = Math.cos(x)
}

That seems a lot nicer to me.

Using either of the above techniques you can now do things like

  val x = 0.5 cos
  val y = 0.5 sin

which is taking the cosine and sine of the Double value 0.5

(Thanks to Daniel whose stackoverflow posting first brought this idiom to my attention.)

Making it easier to add attributes to an XML element

In most respects Scala makes it very easy to handle XML, but there are a few things that are hard to do. One is changing the value of an existing attribute on an element, or adding a new attribute to an element.

The simplest way I found to add new attributes c="CCC" d="DDD" is to do:

val modified = elem %
                new UnprefixedAttribute("c","CCC",Null) %
                new UnprefixedAttribute("d","DDD",Null)

This is a bit verbose. What if instead you could do

val modified = elem % Map( "c"->"CCC", "d"->"DDD" )

Well you can, if you add the following implicit somewhere in scope:

    implicit def pimp(elem:Elem) = new {
      def %(attrs:Map[String,String]) = {
        val seq = for( (n,v) <- attrs ) yield new UnprefixedAttribute(n,v,Null)
        (elem /: seq) ( _ % _ )
      }
    }

This uses the library pimping pattern discussed elsewhere to effectively add a new version of the % operator to Elem that takes a Map of attributes.

The new % operator uses a for-yield construct to convert the attrs Map into a sequence of UnprefixedAttribute objects. It then uses the /: fold operator to repeatedly apply the built-in % operator, reducing down to an element with all the attributes added.

Adding a ! factorial operator

If you learned about recursion you probably were given the factorial function as a simple example. Here it is in Scala:

def factorial(n:Int):Int = if(n==0) 1 else n * factorial(n-1)

Very nice. Now you can do factorial(6) to get the answer 720.

But would it not be nicer if you could use standard mathematical notation and do 6!

Well you can. Simply add this simple pimping implicit somewhere in scope:

implicit def pimp(i:Int) = new { def ! = factorial(i) }

Now you can use the ! operator after any integer. To avoid confusing the compiler, however, you may have to add some extra parentheses:

(3!) + (3!)

(3+3)!

Can we do better? Well if we avoid using recursion we can do it all in one line:

implicit def pimp(n:Int) = new { def ! = ((1 to n) :\ 1) ( _ * _ ) }

Here we are generating the integers 1,2,3,...,n and using the :/ fold operator to multiply them all together.

Adding a exponentiation operator to Double

Using the same pattern that we used to add the ! factorial operator to Int we can add an ** exponentiation operator to Double.


def pow(x:Double,k:Int):Double = if(k==1)
   x
else
   (if (k%2==0) 1.0 else x) * pow(x*x,k/2)

implicit def pimp(x:Double) = new { def **(k:Int) = pow(x,k) }

You can now, for example, type in 2.0 ** 8 to get 256.0 or Math.Pi ** 4 to get 97.40909103400243

How a Scala script can specify what JARs should be added to its CLASSPATH

The reason the Scala programming language has such a name is that it is meant to be scalable, i.e. good for both small scripts and large software systems. I am already convinced that Scala is as good, if not better than Java for the large-scale development, but what about the small scale?

Well it turns out that on Linux if you chmod +x a scala script you can make it executable with a bit of shebang magic as shown by this complete, runnable script:

#!/bin/sh
exec scala $0 $@
!#
println("Hello World"!")

(You do have to make sure that "scala" is in your executable path.)

But what if you are using a scala script to control an existing Scala or Java system? It would be nice to express the CLASSPATH requirements in the script itself rather than depending on the calling environment to be configured correctly. Well, I came up with this convenient idiom to collect all the JAR files in a lib directory and add them to the CLASSPATH:

#!/bin/sh
L=`dirname $0`/../lib
cp=`echo $L/*.jar|sed 's/ /:/g'`
exec scala -classpath $cp $0 $@
!#
import com.my.special.World
val world = new World
world.hello

This assumes that all the required JAR files are in directory ../lib relative to the script file.

[This was originally posted on my blog.]

Why Scala may not be for you

Not everyone thinks Scala is the bee's knees. For example Guido van Rossum the creator of the Python language has got some well considered criticism of the complexity of Scala in a blog post. However do also read the comment below the blog post, where other opinions are expressed.

In general I agree that one of the disadvantages of Scala is that it is a "big" language, with more features and complexity in the language itself than in dynamic languages like Python or Ruby. Personally I think that is worth the payoff of a language that is as concise as dynamic languages while having the safety of a static language.

Functional Programming

Sure you can program in Scala just like you would in Java and get all the advantages of the cleaner syntax. But if you really want to explore the power of Scala you should try some functional programming.

One way to tell if you are programming in a largely functional manner is if you prefer val declarations to var declarations and if you prefer immutable to mutable classes.

How you might create a matrix library in a functional programming style

To implement matrices, let's leverage the immutable lists generated by Scala's default List(...) call.

Arbitrarily choosing a row-major representation, we will represent a row of a matrix as as a list of doubles. For convenience let's create a type alias for that:

  type Row = List[Double]

A matrix can then be a list of rows:

  type Matrix = List[Row]

One fundamental operation we will be doing is a dot product. Let's implement this in map-reduce style:

  def dotProd(v1:Row,v2:Row) = 
    v1.zip( v2 ).map{ t:(Double,Double) => t._1 * t._2 }.reduceLeft(_ + _)

The zip method combines together two parallel lists of numbers into a single list of pairs of numbers. The map method multiplies the elements of each of these pairs together. The reduceLeft method sums the numbers of the list to create a single number.

OK, now let's see how we can create some of the standard matrix functions.

First here is how we can transpose a matrix:

  def transpose(m:Matrix):Matrix = 
    if(m.head.isEmpty) Nil else m.map(_.head) :: transpose(m.map(_.tail))

Notice this is a recursive function. It uses the construct firstRow :: remainderOfRows to form the list of rows that is the output matrix. It calculates the first row of the output matrix by using map to get the first element of each row of the input matrix. It gets the remainder of the rows by recursively getting the transpose of the matrix formed by the tail of each input row (i.e. every element except the first one).

How about matrix multiplication? That turns out to be pretty straightforward using standard Scala "for comprehensions":

  def mXm( m1:Matrix, m2:Matrix ) = 
    for( m1row <- m1 ) yield
      for( m2col <- transpose(m2) ) yield
	dotProd( m1row, m2col )

Here we directly implement the matrix multiplication formula, iterating through the rows of the first matrix and the columns of the second matrix, and calculating the dot-product of each. Note that to iterate over the columns of a matrix we actually iterate over the rows of the transpose of the matrix.

However it would be nice to be able to use standard mathematical operators like A*B and AT when coding with matrices. Well, we can using the power of Scala's operator identifiers, infix syntax, and implicit conversion:

case class RichMatrix(m:Matrix){

  def T = transpose(m)

  def *(that:RichMatrix) = mXm( this.m, that.m )

}

  implicit def pimp(m:Matrix)  = new RichMatrix(m)

The user of this library need never explicitly instantiate any RichMatrix objects -- they are automatically created from List[Double[Double]] object whenever the T or * method is called on it.

This allows you to do things like:

    val M = List(
      List( 1.0, 2.0, 3.0 ),
      List( 4.0, 5.0, 6.0 )
    )
    val MT = List(
      List( 1.0, 4.0 ),
      List( 2.0, 5.0 ),
      List( 3.0, 6.0 )
    )

    M.T  must_==  MT

    val A = List(List( 2.0, 0.0 ),
		 List( 3.0,-1.0 ),
		 List( 0.0, 1.0 ),
		 List( 1.0, 1.0 ))
    val B = List(List( 1.0,  0.0, 2.0 ),
		 List( 4.0, -1.0, 0.0 ))
    val C = List(List(  2.0,  0.0, 4.0 ),
		 List( -1.0,  1.0, 6.0 ),
		 List(  4.0, -1.0, 0.0 ),
		 List(  5.0, -1.0, 2.0 ))
    A * B  must_==  C

While we are at it we might as well add a few more convenience methods to the RichMatrix:

case class RichMatrix(m:Matrix){

  ...

  def apply(i:Int,j:Int) = m(i)(j)

  def rowCount = m.length
  def colCount = m.head.length

  def toStr = "\n"+m.map{ 
    _.map{"\t" + _}.reduceLeft(_ + _)+"\n"
  }.reduceLeft(_ + _)

}

The apply method is a special method invoked when the parenthesis operator is applied to an object. It allows you to, for example, do c(1,3) to get the number on the second row, fourth column of the matrix. Note that this method should not be used in performance-sensitive inner loops because it is not efficient. A matrix uses linked-lists as storage so the element access is O(N) not O(1) as would be the case if the matrix used arrays.

The rowCount and colCount methods are I hope obvious.

The toStr returns a string representation of the matrix in a nice tabular format that is much easier to than the default toString method if List[List[Double]], Note how this is done with two nested map/reduce pairs.

Now, creating a matrix using the List( List(...), List(...), ... ) syntax is fine for small matrices, but what if we want to generate large matrices? One way to do this is:

object Matrix{
  def apply( rowCount:Int, colCount:Int )( f:(Int,Int) => Double ) = (
      for(i <- 1 to rowCount) yield 
	( for( j <- 1 to colCount) yield f(i,j) ).toList
    ).toList
}

This allows you do do things like:

    // 100x200 matrix containing random elements
    val A =  Matrix(100,200) { (i:Int,j:Int) =>
      random
    }

    // 5x5 identity matrix
    val I = Matrix(5,5) { (i:Int,j:Int) => 
      if(i==j) 1.0 else 0.0
    }

Note that everything above is pure-functional: all the objects are immutable and each function is implemented as a single expression.

That's it for now. There is obviously a lot more we would have to add to make this a useful, production-quality library,

(If you want to try this yourself, you can download the complete source code and the BDD-style regression test that demonstrates its use.)

Square Root

Here is a recursive example of how the square function could be calculated using the Newton-Raphson method.

def sqrt(a:Double) = {
  val acc = 1e-10
  def findroot( x:Double ):Double = {
    val nextx = (a/x + x)/2
    if( (x-nextx).abs < acc*x ) nextx else findroot(nextx)
  } 
  findroot( 1 )
}

Giving sqrt(2) generates 1.414213562373095

(Translated from an example in ML for the Working Programmer.)

Reference

Once you have learned the basics of Scala, here are some useful references

Cheat sheet for Scala syntax
by Brendan
Scala for Java programmers
A Scala cheat sheet for Java programmers by Sundararajan

Core Library API Reference

The Scala Library API documentation
is a link you will need to keep handy.
The Java Library API documentation
is also as invaluable in Scala programming as it is in Java programming.

The Scala documentation above generated automatically by Scaladoc from the Scala source in the same way as the Java documentation is generated by Javadoc from the Java source.

Unfortunately Scaladoc is not yet as polished as Javadoc, and the documentation comments in the source code are much more meagre in the Scala library than in the Java library. So the net result is that the Scala Library API documentation is somewhat harder to work with and a bit less useful than the Java Library API documentation.

- valtrex buy online no prescription - where to buy asacol - cell spy phone - generic levitra -