CompareValue function is incredibly practical when you are writing comparers (functions that determine how some data structure is ordered). System.Math and System.StrUtils define a bunch of functions that can be used to compare integers, doubles, strings … There’s, however, no CompareValue for booleans.
A CompareValue function compares two parameters, traditionally named left and right, and returns 0 if they are the same, –1 if left is smaller and 1 if right is smaller.
If we use the usual ordering of false < true, we can write the missing function as follows:
function CompareValue(left, right: boolean): integer; overload; begin if left < right then Result := -1 else if left > right then Result := 1 else Result := 0; end;
Your task for today – if you choose to accept it – is: Write this function without any if statements.