-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHigherOrderFunSample.sc
More file actions
27 lines (18 loc) · 1.07 KB
/
Copy pathHigherOrderFunSample.sc
File metadata and controls
27 lines (18 loc) · 1.07 KB
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
package HigherOrderFun
object HigherOrderFunSample {
println("Welcome to the Scala worksheet") //> Welcome to the Scala worksheet
def add = (a:Int,b:Int) => a+b //> add: => (Int, Int) => Int
add(5,10) //> res0: Int = 15
def cube(x:Int): Int = x * x * x //> cube: (x: Int)Int
def mul = (a:Int,b:Int) => a*b //> mul: => (Int, Int) => Int
def sum(f:Int=>Int,a:Int,b:Int):Int =
{
if (a>b) 0 else f(a)+sum(f,a+1,b)
} //> sum: (f: Int => Int, a: Int, b: Int)Int
def sumcube(a:Int,b:Int) : Int = sum(cube,a,b) //> sumcube: (a: Int, b: Int)Int
sumcube(2,5) //> res1: Int = 224
def sumSquare(a:Int,b:Int) : Int = sum(x=>x*x,a,b)
//> sumSquare: (a: Int, b: Int)Int
def addition(a:Int) = a + 5 //> addition: (a: Int)Int
addition(10) //> res2: Int = 15
}