7z0kxg
Last Updated: February 25, 2016
·
850
· lukasz-madon
B2bc42f69510d6a1d2c9cde1c2214d24

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(_ - _)
}
Say Thanks
Respond