Since its first release, Swift has been a multiparadigm programming language, and one of its supported programming paradigms is functional programming. Functional programming favors immutable data and, therefore, avoids state changes. The code written with a functional programming style is as declarative as possible, and it is focused on what it does instead of how it must do it.
As it happens in many modern programming languages, functions are first-class citizens in Swift 3. You can use functions as arguments for other functions or methods. We can easily understand this concept with a simple example: array filtering. However, take into account that we will start by writing imperative code with functions as first-class citizens, and then, we will create a new version for this code that uses a functional approach in Swift through a filter operation.
The following lines declare the applyFunctionTo
function that receives an array of Int
, numbers
, and a function type: condition
. The function type specifies the parameter types and the return types for the function. In this case, condition
specifies a function type that receives Int
and returns a Bool
value. The function executes the received function, condition
, for each element in the input array and adds the element to an output array whenever the result of the called function is true. This way, only the elements that meet the specified condition will appear in the resulting array of Int
. The code file for the sample is included in the swift_3_oop_chapter_07_04
folder:
public func applyFunctionTo(numbers: [Int], condition: (Int) -> Bool) -> [Int] { var returnNumbers = [Int]() for number in numbers { if condition(number) { returnNumbers.append(number) } } return returnNumbers }
The following line declares a divisibleBy5
function that receives Int
and returns Bool
, indicating whether the received number is divisible by 5
or not. The code file for the sample is included in the swift_3_oop_chapter_07_04
folder:
func divisibleBy5(number: Int) -> Bool { return number % 5 == 0 }
The function type for the divisibleBy5
function is equal to the function type specified in the condition
argument for the applyFunctionToNumbers
function. The following lines show the function type specified in the condition
argument followed by the divisibleBy5
function declaration. The function type specified in the condition
argument matches the function type for the divisibleBy5
function:
condition: (Int) -> Bool func divisibleBy5(number: Int) -> Bool
The following two lines declare an array of Int
initialized with ten numbers and call the applyFunctionTo
function with the array of Int
as the numbers
argument and the divisibleBy5
function as the condition
argument. The divisibleBy5Numbers
array of Int
will have the following values after the applyFunctionTo
function runs: [10, 20, 30, 40, 50, 60]
. The code file for the sample is included in the swift_3_oop_chapter_07_04
folder:
var numbers = [10, 20, 30, 40, 50, 60, 63, 73, 43, 89] var divisibleBy5Numbers = applyFunctionTo(numbers: numbers, condition: divisibleBy5) print(divisibleBy5Numbers)
The following screenshot shows the results of executing the previous lines in the Playground:
3.128.173.53