【发布时间】:2017-03-12 15:42:27
【问题描述】:
我有一个这样的代码 sn-p:
val step = Step.Montyly //this could be Yearly, daily, hourly, etc.
val (lower, upper) = (****, ****) //unix timestamps, which represent a time range
val timeArray = Array[Long](0)
var time = lower
while (time <= upper) {
timeArray +: = time
time = step.next(time) // eg, Step.Hourly.next(time) = time + 3600, Step.Monthly.next(time) = new DateTime(time).addMonths(1).toTimeStamp()
}
return timeArray
虽然这是用 Scala 编写的,但它是一种非功能性方式。我是 Scala 的新手,想知道这是否可以以功能方式重写。我知道以下内容:
for(i <- 1 to 10 by step) yield i
但是这里的step是一个固定值,如何让'i'可以由'next function'生成而不是固定值?
【问题讨论】:
标签: scala loops for-loop functional-programming yield