swift

Map is a method of the type array: one of the many functions that the array structure provides to manipulate its elements. I find this to be a very useful feature that in some cases really makes work much simpler. We start from the typical case of an array of numbers on which we loop on it to perform an operation such as power elevation.

var numbers = [1, 23, 14, 76, 234]

var newNumbers = [Int]()

for i in numbers {

    let tmp = i * i

    newNumbers.append(tmp)

} 

We have initialized the array with random numbers and initialized the target array where the values will be stored after the calculation. We run the for loop and add the computed values to the newNumbers array in append. All this is very simple, incredibly it can be more.

Let's introduce the map method with the help of Xcode, type the name of the array (numbers), type the point and select the map method, then double press enter key, this is what appears.

numbers.map { (<#Int#>) -> T in

    <#code#>

} 

 

Xcode suggests to insert a closure as a map parameter. Recalling what is stated in paragraph closures complete the code as below. Replace the first placeholder with the name and type of variable passed. By the letter 'T', the compiler indicates a generic container, since the operation we perform on each element in the array could return any type (eg I get Int and return String); our procedure returns an integer and is what we write instead of 'T'. Finally, the code to apply to each element, in our case the power elevation.

let newArray = numbers.map { (i: Int) -> Int in

    return i * i

}

 

Map reiterates all the elements of the array by taking their Int value by doing the indicated operation and returning another Int value that is stored in the newArray. By always referring to the paragraph closure, the expression above can be further simplified by reaching its minimum form.

let anotherNewArray = numbers.map { $0 * $0 }

Reduce is a property of the array type as a map. Shares map with the fact that the compiler behind the scenes reiterates all the elements of the array to perform on each of them an operation. The purpose of reduce is to reduce array elements to a single element; the typical use is to get the sum or product of all the values of an array.

let total = numbers.reduce(0, { partial, next in

    return partial + next

}) 

 

In this example, the sum of all the values of the array numbers defined above is summed. The first '0' value specified in reduce statement is to be considered a sort of offset, a value that is added to the final result, or a starting value to which all subsequent valued are summed.

Partial indicates the partial result as the procedure reiterates on the array elements by performing the operation indicated in the body of the function. Every cycle the partial value is updated with the current value, that is, the partial sum. Next is the successive value of the array. The procedure above for each element of the array performs the sum between the partial and the next and places it in partial.

By utilizing what you learned with closures you can simplify the above code by writing:

let total2 = numbers.reduce(0, { $0 + $1 })

 

Filter is another array property that by reiterating on all elements provides a true or false value based on a specified filter. Filter also makes use of closures.

let evenNumbers = numbers.filter { (value: Int) -> Bool in

    return value % 2 == 0
}

Value is the element of the array on which the function reiterates and is used to understand each cycle whether it is even or not. The displayed code returns the only even values as the array numbers. Recalling the capability of synthesis of closures, the above expression can be represented:

let evenNumbers2 = numbers.filter { $0 % 2 == 0 }