Skip to content

Instantly share code, notes, and snippets.

@sshark
Created January 30, 2013 02:50
Show Gist options
  • Save sshark/4670183 to your computer and use it in GitHub Desktop.
Save sshark/4670183 to your computer and use it in GitHub Desktop.
Curry and functions flexibilities
/**
*
* @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