mirror of
https://github.com/jlengrand/kotlin.git
synced 2026-05-10 08:31:29 +00:00
11 lines
363 B
Kotlin
11 lines
363 B
Kotlin
tailRecursive fun <T, A> Iterator<T>.foldl(acc : A, foldFunction : (e : T, acc : A) -> A) : A =
|
|
if (!hasNext()) acc
|
|
else foldl(foldFunction(next(), acc), foldFunction)
|
|
|
|
fun box() : String {
|
|
val sum = (1..1000000).iterator().foldl(0) { (e : Int, acc : Long) ->
|
|
acc + e
|
|
}
|
|
|
|
return if (sum == 500000500000) "OK" else "FAIL: $sum"
|
|
} |