I'm struggling to find simple-functional way to transform
val ints = List(1, 2, 3, 4, 5) into List(1, 3, 6, 10, 15)
How can it be done?
This operation is called prefix sum, cumulative sum, or inclusive scan, the more generalized higher-order function is usually called scan. Scala provides :A,That](z:B)(op:(B,B)=>B)(implicitcbf:scala.collection.generic.CanBuildFrom[Repr,B,That]):That">scan as part of its collections library:
ints.scan(0)(_ + _).tail
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