当前位置:网站首页>Swift array map/flatmap/compactmap/filter/reduce/chaining Usage Summary
Swift array map/flatmap/compactmap/filter/reduce/chaining Usage Summary
2022-06-24 16:45:00 【woopDast1】
I have learned the relevant usage before , But it is easy to forget after a long time , So to sum up , Convenient to review .
stay playground The tests and notes are as follows :
import UIKit
let arr = [1,2,3]
//MARK:map( mapping ):returns an Array containing results of applying a transform to each item.
arr.map { (value) -> Int in
return value \* 2
}
arr.map { (value) -> Int in
value \* 2
}
// When closures are written on one line , perform 4 Time , The first is the closure assignment operation
arr.map { (value) in value \* 2}
let arr1 = arr.map {
$0 \* 2
}
arr.map {$0 \* 2}
let arr2 = arr.map {"\($0)"}
print(arr2)
let dic = ["point1" : 10, "point2" : 20, "point3" : 30]
dic.map { (name,distance) -> Int in// The argument is a tuple
distance \* 2
}
dic.map {$0.1 \* 2}
//MARK:filter:returns an Array containing only those items that match an include condition.
let arrF0 = arr.filter { (num) -> Bool in
num % 2 == 0
}
let arrF1 = arr.filter { $0 % 2 == 0 }
print(arrF1)
//MARK:reduce( inductive ): Initial value + Closure ("+" Operators are essentially closures ).returns a single value calculated by calling a combine closure for each item with an initial value.
let sum = arr.reduce(10) {Result,num -> Int in
Result + num
}
let sum1 = arr.reduce(10,+)
let dicR0 = dic.reduce("res:") { (Result, arg1) -> String in
let (key, value) = arg1
return Result + key + "\(value)" + ","
}
let dicR1 = dic.reduce("res:") { (Result, arg1) -> String in
let (key, value) = arg1
return key + "\(value)" + "!"
}
let arrStr = [" you ", " good ", " ah "]
let dicRStr0 = arrStr.reduce(" inductive :"){res, str in res + str}
let dicRStr1 = arrStr.reduce(" inductive :", +)
//MARK:flatMap( Flatten ): The dimension of two-dimensional array is reduced to one dimension ; Optional values are converted to optional values
let arrWithArr = [[1, 2], [3, 4, 5], [6, 7]]
let arrFm = arrWithArr.flatMap{$0}
print(arrFm)
let num: Int? = Int(8);
let numFM = num.flatMap{$0 < 5 ? $0 : nil}
//MARK:compactMap( compact ): Go to nil+ Become non optional
let arrWithNil: [String?] = ["1", nil, "2", nil]
//'flatMap' is deprecated: Please use compactMap(\_:) for the case where closure returns an optional value
//arrWithNil.flatMap{$0}
let arrCm = arrWithNil.compactMap{$0}
print(arrCm)
//MARK:chaining( chain syntax )
let resCh = arrWithArr.flatMap{$0}.filter{$0 > 5}.reduce(10, +)
print(resCh)边栏推荐
- [golang] Introduction to golang (I) establishment of running environment
- Recent progress of ffmpeg go
- Dismantle the industrial chain of synthetic rubber industry, and the supply chain may become a sharp weapon for breakthrough
- Bitwise Operators
- A memory leak caused by timeout scheduling of context and goroutine implementation
- Principle analysis of robot hardware in the loop system
- How does easydss, an online classroom / online medical live on demand platform, separate audio and video data?
- [go] runtime package for concurrent programming and its common methods
- How to access tke cluster API interface with certificate or token
- Talk about some good ways to participate in the project
猜你喜欢

Cognition and difference of service number, subscription number, applet and enterprise number (enterprise wechat)

Ps\ai and other design software pondering notes

Ui- first lesson

C. K-th not divisible by n (Mathematics + thinking) codeforces round 640 (Div. 4)
![[leetcode108] convert an ordered array into a binary search tree (medium order traversal)](/img/e1/0fac59a531040d74fd7531e2840eb5.jpg)
[leetcode108] convert an ordered array into a binary search tree (medium order traversal)

A survey on model compression for natural language processing (NLP model compression overview)

Problems encountered in the work of product manager

Applet wxss

Some adventurer hybrid versions with potential safety hazards will be recalled

A survey on dynamic neural networks for natural language processing, University of California
随机推荐
[tke] whether to configure SNAT when the container accesses services outside the node
@There is a free copyright protection service for enterprises in Dawan District
Batch BOM Bapi test
C. K-th not divisible by n (Mathematics + thinking) codeforces round 640 (Div. 4)
How to use the national standard streaming media server to view the video stream of the surveillance camera? How to correctly use UDP and TCP protocols?
Funny! Pictures and texts give you a comprehensive understanding of the effects of dynamics and mass
Comparison of jmeter/k6/locust pressure measuring tools (not completed yet)
Coding's first closed door meeting on financial technology exchange was successfully held
Virtual machine virtual disk recovery case tutorial
What is zero trust? Three classes will show you how to understand him!
TRTC web end imitation Tencent conference microphone mute detection
Experience and suggestions on cloud development database
MD5 verification based on stm32
Edit distance (linear dp+ violence matching)
Tencent on the other hand, I was puzzled by the "horse race" problem
Join in ABAP CDs
[tke] modify the cluster corendns service address
Data acquisition and transmission instrument reservoir dam safety monitoring
Introduction to koa (IV) koa operation database
How to access tke cluster API interface with certificate or token