Skip to content

Commit 5daad12

Browse files
numistairspeedswift
authored andcommitted
[amendment] Add inverse() to SE-0240 for API parity with Foundation (#1052)
* [amendment] Add `inverse()` to SE-0240 for API parity with -[NSOrderedCollectionDifference inverseDifference] * Kick off amendment review
1 parent 1e2b652 commit 5daad12

File tree

1 file changed

+5
-21
lines changed

1 file changed

+5
-21
lines changed

proposals/0240-ordered-collection-diffing.md

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
* Proposal: [SE-0240](0240-ordered-collection-diffing.md)
44
* Authors: [Scott Perry](https://github.com/numist), [Kyle Macomber](https://github.com/kylemacomber)
5-
* Review Manager: [Doug Gregor](https://github.com/DougGregor)
5+
* Review Manager: [Doug Gregor](https://github.com/DougGregor), [Ben Cohen](https://github.com/AirspeedSwift)
66
* Status: **Implemented (Swift 5.1)**
7+
* Amendment status: **Active Review (June 19 - 25 2019)**
78
* Implementation: [apple/swift#21845](https://github.com/apple/swift/pull/21845)
89
* Decision notes: [Rationale](https://forums.swift.org/t/accepted-with-modifications-se-0240-ordered-collection-diffing/20008)
910

@@ -140,6 +141,9 @@ public struct CollectionDifference<ChangeElement> {
140141

141142
/// The `.remove` changes contained by this difference, from lowest offset to highest
142143
public var removals: [Change] { get }
144+
145+
/// Produces a difference that is the functional inverse of `self`
146+
public func inverse() -> CollectionDifference<ChangeElement>
143147
}
144148

145149
/// A CollectionDifference is itself a Collection.
@@ -290,26 +294,6 @@ The name `CollectionDifference` gives us the opportunity to build a family of re
290294

291295
This API allows for more interesting functionality that is not included in this proposal.
292296

293-
For example, this propsal could have included a `inverted()` function on the difference type that would return a new difference that would undo the application of the original.
294-
295-
The lack of additional conveniences and functionality is intentional; the goal of this proposal is to lay the groundwork that such extensions would be built upon.
296-
297-
In the case of `inverted()`, clients of the API in this proposal can use `Collection.map()` to invert the case of each `Change` and feed the result into `CollectionDifference.init(_:)`:
298-
299-
``` swift
300-
let diff: CollectionDifference<Int> = /* ... */
301-
let inverted = CollectionDifference<Int>(
302-
diff.map({(change) -> CollectionDifference<Int>.Change in
303-
switch change {
304-
case .insert(offset: let o, element: let e, associatedWith: let a):
305-
return .remove(offset: o, element: e, associatedWith: a)
306-
case .remove(offset: let o, element: let e, associatedWith: let a):
307-
return .insert(offset: o, element: e, associatedWith: a)
308-
}
309-
})
310-
)!
311-
```
312-
313297
### `mutating apply(_:)`
314298

315299
There is no mutating applicator because there is no algorithmic advantage to in-place application.

0 commit comments

Comments
 (0)