![]() |
CompareValue for booleans
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. ![]() ![]() ![]() |
Alle Zeitangaben in WEZ +1. Es ist jetzt 10:59 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz