Highorderfunctions

Preview:

Citation preview

HIGH ORDER

FUNCTIONS

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.

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

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

• val resultExists4 = list.exists(isEqualToFour)

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

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

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

Thanks