Skip to content

Instantly share code, notes, and snippets.

@orionll
Last active August 29, 2015 14:13
Show Gist options
  • Select an option

  • Save orionll/b77364bd4182ce0b6bf9 to your computer and use it in GitHub Desktop.

Select an option

Save orionll/b77364bd4182ce0b6bf9 to your computer and use it in GitHub Desktop.
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