Monday, May 2, 2011

Scala days 2011

The premier event for Scala enthusiasts, researchers, and practitioners Scala Days 2011 will be held at Stanford, California on the 2nd and 3rd of June 2011. Check out the official web site .

Saturday, March 19, 2011

Remote logging with Log4j

I'm writing about Log4j's Socket Appender. I can't just use and stay quiet. It's just really useful. So here is the case. You have a web application running in multiple instances behind a load balancer. Each server is going to have a log file and yes it's hard too track the log files down across multiple instances. If an instance is terminated you might even lose the log file. So we are gonna need a central log server that aggregates the log files to the central log server and this is done by log4j's Socket Appender.

I'll demonstrate it on my local computer but it would simply work for the case. I assume you have already installed and configured log4j for you application.

First we need to setup the log server. For this we use that can service multiple SocketAppender clients. SimpleSocketListener waits for logging events from SocketAppender clients. After reception the events are logged according to the server policy. It takes two parameters: port and configFile; where port is the port to listen on and configFile is configuration script in properties or XML format.

Tuesday, March 1, 2011

Jorge Ortiz on Panel: Scaling with Scala

Viktor Klang on Building loosely coupled and scalable systems using EDA

Paul Chiusano on Actors: can we do better?

Christopher League on Continuations and Other Functional Patterns

Josh Suereth on Implicits without import tax: How to make clean APIs with implicits

Nathan Hamblen on Building an HTTP streaming API with Scala

Harry Heymann on Advanced Lift Techniques

Jonas Bonér on Akka: Simpler Scalability, Fault-Tolerance, Concurrency & Remoting through Actors

Runar Bjarnason on The Guerrilla Guide to Pure Functional Programming

Nermin Serifovic on Scala Performance Considerations

Mark Harrah sbt 0.9: Why, what, how?

Daniel Spiewak on Extreme Cleverness: Functional Data Structures in Scala

Sunday, February 27, 2011

Scala implicit conversion

Implicit conversion helps us to convert between types without explicitly calling conversion methods. Let's take a look at following example.

We have a list of names.
val xs = List("John", "Jack", "Boby")
If we assign it to a string value, scala will complain that there is a type mismatch error.
scala> val s : String  = xs  
<console>:6: error: type mismatch;
 found   : List[java.lang.String]
 required: String
       val s : String  = xs

Let's define the implicit conversion here.

implicit def list2String(l: List[String]) = l.foldLeft(""){ (sum, x) => sum + x + (if (x == l.last) "" else ", ")}
and let's try the assignment now.
scala> val s: String = xs
s: String = John, Jack, Boby

It saves a lot time and code. But we have to follow several rules.

Implicit conversion rules
  1. Marking Rule: Only definitions marked implicit are available.
  2. Scope Rule: An inserted implicit conversion must be in scope as a single
    identifier, or be associated with the source or target type of the conver-
  3. Non-Ambiguity Rule: An implicit conversion is only inserted if there is
    no other possible conversion to insert.
  4. One-at-a-time Rule: Only one implicit is tried.
  5. Explicits-First Rule: Whenever code type checks as it is written, no implicits are attempted.
Look at Predef methods. It has a number of implicit converters which imported automatically.

Programming in Scala by Martin Odersky, Lex Spoon, Bill Venners

Saturday, February 26, 2011

Scala basic data types

Value type Range
Byte 8-bit signed two’s complement integer (-27 to 27 - 1, inclusive)
Short 16-bit signed two’s complement integer (-215 to 215 - 1, inclusive)
Int 32-bit signed two’s complement integer (-231 to 231 - 1, inclusive)
Long 64-bit signed two’s complement integer (-263 to 263 - 1, inclusive)
Char 16-bit unsigned Unicode character (0 to 216 - 1, inclusive)
Stringa sequence of Chars
Float 32-bit IEEE 754 single-precision float
Double 64-bit IEEE 754 double-precision float
Boolean true or false

Data types shown above reside in package scala except String! String is a member of java.lang package and it's automatically imported therefore it's implicitly converted to scala's WrappedString class (it used to convert to RichString before scala 2.8.1). We can call WrappedString's methods on a string. Voila!

Friday, February 25, 2011

Scala Lists

Scala List is one of heavily used data structures in Scala. It's immutable and is designed to enable functional programming style.

Initializing a list
val xs = List(1, 3, 4)

This creates an instance val named xs, initialized with a new List[Int] with the integer elements 1, 3, and 4.

Remember that List is an abstract class so val xs = new List(1, 2, 3) would not work! Then how does this create a list? When we write List(1,3, 4) we are implicitly calling apply method of List companion object. More about companion object and apply method will be another post. For now let's think List.apply as Static object with a factory method.