Skip to content

Instantly share code, notes, and snippets.

@InsertNetan
Last active December 23, 2015 23:29
Show Gist options
  • Save InsertNetan/4c609f81390c9ad62347 to your computer and use it in GitHub Desktop.
Save InsertNetan/4c609f81390c9ad62347 to your computer and use it in GitHub Desktop.
swift CollectionType extension to map any collection to a dictionary
extension CollectionType {
@warn_unused_result
func toDictionary<K, V>
(@noescape transform:(element: Self.Generator.Element) -> (key: K, value: V)?) -> [K : V] {
return self.reduce([:]) { (var dictionary, e) in
if let (key, value) = transform(element: e){
dictionary[key] = value
}
return dictionary
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment