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
</span right span style="color: rgb(0, 128, 0); font-weight: bold;"then/spanbr span style="color: rgb(0, 128, 0);"Result/span span style="color: rgb(102, 102, 102);":=/span span style="color: rgb(102, 102, 102);"-1/spanbr span style="color: rgb(0, 128, 0); font-weight: bold;"else/span span style="color: rgb(0, 128, 0); font-weight: bold;"if/span left span style="color: rgb(102, 102, 102);"> 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.
Read more »[SIZE=-2]--- Published under the
Creative Commons Attribution 3.0 license[/SIZE]
Weiterlesen...