clamp<T> function
- Ord<
T> O
Returns a function that clamps a value within a given range.
Example:
void main() {
// Create an Ord instance for integers
Ord<int> ordInt = fromCompare((x, y) => x.compareTo(y));
// Create a clamp function to limit values between 1 and 10 (inclusive)
int Function(int) clampFn = clamp(ordInt)(1, 10);
// Clamp a value that is outside the range
int clampedValue = clampFn(15); // Output: 10 (Value is clamped to 10)
}
Implementation
T Function(T) Function(T low, T hi) clamp<T>(Ord<T> O) {
return (T low, T high) => (T a) {
if (O.compare(a, low) < 0) return low;
if (O.compare(a, high) > 0) return high;
return a;
};
}