Created
September 29, 2015 04:05
-
-
Save maxchuquimia/ac616737f65dad287024 to your computer and use it in GitHub Desktop.
Map an array into a dictionary with Swift 2.0
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
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