From 8cdb4a8d95b79f78f2e4f672a452db5e48eed030 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Wed, 3 Apr 2013 22:31:44 +0200 Subject: Scala : add curry syntax shortcut --- Scala/sandbox/curry.scala | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Scala/sandbox/curry.scala b/Scala/sandbox/curry.scala index 84b00ee..5c5577a 100644 --- a/Scala/sandbox/curry.scala +++ b/Scala/sandbox/curry.scala @@ -22,12 +22,21 @@ object Curry { def sumInts = sumCurry(x => x) def sumCubes = sumCurry(x => x * x * x) + // syntax shortcut + def sumCurryShort(f: Int => Int)(a: Int, b: Int): Int = + if (a > b) 0 else f(a) + sumCurryShort(f)(a + 1, b) + + // add _ if you want to treat it as a partially applied function + def sumCubesShort = sumCurryShort(cube)_ + def run = { println("Curry") println(sum(x => x * x, 3, 5)) - println(sumCurry(cube)(3, 5)) println(sumInts(3, 5)) + println(sumCurry(cube)(3, 5)) println(sumCubes(3, 5)) + println(sumCurryShort(cube)(3, 5)) + println(sumCubesShort(3, 5)) } } -- cgit v1.1-2-g2b99