From 2e5d10d56a29ea571dcde14a8114e51f0a91a2cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Wed, 3 Apr 2013 22:59:04 +0200 Subject: Scala : curry define mapReduce --- Scala/sandbox/curry.scala | 6 ++++++ 1 file changed, 6 insertions(+) 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)) } } -- cgit v1.1-2-g2b99