Skip to content

Instantly share code, notes, and snippets.

@hyone
Created May 29, 2011 08:24
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 hyone/997567 to your computer and use it in GitHub Desktop.
Save hyone/997567 to your computer and use it in GitHub Desktop.
Accumulator by Scala
object Accumulator {
def accumulator(n: Int): Int => Int = {
var i = n
(x: Int) => { i += x; i }
}
def main(args: Array[String]) {
val acc = accumulator(0)
(1 to 5).foreach { i => println(acc(i * 2)) }
}
}
// $ scalac Accumulator.scala
// $ scala Accumulator
// 2
// 6
// 12
// 20
// 30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment