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.

2 comments:

  1. Piston Bbobet Pro Titanium Straightener - TitaniumArt
    POTON Bbobet tube supplier Pro Titanium Straightener. 100% Free Shipping. $5.00. Quantity. Add to titanium legs cart. $1.00. Quantity. Add to cart. $1.00. Quantity. Add to cart. $1.00. Quantity. Add to samsung galaxy watch 3 titanium cart. $1.00. Quantity. Add to cart. $1.00. Quantity. Add to titanium scrap price cart. $1.00. Quantity. Add to cart. $1.00. Quantity. Add to cart. $1.00. Quantity. Add to cart. $1.00. titanium white octane Quantity. Add to cart. $1.00. Quantity. Add to cart. $1.00. Quantity. Add to cart. $1.00. Quantity. Add to cart. $1.00

    ReplyDelete