Skip to content

Commit b674195

Browse files
committed
Rename uniquingKeysWith → resolvingConflictsWith
1 parent dd2753a commit b674195

File tree

4 files changed

+13
-13
lines changed

4 files changed

+13
-13
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 runtime error by default. Alternatively, you can provide
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: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,28 +49,28 @@ extension Sequence {
4949
/// Creates a new Dictionary from the elements of `self`, keyed by the
5050
/// results returned by the given `keyForValue` closure. As the dictionary is
5151
/// built, the initializer calls the `combine` closure with the current and
52-
/// new values for any duplicate keys. Pass a closure as `combine` that
52+
/// new values for any duplicate keys. Pass a closure as `resolve` that
5353
/// returns the value to use in the resulting dictionary: The closure can
5454
/// choose between the two values, combine them to produce a new value, or
5555
/// even throw an error.
5656
///
5757
/// - Parameters:
5858
/// - keyForValue: A closure that returns a key for each element in `self`.
59-
/// - combine: A closure that is called with the values for any duplicate
59+
/// - resolve: A closure that is called with the values for any duplicate
6060
/// keys that are encountered. The closure returns the desired value for
6161
/// the final dictionary.
6262
@inlinable
6363
public func keyed<Key>(
6464
by keyForValue: (Element) throws -> Key,
65-
uniquingKeysWith combine: (Key, Element, Element) throws -> Element
65+
resolvingConflictsWith resolve: (Key, Element, Element) throws -> Element
6666
) rethrows -> [Key: Element] {
6767
var result = [Key: Element]()
6868

6969
for element in self {
7070
let key = try keyForValue(element)
7171

7272
if let oldValue = result.updateValue(element, forKey: key) {
73-
let valueToKeep = try combine(key, oldValue, element)
73+
let valueToKeep = try resolve(key, oldValue, element)
7474

7575
// This causes a second look-up for the same key. The standard library can avoid that
7676
// by calling `mutatingFind` to get access to the bucket where the value will end up,

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)