Overload 'in' method for Comparable to use ClosedRange

This commit is contained in:
Vitaliy Salnikov 2020-01-23 16:33:20 +03:00
parent 16b209cd59
commit 44ccb8dd28
1 changed files with 7 additions and 0 deletions

View File

@ -30,4 +30,11 @@ public extension Comparable {
func `in`(bounds: (lower: Self, upper: Self)) -> Self {
return min(max(bounds.lower, self), bounds.upper)
}
/// Use this function to restrict comparable with lower and upper values
/// - parameter range: ClosedRange representing bounds
/// - returns: Current value if it fits range, otherwise lower if value is too small or upper if value is too big
func `in`(range: ClosedRange<Self>) -> Self {
return `in`(bounds: (lower: range.lowerBound, upper: range.upperBound))
}
}