summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Scala/sandbox/curry.scala6
1 files changed, 6 insertions, 0 deletions
diff --git a/Scala/sandbox/curry.scala b/Scala/sandbox/curry.scala
index 1a2e44c..adc7e75 100644
--- a/Scala/sandbox/curry.scala
+++ b/Scala/sandbox/curry.scala
@@ -35,6 +35,10 @@ object Curry {
def fact(n: Int) = product(x => x)(1, n)
+ // add unit value and combining function => sum and product generalisation
+ def mapReduce(f: Int => Int, c: (Int, Int) => Int, u: Int )(a: Int, b: Int): Int =
+ if (a > b) u else c(f(a), mapReduce(f, c, u)(a + 1, b))
+
def run = {
println("Curry")
println(sum(x => x * x, 3, 5))
@@ -45,6 +49,8 @@ object Curry {
println(sumCubesShort(3, 5))
println(product(x => x * x)(3, 4))
println(fact(4))
+ println(mapReduce(x => x * x, (x, y)=> x + y, 0)(3, 5))
+ println(mapReduce(x => x * x, (x, y)=> x * y, 1)(3, 4))
}
}