scala - convert timestamps to intervals -
i wrote scala function converts list of timestamps intervals
def tointervals(timestamps: list[string]) = { def helper(timestamps: list[string], accu: list[long]): list[long] = { if (timestamps.tail.isempty) accu.reverse else { val first = timestamps.head.tolong val second = timestamps.tail.head.tolong val newhead = second - first helper(timestamps.tail, newhead :: accu) } } helper(timestamps, list()) }
and without tailcall
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) } }
but have feeling there one/two liner e.g map2 . advice?
(timestamps.tail, timestamps).zipped.map(_.tolong - _.tolong)
is one-liner; it's more efficient val times = timestamps.map(_.tolong)
once, though (which make two-liner).
Comments
Post a Comment