Scala使用外部val生成时间序列数据

时间:2018-06-27 00:24:46

标签: scala time-series java-time

我尝试从start_time生成数据的数组/列表,所以我的代码是

var temp = parse_time

// for (i <- 1 to 10) yield temp.plusSeconds(600) // method 1

val max = 10
Range (0, max).map( _ => (temp.plusSeconds(600))) // method 2

这两种方法都将对数组中的数据产生相同的结果,而无需进行任何增量更改。

有人可以帮我弄清楚为什么以及如何解决它吗?

3 个答案:

答案 0 :(得分:2)

如果要从上一个生成下一个条目(以temp开头),请尝试List.iterate

List.iterate(temp, max)(_.plusSeconds(600))

这是整数的作用:

List.iterate(42, 10)(_ + 600)

产生:

List(42, 642, 1242, 1842, 2442, 3042, 3642, 4242, 4842, 5442)

这里是java.time.LocalDateTime的另一个示例:

List.iterate(LocalDateTime.now(), 10)(_.plusSeconds(600)) foreach println

输出:

2018-06-27T12:52:53.237
2018-06-27T13:02:53.237
2018-06-27T13:12:53.237
2018-06-27T13:22:53.237
2018-06-27T13:32:53.237
2018-06-27T13:42:53.237
2018-06-27T13:52:53.237
2018-06-27T14:02:53.237
2018-06-27T14:12:53.237
2018-06-27T14:22:53.237

答案 1 :(得分:2)

List.tabulate(max)(i => temp.plusSeconds(600 * i))

答案 2 :(得分:1)

由于.plusSeconds产生了一个新值,而不是变异原始值,所以您得到的所有结果加600。

下面是示例LocalDateTimeOffsetDateTime

scala> import java.time.LocalDateTime
import java.time.LocalDateTime

scala> val temp = LocalDateTime.now
temp: java.time.LocalDateTime = 2018-06-26T17:31:36.858

scala> Range (0, 10).map( _ => (temp.plusSeconds(600)))
res1: scala.collection.immutable.IndexedSeq[java.time.LocalDateTime] = Vector(
     2018-06-26T17:41:36.858, 
     2018-06-26T17:41:36.858, 
     2018-06-26T17:41:36.858, 
     2018-06-26T17:41:36.858, 
     2018-06-26T17:41:36.858, 
     2018-06-26T17:41:36.858, 
     2018-06-26T17:41:36.858, 
     2018-06-26T17:41:36.858, 
     2018-06-26T17:41:36.858, 
     2018-06-26T17:41:36.858)

如果要增量使用.plusSeconds,可以使用累加器模式,

scala>     def add(start: Int, end: Int, date: LocalDateTime, dates: List[LocalDateTime]): List[LocalDateTime] = {
     |       if (  start == end ) dates
     |       else {
     |         val newDate = date.plusSeconds(600)
     |         add(start + 1, end, newDate, dates :+ newDate)
     |       }
     |     }
add: (start: Int, end: Int, date: java.time.LocalDateTime, dates: List[java.time.LocalDateTime])List[java.time.LocalDateTime]

scala> add(0, 10, LocalDateTime.now(), List.empty)
res19: List[java.time.LocalDateTime] = List(
2018-06-26T18:10:23.055, 
2018-06-26T18:20:23.055, 
2018-06-26T18:30:23.055, 
2018-06-26T18:40:23.055, 
2018-06-26T18:50:23.055, 
2018-06-26T19:00:23.055, 
2018-06-26T19:10:23.055, 
2018-06-26T19:20:23.055, 
2018-06-26T19:30:23.055, 
2018-06-26T19:40:23.055)

或者您也可以使用List.iterate,它基本上也使用可变的累加器模式。

  def iterate[A](start: A, len: Int)(f: A => A): CC[A] = {
    val b = newBuilder[A]
    if (len > 0) {
      b.sizeHint(len)
      var acc = start
      var i = 1
      b += acc

      while (i < len) {
        acc = f(acc)
        i += 1
        b += acc
      }
    }
    b.result()
  }