Created
May 23, 2016 10:14
-
-
Save naithar/4bfbdf044dca20a3ea7740d2da9ec8c5 to your computer and use it in GitHub Desktop.
This file contains 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
public extension SequenceType { | |
/// Categorises elements of self into a dictionary, with the keys given by keyFunc | |
func categorise<U : Hashable>(@noescape keyFunc: Generator.Element -> U) -> [U:[Generator.Element]] { | |
var dict: [U:[Generator.Element]] = [:] | |
for el in self { | |
let key = keyFunc(el) | |
if case nil = dict[key]?.append(el) { dict[key] = [el] } | |
} | |
return dict | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment