Last active
November 11, 2016 18:57
-
-
Save bfernandesbfs/0ace376521f93c026e859da75fe312e9 to your computer and use it in GitHub Desktop.
Revisions
-
bfernandesbfs revised this gist
Nov 11, 2016 . 1 changed file with 5 additions and 5 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -25,10 +25,10 @@ extension Person: Hashable { let p = [Person(name: "Jack", age: 22), Person(name: "Anne", age: 26), Person(name: "Mary", age: 27), Person(name: "Anne", age: 26)] print(unique(p)) p.contains(Person(name: "Bruno", age: 22)) -
bfernandesbfs renamed this gist
Nov 11, 2016 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
bfernandesbfs created this gist
Nov 11, 2016 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,34 @@ func unique<S: Sequence, E: Hashable>(_ source: S) -> [E] where E==S.Iterator.Element { var seen: [E:Bool] = [:] return source.filter { seen.updateValue(true, forKey: $0) == nil } } let a = ["four","one", "two", "one", "three","four", "four"] print(unique(a)) struct Person { var name: String var age: Int } extension Person: Hashable { var hashValue: Int { get { return name.hashValue ^ age.hashValue } } static func ==(l: Person, r: Person) -> Bool { return l.hashValue == r.hashValue } } let p = [People(name: "Jack", age: 22), People(name: "Anne", age: 26), People(name: "Mary", age: 27), People(name: "Anne", age: 26)] print(unique(p)) p.contains(People(name: "Bruno", age: 22))