1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
object Curry {
// high order function
def sum(f: Int => Int, a: Int, b: Int) = {
def loop(a: Int, acc: Int): Int =
if (a > b) acc
else loop(a + 1, f(a) + acc)
loop(a, 0)
}
// curry version: this function return an (Int, Int) => Int function
def sumCurry(f: Int => Int): (Int, Int) => Int = {
def sumF(a: Int, b: Int): Int =
if (a > b) 0
else f(a) + sumF(a + 1, b)
sumF
}
def cube (x: Int): Int = x * x * x
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(sumInts(3, 5))
println(sumCurry(cube)(3, 5))
println(sumCubes(3, 5))
println(sumCurryShort(cube)(3, 5))
println(sumCubesShort(3, 5))
}
}
|