Created
April 26, 2010 04:23
-
-
Save dce/378969 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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