-
-
Save ecmadao/6eab0a784acd1e6668dbfbc8cf6cd1bb 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") { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment