Created
April 29, 2014 08:13
-
-
Save kaisellgren/11393741 to your computer and use it in GitHub Desktop.
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
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