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(_ - _)
}
Written by lukas
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Scala
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#