Scala: Producing the intermediate results of a fold

前端 未结 4 1018
忘了有多久
忘了有多久 2020-12-28 08:52

I\'ve come across the problem of maintaining a state throughout a map operation several times. Imagine the following task:

Given a List[Int], map each element to the

4条回答
  •  一个人的身影
    2020-12-28 09:43

    @Dario gave the answer, but just to add that the scala library provides scanLeft:

    scala> List(1,2,3).scanLeft(0)(_ + _)
    res26: List[Int] = List(0, 1, 3, 6)
    

提交回复
热议问题