开发者

Using scala to accumulate elementwise sums [duplicate]

开发者 https://www.devze.com 2023-04-02 11:38 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: In Scala, how do I fold a List and return the intermediate results?
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

In Scala, how do I fold a List and return the intermediate results?

I am searching for a functional way of accumulating the elements of a Sorte开发者_如何学CdTree in the following way:

I want a list with containing the sums of all precursors (including the element itself) for all elements in the SortedTree.

For Exmaple:

SortedTree contains (0.4, 0.3, 0.2, 0.1, 0.05)

I want the list: (0,4. 0.7, 0. 9, 1, 1.05)

Any suggestions?


There is no SortedTree in the standard library, but you just want scanLeft (assuming your type extends TraversableLike):

seq.scanLeft(0.0)(_ + _).tail

or if you want a list:

seq.toList.scanLeft(0.0)(_ + _).tail
0

精彩评论

暂无评论...
验证码 换一张
取 消