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
@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)