转换数组

最近项目中,非持久化部分使用了数组来暂存数据,涉及到去重。一直以来都是 for in 循序遍历来处理数组,比较了函数式编程,还是后者的模块化组装性比较有优势。

《Advanced Swift》中 Transforming Arrays 一节讲解了一些数组的基本映射转换方法。其中主要是重新实现了部分方法,以及推荐我们自己写一些拓展标准库的方法。

以下是标准库中13个独立的方法,可以随意组合使用:

  • map and flatMap — how to transform an element 映射:如何转换每一个元素
  • filter — should an element be included? 过滤:是否应该包含该元素
  • reduce — how to fold an element into an aggregate value 归纳:如何折叠元素到一个总值
  • sort and lexicographicCompare — in what order should two elements come? 排序和字典顺序比较:两个元素如何排序
  • indexOf and contains — does this element match? 索引/下标 和 包含:是否与该元素匹配
  • minElement and maxElement — which is the min/max of two elements? 最小元素和最大元素:两个元素中最小/最大的那个
  • elementsEqual and startsWith — are two elements equivalent? 元素等于和开始于:两个元素是否是对等
  • split — is this element a separator? 分割:该元素是否是分隔符

编程毕竟是操作性的语言,以上都有表意很明确的方法,只要在实际调用几次也就理解了。下面是我写的 Demo :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// : Playground - noun: a place where people can play

import UIKit

var fibs = [2, 3, 45, 53, 32, 12, 32, 1, 0, 1]

fibs.map {
Double($0 * 3)
}

let suits = ["♠", "♥", "♣", "♦"]

let ranks = ["J", "Q", "K", "A"]

let allCombinations = suits.flatMap { suit in
ranks.map { rank in
(suit, rank)
}
}

suits.flatMap { (suit) -> [String]? in
[suit, suit]
}

suits.flatMap { (suit) -> [String] in
if suit != "♥" {
return [suit]
} else {
return []
}
}

fibs.flatMap { (num) in
print(num)
}

fibs.sort {
$0 > $1
}

fibs.lexicographicalCompare([3])
fibs.lexicographicalCompare([1])
fibs.lexicographicalCompare([3]) { (num0, num1) -> Bool in
num0 > num1
}
fibs.lexicographicalCompare([1]) { (num0, num1) -> Bool in
num0 > num1
}

fibs.reduce(0) { (total, num) -> Int in
total + num
}

fibs.filter { (num) -> Bool in
num % 3 == 0
}

fibs.indexOf(4)
fibs.indexOf(1)
fibs.contains(4)
fibs.contains(1)

fibs.minElement()
fibs.minElement { (num0, num1) -> Bool in
num0 < num1
}
fibs.minElement { (num0, num1) -> Bool in
num0 > num1
}
fibs.maxElement()
fibs.maxElement { (num0, num1) -> Bool in
print("num0: \(num0), num1: \(num1)")
return num0 < num1
}

fibs.maxElement { (num0, num1) -> Bool in
num0 > num1
}

var strs = ["Lee", "Bee", "Will", "10", ""]
strs.maxElement { (str0, str1) -> Bool in
str0 < str1
}
strs.maxElement { (str0, str1) -> Bool in
str0 > str1
}

strs.elementsEqual(["Lee", "Bee", "Will", "10"])
strs.elementsEqual(["Lee", "Bee", "Will", "10", ""])

strs.startsWith(["Lee"])
strs.startsWith(["10"])
strs.startsWith(["Lee"]) { (str0, str1) -> Bool in
str0 == str1
}

strs.split("10")
strs.split("3")
fibs.split(1)

fibs.split(1, maxSplit: 2, allowEmptySlices: true)
fibs.split(1, maxSplit: 2, allowEmptySlices: false)
fibs.split(1, maxSplit: 3, allowEmptySlices: true)
fibs.split(1, maxSplit: 6, allowEmptySlices: false)
fibs.split(3333, maxSplit: 2, allowEmptySlices: true)
fibs.split(44444, maxSplit: 2, allowEmptySlices: false)

fibs.split(32, maxSplit: 0, allowEmptySlices: true)
fibs.split(32, maxSplit: 1, allowEmptySlices: false)
fibs.split(32, maxSplit: 2, allowEmptySlices: true)
fibs.split(32, maxSplit: 3, allowEmptySlices: false)

fibs.split { (num) -> Bool in
num % 2 == 0
}

fibs.split(1, allowEmptySlices: true) { (num) -> Bool in
num % 2 == 1
}

fibs.split(66, allowEmptySlices: true) { (num) -> Bool in
num % 2 == 1
}

fibs.forEach { (num) in
print(num - 44)
}

Demo 下载地址:https://github.com/gewill/test-projects/tree/master/collections%20transform.playground