Merge pull request #140 from apple/fix-OrderedDictionary-grouping-inference

[OrderedDictionary] Fix type inference issue with `OrderedDictionary.init(grouping:by:)`
This commit is contained in:
Karoy Lorentey authored and GitHub committed 2022-02-09 11:55:48 -08:00
commit 20e7fceec8
2 files changed
+72 -2

No files matched your search

@@ -278,6 +278,50 @@ extension OrderedDictionary {
public init<S: Sequence>(
grouping values: S,
by keyForValue: (S.Element) throws -> Key
) rethrows where Value: RangeReplaceableCollection, Value.Element == S.Element {
try self.init(_grouping: values, by: keyForValue)
}
/// Creates a new dictionary whose keys are the groupings returned by the
/// given closure and whose values are arrays of the elements that returned
/// each key.
///
/// The arrays in the "values" position of the new dictionary each contain at
/// least one element, with the elements in the same order as the source
/// sequence.
///
/// The following example declares an array of names, and then creates a
/// dictionary from that array by grouping the names by first letter:
///
/// let students = ["Kofi", "Abena", "Efua", "Kweku", "Akosua"]
/// let studentsByLetter = OrderedDictionary(grouping: students, by: { $0.first! })
/// // ["K": ["Kofi", "Kweku"], "A": ["Abena", "Akosua"], "E": ["Efua"]]
///
/// The new `studentsByLetter` dictionary has three entries, with students'
/// names grouped by the keys `"E"`, `"K"`, and `"A"`.
///
/// - Parameters:
/// - values: A sequence of values to group into a dictionary.
/// - keyForValue: A closure that returns a key for each element in
/// `values`.
///
/// - Complexity: Expected O(*n*) on average, where *n* is the count of
/// values, if `Key` implements high-quality hashing.
@inlinable
public init<S: Sequence>(
grouping values: S,
by keyForValue: (S.Element) throws -> Key
) rethrows where Value == [S.Element] {
// Note: this extra overload is necessary to make type inference work
// for the `Value` type -- we want it to default to `[S.Element`].
// (https://github.com/apple/swift-collections/issues/139)
try self.init(_grouping: values, by: keyForValue)
}
@inlinable
internal init<S: Sequence>(
_grouping values: S,
by keyForValue: (S.Element) throws -> Key
) rethrows where Value: RangeReplaceableCollection, Value.Element == S.Element {
self.init()
for value in values {
@@ -123,14 +123,40 @@ class OrderedDictionaryTests: CollectionTestCase {
"one", "two", "three", "four", "five",
"six", "seven", "eight", "nine", "ten"
]
let d = OrderedDictionary<Int, [String]>(grouping: items, by: { $0.count })
let d = OrderedDictionary<Int, ContiguousArray<String>>(
grouping: items, by: { $0.count })
expectEqualElements(d, [
(key: 3, value: ["one", "two", "six", "ten"]),
(key: 5, value: ["three", "seven", "eight"]),
(key: 4, value: ["four", "five", "nine"]),
])
] as [(key: Int, value: ContiguousArray<String>)])
}
func test_grouping_initializer_inference() {
struct StatEvent: Equatable {
var name: String
var date: String
var hours: Int
}
let statEvents = [
StatEvent(name: "lunch", date: "01-01-2015", hours: 1),
StatEvent(name: "dinner", date: "01-01-2015", hours: 1),
StatEvent(name: "dinner", date: "01-01-2015", hours: 1),
StatEvent(name: "lunch", date: "01-01-2015", hours: 1),
StatEvent(name: "dinner", date: "01-01-2015", hours: 1)
]
// We expect this to be inferred to `OrderedDictionary<String, [StatEvent]>`
let d = OrderedDictionary(grouping: statEvents, by: { $0.name })
let expected = [
(key: "lunch", value: [statEvents[0], statEvents[3]]),
(key: "dinner", value: [statEvents[1], statEvents[2], statEvents[4]]),
]
expectEqualElements(d, expected)
}
func test_uncheckedUniqueKeysWithValues_labeled_tuples() {
let items: KeyValuePairs<String, Int> = [
"zero": 0,