Skip to content

Commit 4ca6551

Browse files
committed
Rename uniquingKeysWith → resolvingConflictsWith
1 parent 8c7df22 commit 4ca6551

File tree

4 files changed

+18
-26
lines changed

4 files changed

+18
-26
lines changed

Guides/Keyed.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Duplicate keys throw an error by default. Alternatively, you can provide a closu
2222
let fruits = ["Apricot", "Banana", "Apple", "Cherry", "Blackberry", "Avocado", "Coconut"]
2323
let fruitsByLetter = fruits.keyed(
2424
by: { $0.first! },
25-
uniquingKeysWith: { key, old, new in new } // Always pick the latest fruit
25+
resolvingConflictsWith: { key, old, new in new } // Always pick the latest fruit
2626
)
2727
// Results in:
2828
// [
@@ -34,7 +34,7 @@ let fruitsByLetter = fruits.keyed(
3434

3535
## Detailed Design
3636

37-
The `keyed(by:)` and `keyed(by:uniquingKeysWith:)` methods are declared in an `Sequence` extension, both returning `[Key: Element]`.
37+
The `keyed(by:)` and `keyed(by:resolvingConflictsWith:)` methods are declared in an `Sequence` extension, both returning `[Key: Element]`.
3838

3939
```swift
4040
extension Sequence {
@@ -44,7 +44,7 @@ extension Sequence {
4444

4545
public func keyed<Key>(
4646
by keyForValue: (Element) throws -> Key,
47-
uniquingKeysWith combine: ((Key, Element, Element) throws -> Element)? = nil
47+
resolvingConflictsWith resolve: ((Key, Element, Element) throws -> Element)? = nil
4848
) rethrows -> [Key: Element]
4949
}
5050
```

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Read more about the package, and the intent behind it, in the [announcement on s
4848
- [`grouped(by:)](https://github.com/apple/swift-algorithms/blob/main/Guides/Grouped.md): Group up elements using the given closure, returning a Dictionary of those groups, keyed by the results of the closure.
4949
- [`indexed()`](https://github.com/apple/swift-algorithms/blob/main/Guides/Indexed.md): Iterate over tuples of a collection's indices and elements.
5050
- [`interspersed(with:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Intersperse.md): Place a value between every two elements of a sequence.
51-
- [`keyed(by:)`, `keyed(by:uniquingKeysBy:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Keyed.md): Returns a Dictionary that associates elements of a sequence with the keys returned by the given closure.
51+
- [`keyed(by:)`, `keyed(by:resolvingConflictsBy:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Keyed.md): Returns a Dictionary that associates elements of a sequence with the keys returned by the given closure.
5252
- [`partitioningIndex(where:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Partition.md): Returns the starting index of the partition of a collection that matches a predicate.
5353
- [`reductions(_:)`, `reductions(_:_:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Reductions.md): Returns all the intermediate states of reducing the elements of a sequence or collection.
5454
- [`split(maxSplits:omittingEmptySubsequences:whereSeparator)`, `split(separator:maxSplits:omittingEmptySubsequences)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Split.md): Lazy versions of the Standard Library's eager operations that split sequences and collections into subsequences separated by the specified separator element.

Sources/Algorithms/Keyed.swift

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,49 +35,41 @@ extension Sequence {
3535
public func keyed<Key>(
3636
by keyForValue: (Element) throws -> Key
3737
) throws -> [Key: Element] {
38-
var result = [Key: Element]()
39-
40-
for element in self {
41-
let key = try keyForValue(element)
42-
43-
if let previousElement = result.updateValue(element, forKey: key) {
44-
throw KeysAreNotUnique(key: key, previousElement: previousElement, conflictingElement: element)
45-
}
46-
}
47-
48-
return result
38+
try self.keyed(by: keyForValue, resolvingConflictsWith: {
39+
throw KeysAreNotUnique(key: $0, previousElement: $1, conflictingElement: $2)
40+
})
4941
}
5042

5143
/// Creates a new Dictionary from the elements of `self`, keyed by the
5244
/// results returned by the given `keyForValue` closure. As the dictionary is
53-
/// built, the initializer calls the `combine` closure with the current and
54-
/// new values for any duplicate keys. Pass a closure as `combine` that
45+
/// built, the initializer calls the `resolve` closure with the current and
46+
/// new values for any duplicate keys. Pass a closure as `resolve` that
5547
/// returns the value to use in the resulting dictionary: The closure can
5648
/// choose between the two values, combine them to produce a new value, or
5749
/// even throw an error.
5850
///
5951
/// - Parameters:
6052
/// - keyForValue: A closure that returns a key for each element in `self`.
61-
/// - combine: A closure that is called with the values for any duplicate
53+
/// - resolve: A closure that is called with the values for any duplicate
6254
/// keys that are encountered. The closure returns the desired value for
6355
/// the final dictionary.
6456
@inlinable
6557
public func keyed<Key>(
6658
by keyForValue: (Element) throws -> Key,
67-
uniquingKeysWith combine: (Key, Element, Element) throws -> Element
59+
resolvingConflictsWith resolve: (Key, Element, Element) throws -> Element
6860
) rethrows -> [Key: Element] {
6961
var result = [Key: Element]()
7062

7163
for element in self {
7264
let key = try keyForValue(element)
7365

7466
if let oldValue = result.updateValue(element, forKey: key) {
75-
let valueToKeep = try combine(key, oldValue, element)
67+
let valueToKeep = try resolve(key, oldValue, element)
7668

7769
// This causes a second look-up for the same key. The standard library can avoid that
7870
// by calling `mutatingFind` to get access to the bucket where the value will end up,
7971
// and updating in place.
80-
// Swift Algorithms doesn't have access to that API, so we make due.
72+
// Swift Algorithms doesn't have access to that API, so we make do.
8173
// When this gets merged into the standard library, we should optimize this.
8274
result[key] = valueToKeep
8375
}

Tests/SwiftAlgorithmsTests/KeyedTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,16 @@ final class KeyedTests: XCTestCase {
4141
}
4242

4343
func testNonUniqueKeysWithMergeFunction() {
44-
var combineCallHistory = [(key: Character, current: String, new: String)]()
44+
var resolveCallHistory = [(key: Character, current: String, new: String)]()
4545
let expectedCallHistory = [
4646
(key: "A", current: "Apple", new: "Avocado"),
4747
(key: "C", current: "Cherry", new: "Coconut"),
4848
]
4949

5050
let d = ["Apple", "Avocado", "Banana", "Cherry", "Coconut"].keyed(
5151
by: { $0.first! },
52-
uniquingKeysWith: { key, older, newer in
53-
combineCallHistory.append((key, older, newer))
52+
resolvingConflictsWith: { key, older, newer in
53+
resolveCallHistory.append((key, older, newer))
5454
return "\(older)-\(newer)"
5555
}
5656
)
@@ -62,7 +62,7 @@ final class KeyedTests: XCTestCase {
6262
XCTAssertNil(d["D"])
6363

6464
XCTAssertEqual(
65-
combineCallHistory.map(String.init(describing:)), // quick/dirty workaround: tuples aren't Equatable
65+
resolveCallHistory.map(String.init(describing:)), // quick/dirty workaround: tuples aren't Equatable
6666
expectedCallHistory.map(String.init(describing:))
6767
)
6868
}
@@ -83,7 +83,7 @@ final class KeyedTests: XCTestCase {
8383
let error = SampleError()
8484

8585
XCTAssertThrowsError(
86-
try input.keyed(by: { $0.first! }, uniquingKeysWith: { _, _, _ in throw error })
86+
try input.keyed(by: { $0.first! }, resolvingConflictsWith: { _, _, _ in throw error })
8787
) { thrownError in
8888
XCTAssertIdentical(error, thrownError as? SampleError)
8989
}

0 commit comments

Comments
 (0)