Skip to content

Instantly share code, notes, and snippets.

@maxchuquimia
Created September 29, 2015 04:05
Show Gist options
  • Save maxchuquimia/ac616737f65dad287024 to your computer and use it in GitHub Desktop.
Save maxchuquimia/ac616737f65dad287024 to your computer and use it in GitHub Desktop.
Map an array into a dictionary with Swift 2.0
extension Array {
/*!
Convert an array to a dictionary of two of it's element's properties
- parameter f: the parse block
- returns: a dictionary of the type of the tuple returned by the parse block
*/
func toDict<K, V>(f: (Element -> (K, V))) -> [K: V] {
var dict = [K: V]()
forEach { (e) -> () in
let (k, v) = f(e)
dict[k] = v
}
return dict
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment