Skip to content

Instantly share code, notes, and snippets.

@banterCZ
Last active August 29, 2015 14:03
Show Gist options
  • Save banterCZ/158e55d1f05de6fc58f2 to your computer and use it in GitHub Desktop.
Save banterCZ/158e55d1f05de6fc58f2 to your computer and use it in GitHub Desktop.
We want to write a function that generates accumulators-- a function that takes a number n, and returns a function that takes another number i and returns n incremented by i. See the article Revenge of the Nerds http://www.paulgraham.com/icad.html This Scala solution comes from http://rosettacode.org/wiki/Accumulator_factory#Scala
def AccumulatorFactory[N](n: N)(implicit num: Numeric[N]) = {
import num._
var acc = n
(inc: N) => {
acc = acc + inc
acc
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment