You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -20,7 +20,7 @@ Convenient methods like `resolve(favoring:)` and `merge(coalesce:)` also provide
20
20
21
21
Transform a `Delta` value to a different `Delta` value using `map(:)` or `asyncMap(:)`.
22
22
23
-
`Delta` works well when working with optionals, providing initializers to create a `Delta` from optionals as well as alternative methods like `compactMap(:)` and `compactMerge(coalesce:)` to produce optionals.
23
+
`Delta` works well when working with optionals, providing initializers to create a `Delta` from optionals as well as alternative methods like `flatMap(:)`, `compactMap(:)`, and `compactMerge(coalesce:)` to produce optionals.
24
24
25
25
The `Delta` type also conforms to all standard protocols (depending on the conformances of it’s `Element` type):
Copy file name to clipboardExpand all lines: Sources/LightTableDelta/Delta.swift
+39Lines changed: 39 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -105,7 +105,46 @@ public extension Delta where Element: ~Copyable {
105
105
}
106
106
}
107
107
108
+
/// Returns a delta containing the results of mapping the given closure over the delta’s elements, or `nil`, if the closure returns `nil` for all elements.
109
+
///
110
+
/// The notable difference to `compactMap(:)` is that this method does not return `nil` when `transform` returns `nil` for only one element in the `transition` case.
111
+
/// Instead, a `source` or `target` delta is returned, matching the non-`nil` return value.
112
+
@inlinable
113
+
consumingfunc flatMap<T:~Copyable, E>(
114
+
_ transform:(consumingElement)throws(E)->T?
115
+
)throws(E)->Delta<T>?{
116
+
switch consume self {
117
+
case.source(let source):
118
+
guardlet source =trytransform(source)else{
119
+
returnnil
120
+
}
121
+
return.source(source)
122
+
case.target(let target):
123
+
guardlet target =trytransform(target)else{
124
+
returnnil
125
+
}
126
+
return.target(target)
127
+
case.transition(let source,let target):
128
+
letsource=trytransform(source)
129
+
lettarget=trytransform(target)
130
+
returnif source !=nil && target !=nil{
131
+
.transition(source: source!, target: target!)
132
+
}
133
+
elseiflet source {
134
+
.source(source)
135
+
}
136
+
elseiflet target {
137
+
.target(target)
138
+
}
139
+
else{
140
+
nil
141
+
}
142
+
}
143
+
}
144
+
108
145
/// Returns a delta containing the results of mapping the given closure over the delta’s elements, or `nil`, if the closure returns `nil` for any element.
146
+
///
147
+
/// The notable difference to `flatMap(:)` is that this method also returns `nil` when `transform` returns `nil` for only one element in the `transition` case.
0 commit comments