Skip to content

Instantly share code, notes, and snippets.

@maxsz
Created July 25, 2016 14:08
Show Gist options
  • Save maxsz/267eb54c672116f4245fdd52b5a5da7d to your computer and use it in GitHub Desktop.
Save maxsz/267eb54c672116f4245fdd52b5a5da7d to your computer and use it in GitHub Desktop.
Swift SequenceType Extensions
extension SequenceType {
// Index an array with a property.
@warn_unused_result
func index<T: Hashable>(@noescape by indexFunc: (Generator.Element -> T)) -> [T: Generator.Element] {
var dict: [T: Generator.Element] = [:]
for element in self {
let index = indexFunc(element)
dict[index] = element
}
return dict
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment