6

Click here to load reader

Highorderfunctions

Embed Size (px)

Citation preview

Page 1: Highorderfunctions

HIGH ORDER

FUNCTIONS

Page 2: Highorderfunctions

Whats are high order functions?

• One or more of its parameters is a function, and it returns

some value.

• It returns a function, but none of its parameters is a

function.

• One or more of its parameters is a function, and it returns

a function.

Page 3: Highorderfunctions

• def isEqualToFour(a:Int) = a == 4

• val list = List(1, 2, 3, 4)

• val resultExists4 = list.exists(isEqualToFour)

Page 4: Highorderfunctions

def sumInts(a: Int, b: Int): Int =

if (a > b) 0 else a + sumInts(a + 1, b)

Page 5: Highorderfunctions

Advantages

• Higher-order functions reduce program size and

accelerate development speed, particularly for large

programs.

• Expressiveness of a visual syntax

• Zero run-time cost in most cases

Page 6: Highorderfunctions

Thanks