Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-11442] Add generalized variant of Dictionary.init(grouping: by:) #53843

Open
swift-ci opened this issue Sep 10, 2019 · 0 comments
Open

[SR-11442] Add generalized variant of Dictionary.init(grouping: by:) #53843

swift-ci opened this issue Sep 10, 2019 · 0 comments
Labels
new feature standard library Area: Standard library umbrella

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-11442
Radar None
Original Reporter CTMacUser (JIRA User)
Type New Feature
Additional Detail from JIRA
Votes 0
Component/s Standard Library
Labels New Feature
Assignee None
Priority Medium

md5: c8b8351e5e286e8b1670b7994f1c92e3

Issue Description:

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.

extension Dictionary {

    /// 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`.
    @inlinable
    public init<S: Sequence>(grouping values: S, as type: Value.Type, by keyForValue: (S.Element) throws -> Key) rethrows where Value: RangeReplaceableCollection, Value.Element == S.Element {
        let arrayedDictionary = try Dictionary<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.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature standard library Area: Standard library umbrella
Projects
None yet
Development

No branches or pull requests

1 participant