“This is my 28th day of participating in the First Challenge 2022. For details: First Challenge 2022.”
- This paper mainly introduces the use of some high order functions in Swift, this analysis
Swift
The following higher-order functions are provided:map
,flatMap
,compactMap
,filter
,reduce
.
1. map
The Map function applies to each element in the Collection and returns a new Collection. The map function takes a closure expression as its only argument, then calls the closure expression for each element in the array and returns the value of the mapping of that element.
1.1 the use of
- Int
An array of conversion
String array
We can operate on elements directly, or we can use $0 to represent each element.
- A String is converted to an Int
You can see that you are returning an array of optional value elements, containing nil that cannot be converted.
- To generate a
2 d array
Generates a new array of ints, repeating as many elements as possible
It returns a two-dimensional array
1.2 Source Code Analysis
We are incollection.swift
View the map implementation
First construct an array with the same number as the original array, then facilitate each element in the original array, perform the corresponding operation through transform, then append to the new array, and finally return a new array.
2. flatMap
2.1 the use of
- Use flatMap
It can be seen that the flatMap flattens the array in the array, which is equivalent to converting a two-dimensional array into a one-dimensional array.
Of course, we can also map a two-dimensional array and convert it to a String.
For a one-dimensional object, a map is the same as a flatmap.
Can filter
2.2 Source Code Analysis
As you can see, like map, create an array with elements of type SegmentofResult.Element. Transform by calling the closure’s transform function transform. Then append adds the elements of each array. Compared with our map, the two main functions of flatMap are flattening and filtering null values.
Look at an example
You can see here we usemap
When you do the set operation, you getreslut
It’s an optional optional, so here we’re actually usingresult
The process of thinking about the situation is more
throughflatMap
We can get an optional value instead ofOptional Optional
Take a look at the source:
FlatMap returns an optional value when the closure is applied when an optional value is entered, which is then flattened to return an unpacked result. Essentially, flatMap does an unpack at the optional level, as opposed to Map.
FlatMap allows you to do no extra unpacking when making chain calls
3. compactMap
Use compactMap when the conversion closure returns an optional value and you expect the result to be a sequence of non-optional values.
3.1 the use of
or
It automatically filters nil
3.2 Source code Analysis
Map puts the result of transform directly into result, whereas compactMap puts the result of transform directly into result using if-let. And what if-lets do is unpack and filter nil.
4. filter
Filter is used for filtering, similar to the predicates we use on arrays in OC
4.1 the use of
Returns an array of all elements greater than 2
4.2 Source Code Analysis
Iterators are used to iterate over all elements. For each element, closure isIncluded is called to determine whether the criteria are met. And then we add it to the array that we created.
5. reduce
It is mainly used to do the corresponding operation on each element in the collection and the superposition
@inlinable public func reduce<Result>(_ initialResult: Result, _ nextPartialResult: (Result, Element) throws -> Result) rethrows -> Result
@inlinable public func reduce<Result>(into initialResult: __owned Result, _ updateAccumulatingResult: (inout Result, Element) throws -> ()) rethrows -> Result
Copy the code
The two approaches are somewhat similar, but the difference lies in the definition of closures.
-
The first closure receives Result and Element, and returns the Result after the closure is executed. The next operation takes the Result after each closure as an entry to the next Element closure, iterating through all elements.
-
The second closure, which also receives Result and Element, returns no value, and Result is decorated with inout, so the closure is passed the address of Result, so the closure is executed based on Result.
5.1 the use of
- Find the maximum value in the array
- Reverse the array
- sum
5.2 Source Code Analysis
For the first:
Accumulator records the status of each time, and then facilitates the passing of each result and element through iterator. After traversal, Accumulator is returned.
For the second:
Accumulator is an element whose address is not an accumulator. For each element, call the closure updateAccumulatingResult. The parameter is the address of the temporary variable Accumulator. The closure actually updates the value of accumulator.