One of the first things we learn when checking a variable is within a given range is that we can't use this form: 0 < n < 100. Mainly due to the order of operands. Since (0 < n) computes into a boolean value which incidentally (at least in Java) cannot be compared with an integer. Although, if you are using C it would compile since it treats booleans as 0 or 1. However, the second part of the test would make the whole expresion true all the time.

The way we tend to write this in code is with something in the line of this: (n > 0 && n < 100) which makes sense but we can do better. I think this notation is clearer:

(0 < n && n < 100)

Although, hardly diverging from the former, here is easier to recognize the content of the test by leaving the subject in the middle. It's a closer resemblance with it's mathematical analogous.

In scala we can do more magic like this:

class IntChecker(n: Long) {

def within(range: Range): Boolean = range contains n

}

implicit def int2Checker(value: Int) : IntChecker = return new IntChecker(value)

n within (1 until 100)

Scala casts our integer value into IntChecker when it realizes that we are calling the method *within*.

Or you can directly use this:

(1 until 100) contains n

It is possible to do a more natural expression with scala but I will cover that on part 2.