Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Loop with a sum
let nums = [1, 2, 3, 6, 16, 4]
let accumulator: (Int, Int) -> Int = { (sum, increment) in
sum + increment
}
var sum = 0
for var i in (0..<nums.count) {
sum = accumulator(sum, nums[i])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment