I\'d like a function runningSum
on an array of numbers a (or any ordered collection of addable things) that returns an array of the same length where each eleme
Just for fun: The running sum as a one-liner:
let arr = [1, 2, 3, 4]
let rs = arr.map({ () -> (Int) -> Int in var s = 0; return { (s += $0, s).1 } }())
print(rs) // [1, 3, 6, 10]
It does the same as the (updated) code in JAL's answer, in particular, no intermediate arrays are generated. The sum variable is captured in an immediately-evaluated closure returning the transformation.