Last Updated: February 25, 2016
·
1.493K
· lukasz-madon

Converting timestamps to intervals in Scala

I wrote a simple function that converts timestamps to intervals between them.

def toIntervals(timestamps: List[String]): List[Long] = {
  if (timestamps.tail.isEmpty) List()
  else {
    val first = timestamps.head.toLong
    val second = timestamps.tail.head.toLong
    val newHead = second - first
    newHead :: toIntervals(timestamps.tail)
  }
 }

Good nice little helper, but you can do better!

def toIntervals(timestamps: List[String]): List[Long] = {
     val times = timestamps.map(_.toLong)
     (timestail, times).zipped.map(_ - _)
}