Created
August 21, 2017 21:23
-
-
Save JohnSundell/1956ce36b9303eb4bf912da0de9e2844 to your computer and use it in GitHub Desktop.
A way to easily compare a given value against an array of candidates
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
import Foundation | |
struct EquatableValueSequence<T: Equatable> { | |
static func ==(lhs: EquatableValueSequence<T>, rhs: T) -> Bool { | |
return lhs.values.contains(rhs) | |
} | |
static func ==(lhs: T, rhs: EquatableValueSequence<T>) -> Bool { | |
return rhs == lhs | |
} | |
fileprivate let values: [T] | |
} | |
func any<T: Equatable>(of values: T...) -> EquatableValueSequence<T> { | |
return EquatableValueSequence(values: values) | |
} | |
let string = "Three" | |
// Instead of multiple conditions like this: | |
if string == "One" || string == "Two" || string == "Three" { | |
} | |
// You can now do: | |
if string == any(of: "One", "Two", "Three") { | |
} |
lol, dope! love the ∈
But... but... why not just ['One', 'Two', 'Three'].contains('One')
?
@matt-curtis hahahahahaha.
The ∈ is really nice. But I guess I can only use it with the clipboard
真是脱裤子放屁——多此一举
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can I play, please please please, can I, please please? 😆