What is the slickest, most Ruby-like way of calculating the cumulative sum of an array?
Example:
[1,2,3,4].cumulative_sum
should return
[1,3,6,10]
class Array def cumulative_sum sum = 0 self.map{|x| sum += x} end end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With