Last active
December 23, 2015 23:29
-
-
Save InsertNetan/4c609f81390c9ad62347 to your computer and use it in GitHub Desktop.
swift CollectionType extension to map any collection to a dictionary
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
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