Created
May 31, 2016 01:05
-
-
Save anujku/296a541cbffa12e93d1d8df17459693f to your computer and use it in GitHub Desktop.
Scala Currying Examples
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
def product(func: Int => Int)(a: Int, b: Int): Int = { | |
if (a > b) | |
1 | |
else | |
func(a) * product(func)(a + 1, b) | |
} | |
product((x) => (x + x))(1, 3) | |
def fact(a: Int): Int = { | |
product((a) => (a))(1, a) | |
} | |
fact(5) | |
def mapReduce(func: Int => Int, combine: (Int, Int) => Int, baseValue: Int)(a: Int, b: Int): Int = { | |
if (a > b) baseValue | |
else combine(func(a), mapReduce(func, combine, baseValue)(a + 1, b)) | |
} | |
def productMR(func: Int => Int)(a: Int, b: Int): Int = mapReduce(func, (x, y) => x * y, 1)(a, b) | |
def sumMR(func: Int => Int)(a: Int, b: Int): Int = mapReduce(func, (x, y) => x + y, 0)(a, b) | |
productMR((x) => (x + x))(1, 3) | |
sumMR((x) => (x))(1, 3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment