diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2013-04-03 22:59:04 +0200 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2016-11-10 18:03:22 +0100 |
commit | 2e5d10d56a29ea571dcde14a8114e51f0a91a2cb (patch) | |
tree | 3abc5a9568911fb940cc3b634ff1c2d818ad7992 /Scala | |
parent | 72219111494683f655b1faea242970a5636cd674 (diff) | |
download | coursera-2e5d10d56a29ea571dcde14a8114e51f0a91a2cb.zip coursera-2e5d10d56a29ea571dcde14a8114e51f0a91a2cb.tar.gz |
Scala : curry define mapReduce
Diffstat (limited to 'Scala')
-rw-r--r-- | Scala/sandbox/curry.scala | 6 |
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)) } } |