Created
May 29, 2011 08:24
-
-
Save hyone/997567 to your computer and use it in GitHub Desktop.
Accumulator by Scala
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
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