Swift Map , Filter , Reduce

Swift Map , Filter , Reduce

Higher order function as defined in computer science , does at-least one of the following:

1. returns a function as a result
2. takes one or more functions as arguments.

Objective-C does not have direct support for higher order functions, but Swift has, which is good.

There are several examples of higher order functions, some of them are map, filter , reduce .

Let's check them out with Swift.

Map

Definition:

"Returns an array containing the results of mapping the given closure over the sequence’s elements."

So we know now , map always returns an Array.

Example:
Apply an offset of 10 to userIndexes.


let userIndexes = [11,12,13,14]

let offsetUserIndexes = userIndexes.map { (userIndex) -> Int in
    return 10 + userIndex
}


let offsetUserIndexes = userIndexes.map { (userIndex) -> Int in
    return 10 + userIndex
}


Output:

[21, 22, 23, 24]

Here, we pass a function to get the result, the function adds an offset to the userIndex.  Perhaps we now know why they are called higher order functions, it helps achieve (requirement no. 2) as mentioned at the top of this page.

There are many other ways to get the same output , basically trying to shorthand the statement.

Option 1:

Remove -> , which separates the method parameter from the function return type. Also, we don't need the return type as it's inferred from return statement.

userIndexes.map { (userIndex)  in
    return 10 + userIndex
}


Option 2 :

Remove the return from the above statement, as the Swift closures understands the return type from the operation.

userIndexes.map { (userIndex) in
    10 + userIndex
}



Option 3 :

Use of shorthand arguments. Closure with one parameter , can be replaced with $0 . It would mean we are accessing userIndex. In which case in can be eliminated too.

Remove (userIndex) and userIndex from Option 2. Also remove
"in" . Replace the userIndex with $0, which means the first parameter in the closure.

userIndexes.map { 10 + $0 }

Map on Dictionary.

Map can also be applied to dictionary. In any case output will be still be an Array. Array of String ,  Double or it can be Any Object.

Example:
From the grocery list , get me the items which were purchased.


let groceries = ["Orange" : 4.0 , "Milk" : 3.0 , "Bread" : 2.0 ]


let groceryItemList = groceries.map { (key: String, value: Double) -> String in
    return key
}

Output:

["Milk", "Bread", "Orange"]




Filter 


"Returns an array containing, in order, the elements of the sequence that satisfy the given predicate."

Filter also returns an Array like Map, but the only difference here is ,it returns the item in the collection which meets the requirement.

There is no transformation applied to collection unlike Map

Example:

Give me the userIndexes which are greater than or equal to 13.

let userIndexes = [11,12,13,14]

let userIndexesGreaterThan13 = userIndexes.filter { (index) -> Bool in
    return index >= 13
}


Output:
[13, 14]


Comments

Popular posts from this blog

hitTest on iOS to identify the view being hit

CMTimeMakeWithSeconds explained

How to set Custom Section Header in UITableView for ios sdk version greater than 6