Thursday, 29 December 2011

SaveAs for Strings in Scala with "Pimp my library" pattern

Problem

Have you ever wanted to save a string to a file? Well, it's one of this things you don't want to do in 3 lines of code like this:
val f = new FileWriter("some/file/name.txt")
f.write("My string I want to save")
f.close()
I would rather see it like that:
"My string I want to save".saveAs("some/file/name.txt")

Solution

Luckily in Scala we can use "pimp my library" pattern. It uses implicits to convert String to RichString containing the saveAs method:

Monday, 26 December 2011

Matrix multiplication in Scala

First take with idiomatic scala(77ms):


Second take - replacing fors with whiles(63ms):


Third take - sum in a variable(55ms):
Multi-threaded with parallel collections(15ms): Multi-threaded with parallel collections idiomatic scala(32ms):

Sunday, 25 December 2011

Lazy prime numbers in Scala


The key to understanding this example is the #:: operator. It is very similar to :: for collections. The difference is that it doesn't evaluate the right hand side argument immediately. Instead it keeps it's reference as a function and evaluates it only when needed. The evaluation is deferred until the actual element is needed.

Alternative to try-catch in Scala

I just saw such a nice control abstraction in Scala, that I wanted to share it. The original idea comes from akka-samples, I just modified it slightly to be more generic.


In this example the otherwise block gets executed only if system throws ClosedChannelExeption, but I could easily imagine other control abstractions hiding different exception types.

Scala block gotcha

Do you know what this line of code is going to print?
List(1,2,3).map{println("Adding 2"); _+2}

My first answer would be: 3 times "Adding 2".

Lets see:
scala> List(1,2,3).map{println("Adding 2"); _+2}
Adding 2
res11: List[Int] = List(3, 4, 5)

Hmm, it prints "Adding 2" just once...
So what is really happening here?

The cue is in understanding what is the semantics of a block in Scala.

The map function on List expects a function Int => B. The block {println("Adding 2"); _+2} is not a function Int => B. It's just a block of code which gets evaluated and returns value of the last expression. In our case when the block gets evaluated it prints "Adding 2" and then it returns a function x => x+2 (shorter _+2). Then this function is called 3 times to map elements of the list.

What is the workaround?

We just need to make sure that println is in the body of the function, that we pass to map, i.e.
scala> List(1,2,3).map{x => println("Adding 2"); x+2}
Adding 2
Adding 2
Adding 2
res12: List[Int] = List(3, 4, 5)

Enjoy!

Context bounds for type parameters in Scala

I thought I knew all type parameter bounds until recently, when I discovered the context bounds.

Let's have a look at the example:
def sum[T](list: Seq[T])(implicit typeClass : AddTypeClass[T]) = {
    list.reduce(typeClass.add(_,_))
  }

There is nothing unusual in here. Function sum needs a typeclass to perform it's operation.

Now let's express the same using context bounds:
def sum[T: AddTypeClass](list: List[T]) = {
    val typeClass = implicitly[AddTypeClass[T]]
    list.reduce(typeClass.add(_,_))
  }

Expression [T: AddTypeClass] means: "for some T such that, there exists a type AddTypeClass[T]". This is called context bounds.
Now we just need to get hold of an instance of type AddTypeClass[T]. We can use the function implicitly from Predef.

The definition of implicitly is as follows:
def implicitly[T](implicit e: T) = e

Enjoy!

Scala Hello World

You are probably wondering how to write the first scala program. Let’s start with the simple application, which calculates the area of a square.

Runnable application

In order to create runnable application we need to define a singleton object containing a main method. Let’s call it Area. (We’ll talk about singleton objects later.)
package helloWorld
object Area{
  def main(args: Array[String]){
    var width = args(0).toDouble
    println("Area = " + width * width ) 
  }
}

Methods

Method definition always starts with the def keyword followed by method name and comma-separated list of parameters. Unlike in java, parameter name comes first, and is followed by the colon and the parameter type, i.e.
def main(args: Array[String]){ 
  ... 
}
Method main takes one parameter called args of type Array[String].

Scala has no primitive array type

Array[String] is a class similar to ArrayList in java. There is no concept of primitive array in scala.

Generic types use square brackets

Array[String] is just an Array class parameterized by the String type.

Variables

var width = args(0).toDouble
To declare a variable we need to use var keyword. Scala compiler infers (guesses) a type of the variable. In our example, scala figures out that width is a Double. If you want you can be explicit about the types:
var width : Double = args(0).toDouble

Random access with () operator

You probably noticed that we are accessing elements of the args class with () operator. Array implements a special method apply, which allows random access to its elements using () operator, i.e. args(0)

Predef

You might wonder where does the println come from. It is defined in the Predef singleton object and is automatically imported in every scala file.

No semicolons

Semicolons at the end of the line are optional.

Compiling and running scala program

To run scala program you will need a scala distribution. You can download it from www.scala-lang.org. You also need JDK (Java Development Kit). Then make sure that scalac and scala are on the path and run the following commands:
> mkdir out
> scalac Area.scala -d out
> scala -cp out helloWorld.Area 5
Area = 25.0

Scalac is similar to javac; it compiles our file Area.scala into out directory. Then we run scala virtual machine, which is nothing more than java virtual machine with scala standard library on the classpath.


Enjoy!

Type classes in scala

I've recently come across the concept of type classes. They seemed to be widely used in functional languages.

The basic idea of type classes is that they offer code reuse by identifying common properties of seemingly different objects.

Lets look at operation sum. The same operation could be applied to numbers or to strings or even lists. In case of numbers sum will mean arithmetic sum of numbers, in case of strings and lists it would mean concatenation.

The basic logic is the same, if we express it in terms of an add operation i.e.:
sum(1, 2, 3) == (1 add 2) add 3
sum( list1, list2, list3) == (list1 add list2) add list3

Let's have look at the code example:


The sum function uses reduce function to "add" elements. In order to use the right add function for the type of elements in the list, we are using scala implicits. When compiler finds an expression sum(List("abc", "def", "ghi")) it looks for an implicit of type AddTypeClass[String], and uses it in order to compute the sum of elements.

I realise this case is overly simplistic, but I hope you get the idea!

For more examples of type classes check out Typeclassopedia.

Understanding Repr type parameter...

Have you ever wondered why scala collections use the Repr type parameter? I have. Only when I started writing my own library the realisation came.

If you want to have a reusable abstract class/trait, which needs to create instances of the subclasses, then you are probably going to use the abstract factory method pattern.

Let's have a look at the example below:


Make is the abstract factory method. We want it to return something of type USD when called on USD class or something of type EUR when called on instance of EUR class.

In order to tell the type checker what is the right type returned by the make method, we introduce the type parameter Repr which is restricted to be a subtype of CurrencyLike (Repr <: CurrencyLike[Repr]). The concrete class such as EUR needs to set the Repr to itself (ie. EUR), and implement the make method. That way + function which uses make to create an instance of currency returns EUR when called on EUR instance or in turn it returns USD when called on instance of USD class.

I hope it helps!

Scala, Eclipse and Unicode operators

Problem

I usually write my programs using IntelliJ but since I heard so much good about Eclipse scala plugin I decided to give it a go. I imported the project into Eclipse and (surprise, surprise) it did not compile...

I quickly figured that it was a problem with unicode operators. First they weren't displaying well. Second they were not recognised by compiler either.

Solution

I quickly googled for an answer and here is what you need to do:
  1. Find your eclipse.ini file (on mac it's inside the app package in Contents/MacOS folder)
  2. Add this line -Dfile.encoding=UTF-8 at the end.
  3. Restart Eclipse
  4. Clean and rebuild the project

Here is how it looks after the fix:

Enjoy!

Scala for Java Developers: Generic and Parametrized Types – Part II

In previous post we've talked about the basics of scala parametrized types. Let's now talk about some advanced stuff.

Here are few class we are going to use in our examples:
class Home[T]{
  private var inside = Set[T]()
  def enter(entering : T) = inside += entering
  def whoIsInside = inside
}

class Creature
class Human extends Creature


Understanding Covariance

Let’s say, we wanted our Home class to accommodate any Creature, i.e.
var house : Home[Creature] = new Home[Creature]
house enter(new Human)


What if we tried to assign Home[Human] to variable of type Home[Creature]?
house = new Home[Human]
error: type mismatch; found : Home[Human] required: Home[Creature]


Ok, Home[Human] is not a subclass of Home[Creature]. To fix the problem we would have to change our Home declaration to: class Home[+T]. This tells the compiler that class Home[Human] is a subclass of Home[Creature], because Human is a subclass of Creature. This is what it’s called covariance, and type parameter T is covariant.

Covariance of List

Have a look at the List class. It is declared as List[+A]; this gives you a lot of flexibility when manipulating lists, i.e.

var creatures : List[Creature] = List(new Dog, new Dog)
creatures = List(new Human)

Restrictions of covariance

Unfortunately, if we make our type parameter T covariant (Home[+T]), we will get a nasty compilation error:
error: covariant type T occurs in contravariant position in type T of value entering
         def enter(entering : T) = inside += entering


Let’s explain why, by looking at the following example:
class Dog extends Creature{
  def bark = println("Woof woof")
}

class DogHouse extends Home[Dog]{
  override def enter(entering : Dog) = { 
    entering.bark 
    super.enter(dog) 
  }
}


So far so good, the dog house makes the dog bark when it enters the dog house. Assuming that Home was declared as covariant: Home[+T], and the program compiled without errors - which isn’t true - lets try the following:
var home : Home[Creature] = new DogHouse //this is legal; Home[+T] is covariant
home.enter(new Human) // this seems legal as Human is a subtype of Creature


As far as assigning a DogHouse to the home variable - which is a home for any creature - sounds only interesting, the idea of a human entering a dog house is a bit crazy. More than that, if you look at overridden enter method, you will see, that the DogHouse will try to make our human bark, when he enters it...

Knowledge about parametrizing type is problematic

Clearly we did something wrong. We used the knowledge about the parameter type T in the overridden enter method: we knew that entering parameter type is Dog, so we wanted it to bark.

Compiler detects covariance issues

Scala compiler protects us against similar mistakes by enforcing that, if the type we used to parametrize our class is covariant (+T), then information about the type can not be used within the class. In other words: the parametrized class Home[+T] should be orthogonal to T, or should know nothing about the type T.

Learning more about covariance

You can find lots of examples of covariant types between scala immutable collections. By nature any container like object, which doesn’t care what it’s content is, is a good candidate for a covariant type.


I hope this post was helpful to you. Please let me know what you think about it, and leave a comment.

Scala for Java Developers: Generic and Parametrized Types - Part I

Parametrized classes

In its basic use case, generics in scala are very similar to java. Let’s have a look at the example:



class Home[T]{
  private var inside = Set[T]()
  def enter(entering : T) = inside += entering
  def whoIsInside = inside
}

class Creature
class Human extends Creature

var house = new Home[Human]
house enter(new Human)


Parametrized methods

You can parametrize methods the same way as in java:

class Box[T](val content : T)
def wrap[T](present : T) : Box[T] = new Box(present)

Type erasure

Scala parametrized types loose the type information after compilation in the same way as java does; this process is called type erasure.

Parameter type modifiers

Before we'll talk about type modifiers in details let's look briefly at the table below.
Parameter type modifiers

Upper bound example


class Home[ T<: Creature ] 
var home = new Home[Human]     // ok; Human is subtype of Creature
var home = new  Home[Object]   // failure!


Lower bound example


class List[A]{
  def append[B >: A](element : B) : List[B] = {...}
  ...
}

scala> var list = new List[Human]
list: List[Human] = List@4587fd42

scala> var list2  = list append(new Dog) // Dog gets downcasted to Creature
list2: List[Creature] = List@4bdc94ea


I In the expression list append(new Dog), the argument of the append method has to be a supertype of Human. The compiler searches for the closest common supertype of a Dog and Human classes, which is Creature, then it downcasts new Dog to Creature. As a result the return type of list append(new Dog) is List[Creature]. This makes a lot of sense because it contains the mixture of Human and Dog objects, which are both subtypes of Creature.

I hope this post was helpful to you. Please let me know what you think about it, and leave a comment.