我想知道在 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/