我写了一个 scala 函数,将时间戳列表转换为间隔
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())
}
并且没有尾声
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)
}
}
但我有一种感觉,它有一个/两个班轮,例如 map2 。有什么建议吗?