Skip to content

Instantly share code, notes, and snippets.

@bitops
Created July 17, 2014 04:16
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 bitops/46c5617ebce39819b13b to your computer and use it in GitHub Desktop.
Save bitops/46c5617ebce39819b13b to your computer and use it in GitHub Desktop.
An accumulator function written in Swift.
// accumulator function
func accumulator(initial: Int, incrementBy: Int) -> () -> Int {
var value = initial
func accum() -> Int {
value += incrementBy
return value
}
return accum
}
// example usage
var myAccumulator = accumulator(0, 1)
myAccumulator() // => 1
myAccumulator() // => 2
myAccumulator() // => 3
myAccumulator() // => 4
myAccumulator() // => 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment