mirror of
https://github.com/gechandesu/ranges.git
synced 2026-01-02 13:59:36 +03:00
breaking,fix: remove range exclusivity support
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.
This commit is contained in:
@@ -9,14 +9,6 @@ fn test_range() {
|
||||
assert result == [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||
}
|
||||
|
||||
fn test_range_exclusive() {
|
||||
mut result := []int{}
|
||||
for i in ranges.range[int](0, 10, 1, exclusive: true) {
|
||||
result << i
|
||||
}
|
||||
assert result == [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
||||
}
|
||||
|
||||
fn test_range_negative() {
|
||||
mut result := []int{}
|
||||
for i in ranges.range[int](10, 0, -1) {
|
||||
@@ -25,14 +17,6 @@ fn test_range_negative() {
|
||||
assert result == [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
|
||||
}
|
||||
|
||||
fn test_range_negative_exclusive() {
|
||||
mut result := []int{}
|
||||
for i in ranges.range[int](10, 0, -1, exclusive: true) {
|
||||
result << i
|
||||
}
|
||||
assert result == [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
|
||||
}
|
||||
|
||||
fn test_range_with_step() {
|
||||
mut result := []int{}
|
||||
for i in ranges.range[int](0, 10, 2) {
|
||||
@@ -65,14 +49,6 @@ fn test_range_single_item() {
|
||||
assert result == [0]
|
||||
}
|
||||
|
||||
fn test_range_single_item_exclusive() {
|
||||
mut result := []int{}
|
||||
for i in ranges.range(0, 1, 1, exclusive: true) {
|
||||
result << i
|
||||
}
|
||||
assert result == [0]
|
||||
}
|
||||
|
||||
fn test_range_bigint() {
|
||||
start := big.zero_int
|
||||
end := big.integer_from_int(5)
|
||||
|
||||
Reference in New Issue
Block a user