Created
October 13, 2013 09:13
-
-
Save cy6erGn0m/6960104 to your computer and use it in GitHub Desktop.
Kotlin tail recursive foldl demo
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
fun <T, A> Iterator<T>.foldl(acc : A, foldFunction : (e : T, acc : A) -> A) : A = | |
if (!hasNext()) acc | |
else foldl(foldFunction(next(), acc), foldFunction) | |
val sum = (1..1000000).iterator().foldl(0) { (e : Int, acc : Long) -> | |
acc + e | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment