O'Reilly logo

Swift Functional Programming - Second Edition by Dr. Fatih Nayebi

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Higher-order functions

As we have seen in the Defining and using function parameters and Function types sections of this chapter, functions can accept functions as parameters in Swift. Functions that can accept other functions as parameters are called higher-order functions. This concept, along with first-class functions, empowers FP and function decomposition.

As this topic is essential in FP, we will go through another simple example.

Suppose that we need to develop two functions that add and subtract two Int values as follows:

func subtractTwoValues(a: Int, b: Int) -> Int {     return a - b } func addTwoValues(a: Int, b: Int) -> Int {     return a + b } 

Also, we need to develop functions to calculate the square and triple of two Int values ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required