Skip to content

Instantly share code, notes, and snippets.

@Palleas
Created October 4, 2016 14:22
Show Gist options
  • Save Palleas/115990fa585e86dc535a8313f815117f to your computer and use it in GitHub Desktop.
Save Palleas/115990fa585e86dc535a8313f815117f to your computer and use it in GitHub Desktop.
extension SequenceType {
func groupBy<Key: Hashable>(handler: (Generator.Element) -> Key) -> [Key: [Generator.Element]] {
var grouped = [Key: Array<Generator.Element>]()
self.forEach { item in
let key = handler(item)
if grouped[key] == nil {
grouped[key] = []
}
grouped[key]?.append(item)
}
return grouped
}
}
@Abizern
Copy link

Abizern commented Oct 4, 2016

In Swift 3 you could do this as:

//: Playground - noun: a place where people can play

import UIKit
import PlaygroundSupport

// Requires Swift 3

extension Sequence {
    func groupBy<Key: Hashable>(handler: (Iterator.Element) -> Key) -> [Key: [Iterator.Element]] {
        var grouped = [Key: Array<Iterator.Element>]()

        self.forEach { item in
            let key = handler(item)
            if grouped[key] == nil {
                grouped[key] = [item]
            } else {
                grouped[key]?.append(item)
            }
        }

        return grouped
    }
}


// Usage

let list = Array(1...10) as [Int]
let group = list.groupBy { (number) -> String in
    switch number % 2 {
    case 0:
        return "Even"
    default:
        return "Odd"
    }
}

group

By actually setting a value in the case where the group doesn't exist, you save 2 calls in this example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment