Created
October 4, 2014 13:17
-
-
Save pyrtsa/add3efbef6384e5ec3b2 to your computer and use it in GitHub Desktop.
Grouping the elements of a sequence by a key function. (This may not be the fastest implementation out there.)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func groupBy<C: CollectionType, K: Hashable> | |
(xs: C, key: C.Generator.Element -> K) -> [K:[C.Generator.Element]] | |
{ | |
var gs: [K:[C.Generator.Element]] = [:] | |
for x in xs { | |
let k = key(x) | |
var ys = gs[k] ?? [] | |
ys.append(x) | |
gs.updateValue(ys, forKey: k) | |
} | |
return gs | |
} | |
func isEven<T: protocol<IntegerArithmeticType, IntegerLiteralConvertible>>(n: T) -> Bool { return n % 2 == 0 } | |
func isOdd <T: protocol<IntegerArithmeticType, IntegerLiteralConvertible>>(n: T) -> Bool { return n % 2 != 0 } | |
groupBy([0,1,0,3,4,2,2,0], isEven) //=> [false: [1, 3], true: [0, 0, 4, 2, 2, 0]] | |
groupBy([0,1,0,3,4,2,2,0]) {$0 % 3} //=> [2: [2, 2], 0: [0, 0, 3, 0], 1: [1, 4]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I couldn't find a good way to perform updates in-place to the arrays already contained in the dictionary. Maybe something like this might work: