Skip to content

Instantly share code, notes, and snippets.

@orionll
Last active Aug 29, 2015
Embed
What would you like to do?
fun main(args : Array<String>) {
val arr = array(1, 2, 3)
println(foldLeft(0, arr, {x, y -> x + y}))
}
fun foldLeft<A, B>(z: B, array: Array<A>, f: (A, B) -> B): B {
[tailRecursive] fun go(i: Int, z: B) =
if (i == array.size()) z
else go(i + 1, f(array.get(i), z))
return go(0, z)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment