diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2013-04-03 22:31:44 +0200 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2016-11-10 18:03:22 +0100 |
commit | 8cdb4a8d95b79f78f2e4f672a452db5e48eed030 (patch) | |
tree | f38bc5175b04d56849efadf0678142519fc19e99 | |
parent | 95d1672c121f15c64b616124f778554ab9a85b77 (diff) | |
download | coursera-8cdb4a8d95b79f78f2e4f672a452db5e48eed030.zip coursera-8cdb4a8d95b79f78f2e4f672a452db5e48eed030.tar.gz |
Scala : add curry syntax shortcut
-rw-r--r-- | Scala/sandbox/curry.scala | 11 |
1 files changed, 10 insertions, 1 deletions
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)) } } |