-
Notifications
You must be signed in to change notification settings - Fork 43
Integer Ranges
Sometimes you do not want to query a separate collection of Integers, but rather want to query a range of values.
For example, if you want to enumerate all Even numbered values between 100 and 200, you can do something like this:
for I in Range(100, 200).Even do
Note, there was no need to call Query or From, as the Range method returns a Bound Integer Query.
If you want to enumerate the same values but in reverse, just swap the values:
for I in Range(200, 100).Even do
These can of course be used with Terminating Integer operations, for example:
I := Range(100, 200).Even.Sum;
Gives the Sum of all even values between 100 and 200. Likewise:
MyList := Range(100, 200).Even.ToTList;
Results in MyList containing a reference to a TList of all even values between 100 and 200.
The first parameter (Start) has a default value of 0, while the second parameter has a default value of MaxInt. Therefore:
for I in Range(100).Odd do
will enumerate all Odd values between 100 and MaxInt, while:
for I in Range.Even do
will enumerate all Odd values between 0 and MaxInt.