Scala Comparators

A few new concepts are needed for this SKB.

First, what is aBoolean? ABooleancan only take two values:trueorfalse. It is the result of a mathematical question. For instance, is 2 less than 5 (2 < 5), it would returns "yes" (true).

And from previous SKB, you might have noticed thatasserttests if what is inside istrue.

In Scala, and in the majority of other programming languages, you have a series of built-in comparators for mathematics:

  • ==: for equality. Some tips here:
    • Notice that there are two==. With only one=, it would be an assignation (Like in:val a = 2).
    • In your projects as a software engineer, always be careful about comparing numbers with decimals (Like1.5632426546, they are calledDoubleorFloat) because the computer will round those numbers. So instead ofa == b, you should be doing something like:Math.abs(a - b) < 0.01.
  • !=: Not equal
  • >: Greater than
  • <: Less than
  • >=: Greater than or equal
  • <=: Less than or equal

Reveal more information and clues
Load Exercise