Created
July 17, 2014 04:16
-
-
Save bitops/46c5617ebce39819b13b to your computer and use it in GitHub Desktop.
An accumulator function written in Swift.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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