Skip to content

Instantly share code, notes, and snippets.

@ambientlight
Created February 2, 2020 12:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ambientlight/aba00f202038ccd1e86c1542302a2f6d to your computer and use it in GitHub Desktop.
Save ambientlight/aba00f202038ccd1e86c1542302a2f6d to your computer and use it in GitHub Desktop.
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