Skip to content

Instantly share code, notes, and snippets.

@anujku
Created May 31, 2016 01:05
Show Gist options
  • Save anujku/296a541cbffa12e93d1d8df17459693f to your computer and use it in GitHub Desktop.
Save anujku/296a541cbffa12e93d1d8df17459693f to your computer and use it in GitHub Desktop.
Scala Currying Examples
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