-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mapping.swift
175 lines (162 loc) · 7.11 KB
/
Mapping.swift
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//
// Mapping.swift
// MakingWatchTheBirdie
//
// Created by Andrey Zarembo-Godzyatskiy on 15.09.2023.
//
import Foundation
import Combine
import SwiftUI
extension SwiftCombineDemo {
static var mapping: OperationInfoCollection = OperationInfoCollection(
title: "Mapping",
operations: [
OperationInfo(
title: "Map",
examples: [
OperationInfoExample(
inputs: [
Input(title: "a", items: [
.item(color: .red, icon: .star),
.item(color: .blue, icon: .star),
.delay,
.item(color: .green, icon: .star)
])
],
equation: "a.map( ★ → ▲ )",
output: .publisher { inputs in
inputs["a"]!.map { $0!.mapOperation() }
}
)
]),
OperationInfo(
title: "MapError",
examples: [
OperationInfoExample(
inputs: [
Input(title: "a", items: [
.item(color: .red, icon: .star),
.error(color: .red),
.delay,
.item(color: .green, icon: .star)
])
],
equation: "a.mapError( red → green )",
output: .publisher { inputs in
inputs["a"]!
.mapError { error in
guard case let .error(color) = error, color == .red else { return error }
return DemoError.error(color: .green)
}
}
)
]),
OperationInfo(
title: "ReplaceNil",
examples: [
OperationInfoExample(
inputs: [
Input(title: "a", items: [
.item(color: .red, icon: .star),
.nil,
.item(color: .blue, icon: .star),
.delay,
.item(color: .green, icon: .star)
])
],
equation: "a.replaceNil(with: ★ )",
output: .publisher { inputs in
inputs["a"]!.replaceNil(with: Item.item(color: .orange, icon: .star))
.map { (r) -> Item? in r }
}
)
]),
OperationInfo(
title: "Scan",
examples: [
OperationInfoExample(
inputs: [
Input(title: "a", items: [
.item(color: .red, icon: .number(value: 1)),
.item(color: .blue, icon: .number(value: 2)),
.item(color: .purple, icon: .star),
.item(color: .green, icon: .number(value: 3)),
.delay,
.item(color: .orange, icon: .number(value: 4)),
])
],
equation: "a.scan(.yellow) { repaint } ",
output: .publisher { inputs in
inputs["a"]!.scan(Item.item(color: .teal, icon: .number(value: 0))) { res, item in
guard case let .item(_, icon) = item, case let .item(res_color, _) = res else {
return res
}
return Item.item(color: res_color, icon: icon)
}
}
)
]
),
OperationInfo(
title: "SetFailureType",
examples: [
OperationInfoExample(
inputs: [
Input(title: "a", items: [
.error(color: .green)
])
],
equation: "a.catch { ... }.setFailureType(DemoError.self)",
output: .publisher { inputs in
inputs["a"]!.catch { _ in Just(Item.error(color: .red)) }
.setFailureType(to: DemoError.self)
}
)
]
),
OperationInfo(
title: "Map Keypath",
examples: [
OperationInfoExample(
inputs: [
Input(title: "a", items: [
.item(color: .red, icon: .number(value: 1)),
.item(color: .blue, icon: .number(value: 2)),
.item(color: .purple, icon: .star),
.item(color: .green, icon: .number(value: 3)),
])
],
equation: "a.map(\\.color).map { .item($0...) }",
output: .publisher { inputs in
inputs["a"]!.map(\.?.color)
.compactMap { color in
guard let _color = color else { return nil }
return Item.item(color: _color, icon: .none)
}
}
),
OperationInfoExample(
inputs: [
Input(title: "b", items: [
.item(color: .red, icon: .number(value: 1)),
.item(color: .blue, icon: .number(value: 2)),
.item(color: .purple, icon: .star),
.item(color: .green, icon: .number(value: 3)),
])
],
equation: "b.map(\\.color, \\.icon).map { .item($0.0, $0.1) }",
output: .publisher { inputs in
inputs["b"]!.map(\.?.color, \.?.icon)
.compactMap { color, icon in
guard let _color = color, let _icon = icon else { return nil }
return Item.item(color: _color, icon: _icon)
}
}
)
]
)
])
}
#Preview {
CombineDemo(collections: [SwiftCombineDemo.mapping])
}