Skip to content

Instantly share code, notes, and snippets.

@jakehawken
Created March 14, 2018 04:40
Show Gist options
  • Select an option

  • Save jakehawken/7a03f8fd9a02a773ffa8989b18e6ad2e to your computer and use it in GitHub Desktop.

Select an option

Save jakehawken/7a03f8fd9a02a773ffa8989b18e6ad2e to your computer and use it in GitHub Desktop.
A block-based approach to inflating an array into a dictionary.
extension Array {
func mapToDictionary<Key:Hashable,Value>(mapBlock: (Element)->(Key,Value)) -> [Key:Value] {
var dict = [Key:Value]()
self.forEach { (element) in
let kvTuple: (Key,Value) = mapBlock(element)
dict[kvTuple.0] = kvTuple.1
}
return dict
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment