Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Foundation
- internal extension Sequence {
- func grouped<T>(by keyPath: KeyPath<Element, T>) -> [T: [Element]] {
- var groups = [T: [Element]]()
- for element in self {
- let key = element[keyPath: keyPath]
- if !groups.keys.contains(key) {
- groups[key] = [Element]()
- }
- groups[key]?.append(element)
- }
- return groups
- }
- }
Add Comment
Please, Sign In to add comment