Created
January 30, 2013 02:50
-
-
Save sshark/4670183 to your computer and use it in GitHub Desktop.
Curry and functions flexibilities
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
/** | |
* | |
* @author Lim, Teck Hooi | |
* | |
* <br/><br/>30/01/2013 10:19 AM | |
* | |
*/ | |
object MapReduce { | |
def mapReduce(f : Int => Int, combine : (Int, Int) => Int, zero : Int)(a:Int, b: Int) : Int = | |
if (a > b) zero | |
else combine(f(a), mapReduce(f, combine, zero)(a + 1, b)) | |
def product(f:Int => Int)(a : Int, b : Int) : Int = | |
mapReduce(f, (x, y) => x * y, 1)(a, b) | |
product(x => x * x)(3, 4) | |
def fact(n : Int) = product((n => n))(1, n) | |
fact(5) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment