Sunday, November 10, 2013

Reactive Programming

Coursera is introducing a new course on Principals of Reactive Programming, which is a follow-on on their Functional Programming Scala course. I am not sure about the origins for the term reactive programming, but it seems it's now becoming a hot term in software development. They are also pointing to a Reactive Manifesto

After watching the first week of videos, it looks interesting, although some of the concepts like Monads could be confusing. As, I am following the class, I might share more information about it here.

Monday, October 7, 2013

Play Framework and Functional Programming

Check this post if you are interested in the Play Framework and how functional programming is used in a web framework. This is an example of composable data manipulation:


If you are confused by the Iteratees mentioned in previous post, take a look at this post.

Saturday, September 7, 2013

Iteration over Tuples in Scala

Tuples are a useful component in Scala. They are used as containers of related objects that can be passed together as an entity. Unlike other collections like Lists, they can hold objects of different types, which results in some of its limitations compared to these collections. Tuples cannot be built dynamically like Lists and their length and element types need to be known beforehand. In some situations, like when reading from a database, the default entities returned back are Tuples, so knowing how to deal with them becomes necessary.

As pointed above, some of the operations that are simple to do on Lists aren't possible on Tuples. Take the following 2 examples:
(1, 2.3) + 1 => (2, 3.3)
(2, 3) + (5, 7) => (7, 10)
There is no straightforward way for doing these operations, and if you try to convert to Lists (assuming all values are of the same type or can be casted to the same type) and use List operations, you can do this by
val t = (2, 3)
val l = t.productIterator.toList
but then you will have type-safety issues and handling the List l won't be as straight-forward, since l is of type List[Any].

Let us look at a simple example, where you can get an Iterator to a Tuple and do some processing on it:
val t = (2, 3)
val it = t.productIterator
and even
it.foreach( x => println(x.isInstanceOf[Int]) )
returns true,
it.foreach( x => println(x+1) )
returns an error: type mismatch; found : Int(1) required: String

For this to work you have to use asInstanceOf[...] like this
it.foreach( x => println(x.asInstanceOf[Int]+1) )
In the case when the Tuple elements are of different types, one elegant approach is to use Scala pattern matching as follows:
it.foreach {
    case i: Int => println(i+1)

    case d: Double => println(d+1)
}
There have been some libraries developed to make it easier to deal with these cases. For example, shapeless provides a new type HList that you can read more about here, but that topic is for another post.

Sunday, August 25, 2013

Binary Search in Scala


Scala is a programming language that supports both object-oriented as well as functional programming paradigms. Many of the common tasks can be implemented in different ways in Scala and it becomes a matter of style which approach to choose. In this post, I picked one of the classic algorithms, Binary Search, showing how to implement in different ways in Scala. The code is showing 3 functions for binary search: an iterative, recursive, and the functional programming, pattern matching, approach Scala is popular for.

In addition to the difference in style, I was also curious if there are performance differences between these implementations. So, I ran a small experiment, generating a list of 1 billion items, and searching the list 1 million times (each time a different random item is searched) using each of the 3 functions. The results were close, around 1 second total in each of the 3 cases. So, unless different results are observed, I am assuming all approaches are equivalent performance-wise and it's a matter of style and preference which one to use.

It worth mentioning, that I ran the same experiment using java.util.Arrays.binarySearch and the result was also similar.