2

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 SortedTree 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?

Community
  • 1
  • 1
peri4n
  • 1,389
  • 13
  • 24

1 Answers1

9

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
Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487