1

test("ok") 是从 Noel Welsh 和 Dave Gurnell 第 254 页("D.4 Safer Folding using Eval")的书 "scala with cats" 中复制的,代码运行良好,它是蹦床 foldRight

import cats.Eval
test("ok") {
val list = (1 to 100000).toList


def foldRightEval[A, B](as: List[A], acc: Eval[B])(fn: (A, Eval[B]) => Eval[B]): Eval[B] =
  as match {
    case head :: tail =>
      Eval.defer(fn(head, foldRightEval(tail, acc)(fn)))
    case Nil =>
      acc
  }

def foldRight[A, B](as: List[A], acc: B)(fn: (A, B) => B): B =
  foldRightEval(as, Eval.now(acc)) { (a, b) =>
    b.map(fn(a, _))
  }.value

val res = foldRight(list, 0L)(_ + _)

assert(res == 5000050000l)
}

test("ko") 为小列表返回相同的 test("ok") 值,但对于长列表,该值不同。为什么?

test("ko") {
val list = (1 to 100000).toList

def foldRightSafer[A, B](as: List[A], acc: B)(fn: (A, B) => B): Eval[B] = as match {
  case head :: tail =>
    Eval.defer(foldRightSafer(tail, acc)(fn)).map(fn(head, _))
  case Nil => Eval.now(acc)
}

val res = foldRightSafer(list, 0)((a, b) => a + b).value

assert(res == 5000050000l)
}
4

1 回答 1

1

这是@OlegPyzhcov 的评论,已转换为社区 wiki 答案

您忘记了L作为0L第二个参数传递给foldRightSafer. 因此,调用的推断泛型类型是

foldRightSafer[Int, Int]((list : List[Int]), (0: Int))((_: Int) + (_: Int))

所以你的加法溢出并给你小于2000000000(9个零,Int.MaxValue = 2147483647)的东西。

于 2018-02-21T12:54:40.200 回答