Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to calculate running sum?

 
Peter Hsu
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2407
33
Linux Oracle Postgres Database Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Peter Hsu
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2407
33
Linux Oracle Postgres Database Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks. This really helps

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

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic