Showing posts with label scala. Show all posts
Showing posts with label scala. Show all posts

Tuesday, October 13, 2009

P09: Pack consecutive duplicates of list elements into sublists.

This might be a good candidate for recursive solution. But much faster iterative solution is shown below:


import scala.collection.mutable.ListBuffer
def unflatten[T](list:List[T]):List[List[T]]={
val uniq = list.removeDuplicates
val c = new ListBuffer[List[T]]()
var uniqv = uniq
while(!uniqv.isEmpty){
c.append(list.filter(_==uniqv.head))
uniqv=uniqv.tail
}
c.toList
}
val x = List(1,2,3,1,2,3,1,2,3,1,2,3)
println(unflatten(x))

Output:
List(List(1, 1, 1, 1), List(2, 2, 2, 2), List(3, 3, 3, 3))

Thursday, October 8, 2009

P08: Eliminate consecutive duplicates of list elements.

The simple solution:
scala> val x = List(1,2,2,3,4,4,5,5)
x: List[Int] = List(1, 2, 2, 3, 4, 4, 5, 5)

scala> x.removeDuplicates
res0: List[Int] = List(1, 2, 3, 4, 5)

For the curious under the hood:

scala> import scala.collection.mutable.ListBuffer
import scala.collection.mutable.ListBuffer

scala> def myRemoveDuplicates[T](in:List[T]):List[T] = {
| val b = new ListBuffer[T]
| var temp = in
| while(!temp.isEmpty){
| if(!temp.tail.contains(temp.head)) b+=temp.head
| temp = temp.tail
| }
| b.toList
| }
myRemoveDuplicates: [T](in: List[T])List[T]

scala> myRemoveDuplicates(x)
res2: List[Int] = List(1, 2, 3, 4, 5)

Wednesday, October 7, 2009

P07: Flatten a list

Just use flatten method:
scala> val m = List(List(1, 2), List(3, 4))
res2: List[List[Int]] = List(List(1, 2), List(3, 4))

scala> m.flatten
res3: List[Int] = List(1, 2, 3, 4)

Just out of sheer curiosity, here another implementation with much better performace:

scala> import scala.collection.mutable.ListBuffer
import scala.collection.mutable.ListBuffer

scala> def myFlatten[T](nl:List[List[T]]):List[T] = {
| val b = new ListBuffer[T]
| for(list<-nl){
| var tlist = list
| while(!tlist.isEmpty){
| b+=tlist.head
| tlist = tlist.tail
| }
| }
| b.toList
| }
myFlatten: [T](nl: List[List[T]])List[T]

scala> myFlatten(m)
res1: List[Int] = List(1, 2, 3, 4)

Tuesday, October 6, 2009

P06: Is a list palindrome

scala> val plist = List(1, 2, 3, 2, 1)
res19: List[Int] = List(1, 2, 3, 2, 1)

scala> plist == plist.reverse
res20: Boolean = true

P03 - P05: Find nth element, length, reverse a list

scala> val list = List(1,2,3,4,5)
list: List[Int] = List(1, 2, 3, 4, 5)

scala> list(4)
res0: Int = 5

scala> list.length
res1: Int = 5

scala> list.reverse

scala> list.reverse
res2: List[Int] = List(5, 4, 3, 2, 1)

Monday, October 5, 2009

How to simply sum values in a Map in Scala

Here is a code snippet that sums up the values in a map in Scala:
m.foldLeft(0)(_+_._2)

It may look cryptic at first but once you get the Scala feel, you will appreciate its conciseness and elegance. Here below is the explanation:
m stands for a Map, foldLeft is a method that takes an initial value (0 here since sum is 0 at the beginning) and feeds it to its next iteration on this map entry. Note that the first _ stands for the result of previous iteration result. And _+_._2 means:
take previous result and add it to the next map entry's value, which _._2 stands for. In Scala, Map values can be accessed as Tuples. _1 is the key and _2 is the value. Here is a working example:

scala> val m = Map("one"->1,"two"->2,"three"->3)
m: scala.collection.immutable.Map[java.lang.String,Int] = Map(one -> 1, two -> 2, three -> 3)
scala> m.foldLeft(0)(_+_._2)
res0: Int = 6

Note: You can change + to * and use 1 for the initial value to get the product of values.

P02: Find the last but one element of a list.

scala> List(1,2,3,4,5).takeRight(2).head
res0: Int = 4

Friday, October 2, 2009

Over all these years of Software development, I came to really value the KISS principle, as the name of the blog suggests. Keeping up with a good programmer tradition of learning a new language every year, my journey took me from Ruby -> Groovy ->Now Scala. I used both Ruby and Groovy for rapid prototyping and where I feel it simplifies things that I am working on. Since I started to wonder in the Scala world, I started to feel strongly that Scala have chosen me over other of my favorites namely Ruby and Groovy, which by the way I still use and love. Writing programs in Scala feels so natural to me now that I really wish it is the only language that I work with. There are lots of Scala tutorials on the web and I won't be starting one. I recently came across an interesting site that talks about 99 things in Scala(http://aperiodic.net/phil/scala/s-99/). Most of problems has solutions in more than one way, which prompted me to blog about writing one and only one simple way of solving them. I will gradually work my way with them as my schedule allows me. Without much rant, here is the simple solution to first problem:
P1: Find the last element of a list

A: scala> List(1,2,3,4,5).last
res0: Int = 5