-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFunctionalKeyPath.swift
214 lines (198 loc) · 5.91 KB
/
FunctionalKeyPath.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import FunctionalModification
/// A path that supports embedding a value in a root and attempting to extract a root's embedded
/// value.
///
/// This type defines key path-like semantics for enum cases.
public struct FunctionalKeyPath<Root, Value> {
@usableFromInline
internal let _embed: (Value, Root) -> Root
@usableFromInline
internal let _extract: (Root) -> Value
/// Creates a functional keyPath with a pair of functions.
///
/// - Parameters:
/// - embed: A function that always succeeds in embedding a value in a root.
/// - extract: A function that can optionally fail in extracting a value from a root.
public init(
embed: @escaping (Value, Root) -> Root,
extract: @escaping (Root) -> Value
) {
self._embed = embed
self._extract = extract
}
/// Creates a functional keyPath with a writableKeyPath
@inlinable
public init(_ keyPath: WritableKeyPath<Root, Value>) {
self.init(
embed: { value, root in
var root = root
root[keyPath: keyPath] = value
return root
},
extract: { root in
root[keyPath: keyPath]
}
)
}
/// Creates a functional keyPath with a writableKeyPath
@inlinable
public static func optional(
_ keyPath: WritableKeyPath<Root, Value>
) -> FunctionalKeyPath<Root?, Value?> {
FunctionalKeyPath(keyPath).optional()
}
/// Creates a functional keyPath with a keyPath
///
/// Ignores embed function call
@inlinable
public static func getonly(_ keyPath: KeyPath<Root, Value>) -> FunctionalKeyPath {
FunctionalKeyPath(
embed: { _, root in
root
},
extract: { root in
root[keyPath: keyPath]
}
)
}
/// Makes path optional
@inlinable
public func optional() -> FunctionalKeyPath<Root?, Value?> {
FunctionalKeyPath<Root?, Value?>(
embed: { value, root in
guard let root = root, let value = value else { return nil }
return self._embed(value, root)
},
extract: { root in
guard let root = root else { return nil }
return self._extract(root)
}
)
}
/// Returns a root by embedding a value.
///
/// Note: Value will not be embed if FunctionalKeyPath was initialized by default (non-writable) `KeyPath` via `getonly` function
///
/// - Parameter value: A value to embed.
/// - Returns: A root that embeds `value`.
@inlinable
public func embed(_ value: Value, in root: Root) -> Root {
_embed(value, root)
}
/// Returns a root by embedding a value.
///
/// Note: Value will not be embed if FunctionalKeyPath was initialized by default (non-writable) `KeyPath` via `getonly` function
///
/// - Parameter value: A value to embed.
@inlinable
public func embed(_ value: Value, in root: inout Root) {
root = embed(value, in: root)
}
/// Attempts to extract a value from a root.
///
/// - Parameter root: A root to extract from.
/// - Returns: A value iff it can be extracted from the given root, otherwise `nil`.
@inlinable
public func extract(from root: Root) -> Value {
_extract(root)
}
/// Returns a new functional keyPath created by appending the given functional keyPath to this one.
///
/// Use this method to extend this functional keyPath to the value type of another functional keyPath.
///
/// - Parameter path: The functional keyPath to append.
/// - Returns: A functional keyPath from the root of this functional keyPath to the value type of `path`.
@inlinable
public func appending<AppendedValue>(path: FunctionalKeyPath<Value, AppendedValue>)
-> FunctionalKeyPath<Root, AppendedValue>
{
FunctionalKeyPath<Root, AppendedValue>(
embed: { appendedValue, root in
self.embed(
path.embed(
appendedValue,
in: self.extract(from: root)
),
in: root
)
},
extract: { root in
path.extract(
from: self.extract(from: root)
)
}
)
}
/// Returns a new functional keyPath created by appending the given functional keyPath to this one.
///
/// Use this method to extend this functional keyPath to the value type of another functional keyPath.
///
/// - Parameter path: The functional keyPath to append.
/// - Returns: A functional keyPath from the root of this functional keyPath to the value type of `path`.
@inlinable
public func appending<Wrapped, AppendedValue>(
path: FunctionalKeyPath<Wrapped, AppendedValue>
) -> FunctionalKeyPath<Root, AppendedValue?> where Value == Wrapped? {
appending(path: path.optional())
}
}
extension FunctionalKeyPath {
@inlinable
public static func key<Key: Hashable, _Value>(
_ key: Key
) -> FunctionalKeyPath
where Root == [Key: _Value], Value == _Value?
{
FunctionalKeyPath(
embed: { value, root in
reduce(root) { $0[key] = value }
},
extract: { $0[key] }
)
}
@inlinable
public static func index(_ index: Root.Index) -> FunctionalKeyPath
where Root: MutableCollection, Value == Root.Element
{
FunctionalKeyPath(
embed: { value, root in
reduce(root) { root in
root[index] = value
}
},
extract: { root in
root[index]
}
)
}
@inlinable
public static func getonlyIndex(_ index: Root.Index) -> FunctionalKeyPath
where Root: Collection, Value == Root.Element
{
FunctionalKeyPath(
embed: { _, root in root },
extract: { $0[index] }
)
}
@inlinable
public static func safeIndex(_ index: Root.Index) -> FunctionalKeyPath<Root, Value?>
where Root == [Value]
{
FunctionalKeyPath<Root, Value?>(
embed: { value, root in
reduce(root) { root in
guard
let value = value,
root.indices.contains(index)
else { return }
root[index] = value
}
},
extract: { root in
root.indices.contains(index)
? root[index]
: nil
}
)
}
}