mirror of
https://github.com/gechandesu/ranges.git
synced 2026-01-02 13:59:36 +03:00
4306b2220cdb94d14deef83cad66530f48c14b36
The usefulness of this feature is questionable, and its correct implementation is difficult due to the use of generics. The implementation worked incorrectly in cases where the iterator step is not equal to one and is not a multiple of the end element. For example, for `range(0, 7, 4)`, the result is `[0]` instead of `[0, 4]`. After this commit range end value is always included. To check for multiples, a user-defined type must also implement an overload of the remainder operator (`%`), even if the exclusivity function is not needed. Another correct implementation requires subtracting one from the end element for integers and the minimum fractional part supported by the type for floats. This cannot be done correctly for generics, as it requires casting the literal to a specific type, and we cannot cast number to any type.
Operating with Ranges of Numbers
The ranges module provides tools for creating ranges of numbers.
Ranges are represented by the generic Range iterator, which has start and
end points, a step size, and an inclusive/exclusive flag.
import ranges
// Iterate from 0 to 5 with step 2. Negative values also supported.
for i in ranges.range(0, 5, 2) {
println(i)
}
// 0
// 2
// 4
See more usage examples in ranges_test.v.
Description
Languages
V
100%