Functional programming is a way of writing code that focuses on using functions to transform data, avoiding changes to data and state, which makes the code easier to understand and test.
Map
The map
function transforms each element of a collection by applying a closure, returning a new array with the transformed values.
let numbers = [1, 2, 3, 4, 5]
let squares = numbers.map { number in
number * number
}
print(squares)
// The console shows: [1, 4, 9, 16, 25]
This function takes a closure as a parameter, which will be executed once per each element of the array, so the temporary variable number
will take the value of that element. The final result will be an array with all the results we returned.
This function is available for any Collection
.
let names = ["Pablo", "Andrea", "Miguel"]
let greetings = names.map { name in
"Hola \(name)"
}
print(greetings)
// The console shows: ["Hola Pablo", "Hola Andrea", "Hola Miguel"]
Transforming values
We can return an array of a different type than the one we are mapping. This type will be infered from the value type we return. Let's see how to convert an array of Int
into one of String
.
let numbers = [1, 2, 3, 4, 5]
let strings = numbers.map { number in
"This is the number \(number)"
}
print(strings)
// The console shows: ["This is the number 1", "This is the number 2", "This is the number 3", "This is the number 4", "This is the number 5"]
If our closure is too complex for the system to infer its type, we need to specify it. There are two options:
Defining the type of the result we expect:
let strings: [String] = numbers.map { number in
...
Or defining the result type we are going to return:
let strings = numbers.map { number -> String in
...
Shorthand Argument Names
In any closure we can use the symbol $
followed by the parameter number to avoid declaring a name for each parameter. Let's see how we would use this with the closure passed to the map()
function.
let numbers = [1, 2, 3, 4, 5]
let squares = numbers.map { $0 * $0 }
print(squares)
// The console shows: [1, 4, 9, 16, 25]
Filter
Filter function creates a new array containing only the elements of a collection that satisfy a given condition specified in a closure. The type of the result will be the same as the original array.
let numbers = [1, 20, 13, 24, 7, 55, 3]
let bigNumbers = numbers.filter { $0 > 10 }
print(bigNumbers)
// The console shows: [20, 13, 24, 55]
Reduce
The reduce
function combines all elements of a collection into a single value by applying a closure that accumulates a result, starting from an initial value. As an example, we can sum all the values of an array with this function.
let numbers = [1, 20, 13, 24, 7, 55, 3]
let total = numbers.reduce(0) { accumulated, number in
return accumulated + number
}
print(total) // The console shows: 123
The first parameter of this function will be the initial value, and each time our closure executes, we will have access to the accumulated
value and the corresponding item. The value we return will be the accumulated value for the next execution.
Using Shorthand Argument Names:
let total = numbers.reduce(0) { $0 + $1 }
Be the first to comment