2
我寫了轉換時間戳列表間間隔轉換時間戳間間隔
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)
}
}
Scala的功能,但我有一種感覺,有一個一/二襯裏它例如map2。有什麼建議?