我写了一个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。有什么建议吗?

最佳答案

(timestamps.tail, timestamps).zipped.map(_.toLong - _.toLong)


是你的单线;但是,仅一次val times = timestamps.map(_.toLong)效率更高(这将使其成为两线)。

关于scala - 将时间戳转换为间隔,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16547686/

10-10 19:38