Skip to content

Latest commit

 

History

History
74 lines (49 loc) · 2.58 KB

higherOrderFunctions.md

File metadata and controls

74 lines (49 loc) · 2.58 KB

previous page: enums

Higher Order Functions

Use these collection instances in reference to the following code snippets.
let numArr = [1, 3, 22, 9, 7]
let nameToScore = ["rin": 9, "sar": 10, "cenz": 9, "ruh": 10, "mike": 7]

I wrote an article on higher order functions that can be found here.

Map

Returns the collection containing the results of mapping the input closure over the collection's elements.

Does not modify the collection - rather returns a new collection.

let fifteenPercentOfNums = numArr.map { Float($0) * 0.15 }
// fifteenPercentOfNums = [0.15, 0.6, 1.35, 3.3000002]

let playersNames = nameToScore.map { $0.key.capitalized }
// playersNames = ["Cenz", "Rin", "Sar", "Ruh", "Mike"]

Filter

Returns the collection containing the existing elements type from the results that match the input closures condition from the collection's elements.

The result in the closure must contain a true/false value.

Does not modify the collection - rather returns a new collection.

let items = numArr.filter { $0 > 20 }
// items = [22]

let topPlayers = itemsDict.filter { $0.value >= 8 }.keys
// topPlayers = ["ruh", "sar", "rin", "cenz"]
let topPlayersTwo = itemsDict.filter { key, value in
    return value >= 8
}.keys
// topPlayersTwo = ["sar", "ruh", "cenz", "rin"]

Reduce

Returns a single value that is the result of combining all elements using the input closure.

Does not modify the collection - rather returns a new collection.

There's two parts to this.

  • initialResult: The value to use as the initial accumulative value.
  • nextPartialResult: The closure that combines the initial accumulative value and an element in the collection.
let totalNum = numArr.reduce(0, { $0 + $1 })
// total = 36

let totalScore = nameToScore.reduce(0, { $0 + $1.value })
// totalScore = 45

Discussion
0 is the initialResult and $0 is the nextPartialResult whereas $1 is the element.
The value $1 here is a named tuple: (key: String, value: Int) where you can access it through .0 and .1 rather than .key and .value respectively as well.

Read more about shorthand argument names here.

ForEach, contains, sorted, removeAll

next page: closures