Skip to content

Instantly share code, notes, and snippets.

@daehn

daehn/OneOf.swift

Last active Apr 10, 2019
Embed
What would you like to do?
import Foundation
extension Equatable {
/// Whether `self` is contained in a list of other values.
/// Variadic version, see the method below for the generic implementation.
///
/// - Parameter others: The values that `self` should be checked against.
/// - Returns: Whether or not `self` is one of the provided other values.
func isOne(of others: Self...) -> Bool {
return isOne(of: others)
}
/// Whether `self` is contained in a list of other values.
/// Non-variadic version that accepts any sequence of the same type.
///
/// - Parameter others: The values that `self` should be checked against.
/// - Returns: Whether or not `self` is one of the provided other values.
func isOne<T: Sequence>(of others: T) -> Bool where T.Element == Self {
return others.contains(self)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment