Scala foldLeft while some conditions are true
问题 How to emulate following behavior in Scala? i.e. keep folding while some certain conditions on the accumulator are met. def foldLeftWhile[B](z: B, p: B => Boolean)(op: (B, A) => B): B For example scala> val seq = Seq(1, 2, 3, 4) seq: Seq[Int] = List(1, 2, 3, 4) scala> seq.foldLeftWhile(0, _ < 3) { (acc, e) => acc + e } res0: Int = 1 scala> seq.foldLeftWhile(0, _ < 7) { (acc, e) => acc + e } res1: Int = 6 UPDATES: Based on @Dima answer, I realized that my intention was a little bit side