Skip to content

Instantly share code, notes, and snippets.

@kaisellgren
Created April 29, 2014 08:13
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 kaisellgren/11393741 to your computer and use it in GitHub Desktop.
Save kaisellgren/11393741 to your computer and use it in GitHub Desktop.
def kaiFold[A, B](stuff: Seq[A], initial: B)(predicate: (B, A) => B): B = {
@tailrec
def it(items: Seq[A], accumulator: B): B = {
if (items.length == 0) accumulator
else {
val nextAccumulator = predicate(accumulator, items.head)
it(items.tail, nextAccumulator)
}
}
it(stuff, initial)
}
val stuff = Vector(1, 2, 3)
kaiFold(stuff, 0)((p: Int, e: Int) => p + e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment