Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Kotlin tail recursive foldl demo
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