-
Notifications
You must be signed in to change notification settings - Fork 42
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.
Also note, FluentQuery Ranges are Lazy, that is, the next value in the range is generated on request.
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, the following code results in MyList containing a reference to a TList of all even values between 100 and 200.:
MyList := Range(100, 200).Even.ToTList;
The first parameter (Start) has a default value of 0, while the second parameter has a default value of MaxInt. Therefore, this code will enumerate all Odd values between 100 and MaxInt:
for I in Range(100).Odd do
While this code will enumerate all Odd values between 0 and MaxIn:
for I in Range.Even do