Created
March 14, 2018 04:40
-
-
Save jakehawken/7a03f8fd9a02a773ffa8989b18e6ad2e to your computer and use it in GitHub Desktop.
A block-based approach to inflating an array into 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 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