Skip to content

Instantly share code, notes, and snippets.

@cy6erGn0m
Created October 13, 2013 09:13
Show Gist options
  • Save cy6erGn0m/6960104 to your computer and use it in GitHub Desktop.
Save cy6erGn0m/6960104 to your computer and use it in GitHub Desktop.
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