Keying and Grouping
Convert a sequence to a dictionary, providing keys to individual elements or to use as grouping values.
Creating a Keyed Dictionary
func keyed<Key>(by: (Element) throws -> Key
) rethrows -> [Key : Element] Creates a new Dictionary from the elements of
self
, keyed by the results returned by the givenkeyForValue
closure.func keyed<Key>(by: (Element) throws -> Key, resolvingConflictsWith: (Key, Element, Element) throws -> Element
) rethrows -> [Key : Element] Creates a new Dictionary from the elements of
self
, keyed by the results returned by the givenkeyForValue
closure. As the dictionary is built, the initializer calls theresolve
closure with the current and new values for any duplicate keys. Pass a closure asresolve
that returns the value to use in the resulting dictionary: The closure can choose between the two values, combine them to produce a new value, or even throw an error.
Grouping Elements by Key
func grouped<GroupKey>(by: (Element) throws -> GroupKey
) rethrows -> [GroupKey : [Element]] Groups up elements of
self
into a new Dictionary, whose values are Arrays of grouped elements, each keyed by the group key returned by the given closure.