scala - 如何使用 fold 做 bool 测试

标签 scala datetime fold

我想知道在 Scala 中解决这个问题的惯用方法。

给定开始日期和结束日期以及介于两者之间的日期集合,确定给定的日期集合是否包含从开始日期到结束日期所需的所有日期且中间没有间隔日期。

类型签名:

def checkDate(开始:DateTime,结束:DateTime,介于:IndexedSeq[DateTime]): bool 值

执行此操作的“正常”或“无效”方式如下:

def checkDate(start: DateTime, end: DateTime, between: IndexedSeq[DateTime]): Boolean = {
  i = 1
  status = true
  while(start != end) {
    d = start.plusDays(i)
    if (!between.contains(d) {
      status = false
      break
    }
    i += 1
  }
  return status
}

我如何使用折叠来做到这一点?

到目前为止,这是我的思考过程:

def checkDate(start: DateTime, end: DateTime, between: IndexedSeq[DateTime]): Boolean = {

  // A fold will assume the dates are in order and move left (or right)
  // This means the dates must be sorted.
  val sorted = between.sortBy(_.getMillis())

  val a = sorted.foldLeft(List[Boolean]) {
    (acc, current) => {
      // How do I access an iterable version of the start date?
      if (current == ??) {
        acc :: true
      } else false
    }
  }

  // If the foldLeft produced any values that could NOT be matched
  // to the between list, then the start date does not have an 
  // uninterrupted path to the end date.
  if (a.count(_ == false) > 0) false
  else true
}

我只需要弄清楚如何索引 start 参数,这样我就可以在折叠遍历 between 集合时增加它的值。或者可能 fold 根本不是我应该使用的。

如有任何帮助,我们将不胜感激!

最佳答案

您可以在累加器中传递之前的 DateTime 项:

val a = sortedBetween.foldLeft((List[Boolean](), start)) {
  case ((results, prev), current) => {
    ... calculate res here ...
    (results ++ List(res), current)
  }
}

但对于这种检查,您最好使用滑动和 forall 组合:

 sortedBetween.sliding(2).forall {
   case List(prev,cur) => ..do the check here ..
 }

另外,请注意您忽略了排序之间的结果,因为 IndexedSeq 是不可变的。修复 - 使用另一个 val:

val sortedBetween = between.sortBy(_.getMillis())

关于scala - 如何使用 fold 做 bool 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46841897/

相关文章:

scala - Apache Kafka - 向后分支 162 上存在未初始化的对象

Haskell Foldl 无法构造无限类型

javascript - 如何检查 DST(夏令时)是否有效,如果有效,偏移量?

SQL Server 2008 BETWEEN 函数

haskell - foldr 和 foldr1 Haskell

haskell - 折叠函数不断切换var位置

scala - 根据两个数组列的交集大小连接两个 Spark DataFrame

scala - 如何为默认构造函数参数定义访问器方法?

Scala:将元组解包为参数列表的一部分

c# - 使用 open xml sdk 从 xlsx 中读取日期