aspose file tools*
The moose likes Scala and the fly likes How to calculate running sum? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Languages » Scala
Bookmark "How to calculate running sum?" Watch "How to calculate running sum?" New topic
Author

How to calculate running sum?

Peter Hsu
Ranch Hand

Joined: Aug 25, 2006
Posts: 72
When doing dynamic programming, it's very often that you need to calculate a running sum to an array. I decided that I want to write a function that does this. But I got stuck. Can someone help me?

I wanted to write something like:


Another crappy version that won't compile either, of course
chris webster
Bartender

Joined: Mar 01, 2009
Posts: 1871
    
  16

If you simply have a list of numbers, you could use the List's sum() method directly (min() and max() methods are also available):

A more generic approach is to use foldLeft() with a starting value of 0, so an empty list would return a sum of zero here (example stolen from Matt Malone's blog):

Could just use place-holders instead of running and current:

map() applies the given function to each element in the original list and yields another list, not a single value e.g. double the integers in your original list:

You might use map() to convert a List of more complicated objects into a series of numbers you could then add up, for example:

The same methods should work on other collection types e.g. Arrays as well. Scala's collections offer a lot of extra functionality (more than Java) so it's worth looking through the relevant APIs before you try to re-invent the wheel. Also check out Effective Scala on collections, and 10 Scala One Liners To Impress Your Friends for a quick insight into how concise and elegant Scala code can be.


No more Blub for me, thank you, Vicar.
Peter Hsu
Ranch Hand

Joined: Aug 25, 2006
Posts: 72
chris webster wrote:If you simply have a list of numbers, you could use the List's sum() method directly (min() and max() methods are also available):
The same methods should work on other collection types e.g. Arrays as well. Scala's collections offer a lot of extra functionality (more than Java) so it's worth looking through the relevant APIs before you try to re-invent the wheel. Also check out Effective Scala on collections, and 10 Scala One Liners To Impress Your Friends for a quick insight into how concise and elegant Scala code can be.


hmm... I am aware of these methods. But none of them helped me on my problem.
Note that I want the following
input
1, 3, 4, 5
output
1, 4, 8, 13
chris webster
Bartender

Joined: Mar 01, 2009
Posts: 1871
    
  16

Peter Hsu wrote:hmm... I am aware of these methods. But none of them helped me on my problem. Note that I want the following
input
1, 3, 4, 5
output
1, 4, 8, 13

Sorry - didn't register that you were after the partial sum. There's a scanLeft() method on the collection classes which seems to do the trick, although it puts the starting element (0) into the output list as well, so you need to take the tail of the resulting list:

Peter Hsu
Ranch Hand

Joined: Aug 25, 2006
Posts: 72
thanks. This really helps

Assuming we don't want to trim the zero, this is it:

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to calculate running sum?