Skip to content

Instantly share code, notes, and snippets.

@dce
Created April 26, 2010 04:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dce/378969 to your computer and use it in GitHub Desktop.
Save dce/378969 to your computer and use it in GitHub Desktop.
object FunctionalTest {
def fold(args: Array[Int], lambda: (Int, Int) => Int) : Int = {
var result = 0
args.foreach((arg: Int) => result = lambda(result, arg))
result
}
def fold2(lambda: (Int, Int) => Int) : (Array[Int]) => Int = {
def folded(args: Array[Int]) : Int = {
var result = 0
args.foreach((arg: Int) => result = lambda(result, arg))
result
}
folded
}
def main(args: Array[String]) {
println(fold(Array(1, 2, 3), (sum, item) => sum + item))
println(fold(Array(3, 4, 1), (max, item) => if(max > item) max else item))
println(fold2((sum, item) => sum + item)(Array(1, 2, 3)))
println(fold2((max, item) => if(max > item) max else item)(Array(3, 4, 1)))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment