Skip to content

Instantly share code, notes, and snippets.

@NoahPeeters
Last active July 3, 2017 22:27
Show Gist options
  • Save NoahPeeters/479156cf1a6f67c3cd63b2dfa8b396f3 to your computer and use it in GitHub Desktop.
Save NoahPeeters/479156cf1a6f67c3cd63b2dfa8b396f3 to your computer and use it in GitHub Desktop.
Restrict the value of any comparable type using easy-to-use functions
/// Returns value if it's contained in range; otherwise the returned value is limited to the range's bounds.
///
/// - Parameters:
/// - value: The value to restrict.
/// - range: The range used to restict the value.
/// - Returns: The resticted value.
func restrict<T>(_ value: T, between range: ClosedRange<T>) -> T {
return min(range.upperBound, max(range.lowerBound, value))
}
/// Returns value if it's not contained in range; otherwise the default value is returned.
///
/// - Parameters:
/// - value: The value to restrict.
/// - range: The range used to restict the value.
/// - defaultValue: The default value used if the value is not contained in the range.
/// - Returns: The resticted value.
func restrict<T>(_ value: T, except range: Range<T>, withDefaultValue defaultValue: T) -> T {
return range.contains(value) ? defaultValue : value
}
/// Returns value if it's not contained in range; otherwise the default value is returned.
///
/// - Parameters:
/// - value: The value to restrict.
/// - range: The range used to restict the value.
/// - defaultValue: The default value used if the value is not contained in the range.
/// - Returns: The resticted value.
func restrict<T>(_ value: T, except range: ClosedRange<T>, withDefaultValue defaultValue: T) -> T {
return range.contains(value) ? defaultValue : value
}
/// Returns value if it is equal or bigger than `minimumValue`; otherwise returns `minimumValue`.
///
/// - Parameters:
/// - value: The value to restrict.
/// - minimumValue: The minimum value.
/// - Returns: The resticted value.
func restrict<T: Comparable>(_ value: T, withMinimumValue minimumValue: T) -> T {
return max(value, minimumValue)
}
/// Returns value if it is equal or smaller than `maximumValue`; otherwise returns `maximumValue`.
///
/// - Parameters:
/// - value: The value to restrict.
/// - maximumValue: The maximum value.
/// - Returns: The resticted value.
func restrict<T: Comparable>(_ value: T, withMaximumValue maximumValue: T) -> T {
return min(value, maximumValue)
}
/// Returns value if it's contained in allowed values; otherwise the default value is returned.
///
/// - Parameters:
/// - value: The value to restrict.
/// - possibleValues: A list of allowed values.
/// - defaultValue: The default value used if the value is not contained in the allowed values.
/// - Returns: The resticted value.
func restrict<T: Comparable>(_ value: T, toValues allowedValues: [T], withDefaultValue defaultValue: T) -> T {
return allowedValues.index(of: value) == nil ? defaultValue : value
}
restrict(5, between: 0...5) // return 5
restrict(5, except: 0..<5, withDefaultValue: 6) // return 5
restrict(5, withMaximumValue: 4) // return 4
restrict(-8, toValues: [10, 5], withDefaultValue: 6) // returns 6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment