Posts

Showing posts from April, 2020

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