Created
October 26, 2017 16:56
-
-
Save bmichotte/0d0c487ff61e74bc34ca049bcefd3b96 to your computer and use it in GitHub Desktop.
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
| struct A { let a: String } | |
| let array = [A(a: "b"), A(a: "C"), A(a: "b")] | |
| let dictionary = Dictionary(grouping: array) { $0.a } | |
| dictionary | |
| extension Array { | |
| func group<K: Hashable, Element>(_ fn: (Element) -> K) -> Dictionary<K, [Element]> { | |
| return Dictionary<K, [Element]>(grouping: self, by: fn) | |
| } | |
| } | |
| let dictionary2 = array.group { $0.a } | |
| dictionary2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Remove
Elementas a generic type local to the function (since that override'sArray's associatedElementtype). So replace line 7 with this:And it'll work 🎉