Difference between fold and foldLeft or foldRight?

前端 未结 7 1785
星月不相逢
星月不相逢 2020-11-29 20:43

NOTE: I am on Scala 2.8—can that be a problem?

Why can\'t I use the fold function the same way as foldLeft or foldRight?

7条回答
  •  感情败类
    2020-11-29 21:38

    For your particular example you would code it the same way you would with foldLeft.

    val ns = List(1, 2, 3, 4)
    val s0 = ns.foldLeft (0) (_+_) //10
    val s1 = ns.fold (0) (_+_) //10
    assert(s0 == s1)
    

提交回复
热议问题