Skip to content

Instantly share code, notes, and snippets.

@aryaxt
aryaxt / Swift SequenceType GroupBy
Last active February 4, 2020 13:55
Swift Array GroupBy
extension Sequence {
func groupBy<G: Hashable>(closure: (Iterator.Element)->G) -> [G: [Iterator.Element]] {
var results = [G: Array<Iterator.Element>]()
forEach {
let key = closure($0)
if var array = results[key] {
array.append($0)