Sometimes you need to constrain your variables. For example, all values must be within a specific range. Let's create a function newConstraint() with a closure that takes two int numbers as arguments minValue and maxValue that will serve as range delimiters, and returns an anonymous function.
The anonymous function checks if the given value is inside the defined range:
- If the value of
numis less than theminValueof the defined range, it returnsminValue; - If the value of
numis more than themaxValueof the defined range, it returnsmaxValue; - Otherwise, it returns the original value of
num.