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
The Dictionary.init(grouping: by: ) method uses the mapping function in by to convert each value in grouping to a Key and then creates a dictionary from the found Key values to Array-s of mapped Value values. There should be a way to override which RangeReplaceableCollection is used. It probably won't be common, except grouping sequences of Character to String, but it's still the right thing to do.
extensionDictionary {
/// Creates a new dictionary whose keys are the groupings returned by the/// given closure and whose values are collections of the elements, using a/// given type, that returned each key.////// The collections 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 a string, then creates a dictionary from/// that string by grouping by a filtering predicate.////// let vowels = Set<Character> = ["a", "e", "i", "o", "u"]/// let swiftMessage = "Swift Dictionary"/// let swiftMsgByType = Dictionary(grouping: swiftMessage, as: String.self, by: { vowels.contains($0) })/// // [false: "Swft Dctnry", true: "iiioa"]////// The new `swiftMsgByType` dictionary has two entries, one grouping/// non-vowels (key `false`) and one group with vowels (key `true`).////// - Parameters:/// - values: A sequence of values to group into a dictionary./// - type: A metatype specifier for the per-key collection type of/// elements./// - keyForValue: A closure that returns a key for each element in/// `values`.@inlinablepublicinit<S: Sequence>(groupingvalues: S, astype: Value.Type, bykeyForValue: (S.Element) throws -> Key) rethrowswhereValue: RangeReplaceableCollection, Value.Element == S.Element {
letarrayedDictionary = tryDictionary<Key, [Value.Element]>(grouping: values, by: keyForValue)
self = arrayedDictionary.mapValues(Value.init)
}
}
The sample code implements the new initializer in terms of the current one, but the real final code could reverse the dependency.
The text was updated successfully, but these errors were encountered:
Additional Detail from JIRA
md5: c8b8351e5e286e8b1670b7994f1c92e3
Issue Description:
The
Dictionary.init(grouping: by: )
method uses the mapping function inby
to convert each value ingrouping
to aKey
and then creates a dictionary from the foundKey
values toArray
-s of mappedValue
values. There should be a way to override whichRangeReplaceableCollection
is used. It probably won't be common, except grouping sequences ofCharacter
toString
, but it's still the right thing to do.The sample code implements the new initializer in terms of the current one, but the real final code could reverse the dependency.
The text was updated successfully, but these errors were encountered: