Swift map filter reduce 使用指南

Posted mnstar

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Swift map filter reduce 使用指南相关的知识,希望对你有一定的参考价值。

转载:https://useyourloaf.com/blog/swift-guide-to-map-filter-reduce/

Using mapfilter or reduce to operate on Swift collection types such as Array or Dictionary is something that can take getting used to. Unless you have experience with functional languages your instinct may be to reach for the more familiar for-in loop. With that in mind here is my guide to using map, filter, reduce (and flatMap).

Map

Use map to loop over a collection and apply the same operation to each element in the collection. The map function returns an array containing the results of applying a mapping or transform function to each item:

技术分享图片

We could use a for-in loop to compute the squares of each item in an array:

let values = [2.0,4.0,5.0,7.0]
var squares: [Double] = []
for value in values {
  squares.append(value*value)
}

This works but the boilerplate code to declare the type of the squares array and then loop over it is a little verbose. We also need to make the squaresarray a var as we are changing it in the loop. Now compare to when we use map:

let values = [2.0,4.0,5.0,7.0]
let squares = values.map {$0 * $0}
// [4.0, 16.0, 25.0, 49.0]

This is a big improvement. We don’t need the for loop as map takes care of that for us. Also the squares result is now a let or non-mutating value and we did not even need to declare its type as Swift can infer it.

The shorthand closure syntax can make this hard to follow at first. The map function has a single argument which is a closure (a function) that it calls as it loops over the collection. This closure takes the element from the collection as an argument and returns a result. The map function returns these results in an array.

Writing the mapping function in long form can make it easier to see what is happening:

let values = [2.0,4.0,5.0,7.0]
let squares2 = values.map({
  (value: Double) -> Double in
  return value * value
})

The closure has a single argument: (value: Double) and returns a Doublebut Swift can infer this. Also since map has a single argument which is a closure we do not need the ( and ) and with a single line closure we can even omit the return:

let squares2 = values.map {value in value * value}

The in keyword separates the argument from the body of the closure. If you prefer you can go one step further and use the numbered arguments shorthand:

let squares = values.map { $0 * $0 }

The type of the results is not limited to the type of the elements in the original array. Here is an example of mapping an array of integers to strings:

let scores = [0,28,124]
let words = scores.map { NSNumberFormatter.localizedStringFromNumber($0, 
    numberStyle: .SpellOutStyle) }
// ["zero", "twenty-eight", "one hundred twenty-four"]

The map operation is not limited to Arrays you can use it anywhere you have a collection type. For example, use it with a Dictionary or a Set, the result will always be an Array. Here is an example with a Dictionary:

let milesToPoint = ["point1":120.0,"point2":50.0,"point3":70.0]
let kmToPoint = milesToPoint.map { name,miles in miles * 1.6093 }

Quick tip: If you have trouble understanding the argument types of the closure Xcode code completion will help you:

技术分享图片

In this case we are mapping a Dictionary so as we iterate over the collection our closure has arguments that are a String and a Double from the types of the key and value that make up each element of the dictionary.

A final example with a Set:

let lengthInMeters: Set = [4.0,6.2,8.9]
let lengthInFeet = lengthInMeters.map {meters in meters * 3.2808}

In this case we have a set containing elements of type Double so our closure also expects a Double.

Filter

Use filter to loop over a collection and return an Array containing only those elements that match an include condition.

技术分享图片

The filter method has a single argument that specifies the include condition. This is a closure that takes as an argument the element from the collection and must return a Bool indicating if the item should be included in the result.

An example that filters as array of integers returning only the even values:

let digits = [1,4,10,15]
let even = digits.filter { $0 % 2 == 0 }
// [4, 10]

Reduce

Use reduce to combine all items in a collection to create a single new value.

技术分享图片

The reduce method takes two values, an initial value and a combine closure. For example, to add the values of an array to an initial value of 10.0:

let items = [2.0,4.0,5.0,7.0]
let total = items.reduce(10.0,combine: +)
// 28.0

This will also work with strings using the + operator to concatenate:

let codes = ["abc","def","ghi"]
let text = codes.reduce("", combine: +)
// "abcdefghi"

The combine argument is a closure so you can also write reduce using the trailing closure syntax:

let names = ["alan","brian","charlie"]
let csv = names.reduce("===") {text, name in "\(text),\(name)"}
// "===,alan,brian,charlie"

FlatMap

The simplest use is as the name suggests to flatten a collection of collections.

let collections = [[5,2,7],[4,8],[9,1,3]]
let flat = collections.flatMap { $0 }
// [5, 2, 7, 4, 8, 9, 1, 3]

Even more usefully it knows about optionals and will remove them from a collection.

let people: [String?] = ["Tom",nil,"Peter",nil,"Harry"]
let valid = people.flatMap {$0}
// ["Tom", "Peter", "Harry"]

The real power of flatMap comes when you use it to produce an Arraywhich is the flattened concatenation of transforming each of the subarrays.

For example to return an array of even integers contained in a collection of integer arrays by applying a filter to each item in the subarrays:

let collections = [[5,2,7],[4,8],[9,1,3]]
let onlyEven = collections.flatMap {
  intArray in intArray.filter { $0 % 2 == 0 }
}
// [2, 4, 8]

Note that flatMap is iterating over the subarrays of integers so its argument is a closure whose argument intArray is of type [Int]. This is also a situation where I find the shorthand closure syntax hard to read but you could write this:

let onlyEven = collections.flatMap { $0.filter { $0 % 2 == 0 } }

Another example to produce a flat Array that contains the squares of each Int by applying a map to each subarray and then concatenating the result:

let allSquared = collections.flatMap { $0.map { $0 * $0 } }

or in longer form:

let allSquared = collections.flatMap {
  intArray in intArray.map { $0 * $0 }
}
// [25, 4, 49, 16, 64, 81, 1, 9]

A final example that returns the individual sums of each of the arrays of integers by applying reduce to each of the subarrays:

let sums = collections.flatMap { $0.reduce(0, combine: +) }

Note though as someone helpfully pointed out to me this last example can be achieved with a plain map as reduce is returning an integer not an array:

let sums = collections.map { $0.reduce(0, combine: +) }

Chaining

You can chain methods. For example to sum only those numbers greater than or equal to seven we can first filter and then reduce:

let marks = [4,5,8,2,9,7]
let totalPass = marks.filter{$0 >= 7}.reduce(0,combine: +)
// 24

Another example that returns only the even squares by first mapping and then filtering:

let numbers = [20,17,35,4,12]
let evenSquares = numbers.map{$0 * $0}.filter{$0 % 2 == 0}
// [400, 16, 144]

Quick Summary

Next time you find yourself looping over a collection check if you could use map, filter or reduce:

  • map returns an Array containing results of applying a transform to each item.
  • filter returns an Array containing only those items that match an include condition.
  • reduce returns a single value calculated by calling a combine closure for each item with an initial value.

以上是关于Swift map filter reduce 使用指南的主要内容,如果未能解决你的问题,请参考以下文章

Swift中 Map,Flatmap,Filter,Reduce的用法

Python函数式编程 map reduce filter

lambda&filter&map&reduce函数的基本使用

Python高阶函数_map/reduce/filter函数

Python内置函数filter, map, reduce

Python 之内置函数:filter、map、reduce、zip、enumerate