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.
Showing posts with label scala. Show all posts
Showing posts with label scala. Show all posts
Monday, October 7, 2013
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)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
(2, 3) + (5, 7) => (7, 10)
val t = (2, 3)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].
val l = t.productIterator.toList
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)and even
val it = t.productIterator
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 {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.
case i: Int => println(i+1)
case d: Double => println(d+1)
}
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.
It worth mentioning, that I ran the same experiment using java.util.Arrays.binarySearch and the result was also similar.
Subscribe to:
Posts (Atom)