Scala: Producing the intermediate results of a fold

前端 未结 4 1009
忘了有多久
忘了有多久 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:31

    I like to fold around just like everybody else, but a less FP answer is very concise and readable:

     a.map{var v=0; x=>{v+=x; v}}
    

提交回复
热议问题