0

我刚刚开始我的 scala 之旅。我正在尝试定义一个隐式转换,它可以在一段时间内以这种方式迭代每一天:

for (day <- firstDay until lastDay) {
  // a lot of interesting code goes here
}

到目前为止,我设法做的是这样的:

implicit class DateTimeWithUntil(from: DateTime) {
  def until(to: DateTime): Stream[DateTime] =
    from #:: from.plusDays(1)
}

实现直到方法的方法是什么?流适合这个吗?还是应该是迭代器或序列?或其他?

谢谢

4

1 回答 1

2

我想你正在寻找这样的东西:

import org.joda.time._

implicit class DateTimeOps (startDt: DateTime) {
  def until(endDt: DateTime) = for(dayNo <- 0 until Days.daysBetween(startDt, endDt).getDays) yield(startDt.plusDays(dayNo))
}

for(day <- new DateTime() until new DateTime().plusDays(10)) println (day)

希望能帮助到你。

于 2013-10-18T14:13:35.423 回答