1

I have an [Int] array that looks like this:

1 2 1 4 3 ...

I want to transform it into an array of running subtotals that looks like this:

1 3 4 8 11 ...

It's easy enough to do using a for loop:

var subTotal = 0
var subTotalArray = [Int]()
for int in intArray {
    subTotal = subTotal + int
    subTotalArray.append(subTotal)
}

I'm just curious if there's a more elegant, tighter, and hopefully easy-to-read way of doing this using some of Swift's higher-order functions.

hkatz
  • 951
  • 11
  • 21

0 Answers0