4 Commits

Author SHA1 Message Date
ge
39314b474b use struct update syntax in with_step() 2026-01-11 08:41:46 +03:00
ge
06f85c48d3 mod: bump version 2026-01-11 03:13:54 +03:00
ge
74e92c77ab feat: add with_step(), reset() 2026-01-11 03:13:01 +03:00
ge
b51f74a877 readme: fix 2025-12-30 15:43:51 +03:00
4 changed files with 56 additions and 6 deletions

View File

@@ -3,7 +3,7 @@
The `ranges` module provides tools for creating ranges of numbers.
Ranges are represented by the generic `Range` iterator, which has start and
end points, and a step size.
end points and a step size.
```v
import ranges

View File

@@ -4,7 +4,8 @@ import strconv
import math.big
struct Range[T] {
limit T
start T
end T
step T
is_neg bool
mut:
@@ -13,7 +14,7 @@ mut:
// next returns the new element from range or none if range end is reached.
pub fn (mut r Range[T]) next() ?T {
if (r.is_neg && r.cur < r.limit) || (!r.is_neg && r.cur > r.limit) {
if (r.is_neg && r.cur < r.end) || (!r.is_neg && r.cur > r.end) {
return none
}
defer {
@@ -22,6 +23,21 @@ pub fn (mut r Range[T]) next() ?T {
return r.cur
}
// reset resets the internal iterator state to its initial value, after which the iterator can be reused.
// Note: `for i in iter {` does not modify the internal iterator state, but direct `next()` call does.
pub fn (mut r Range[T]) reset() {
r.cur = r.start
}
// with_step returns copy of the range with new step value.
pub fn (r Range[T]) with_step[T](step T) Range[T] {
return Range[T]{
...r
step: step
cur: r.start
}
}
// range creates new Range iterator with given start, end and step values.
//
// Generally numbers are expected. If type is a struct the following operators
@@ -33,7 +49,8 @@ pub fn (mut r Range[T]) next() ?T {
// Note: Zero step value will cause an infitite loop!
pub fn range[T](start T, end T, step T) Range[T] {
return Range[T]{
limit: end
start: start
end: end
step: step
cur: start
is_neg: start > end
@@ -52,7 +69,7 @@ pub:
// Use from_string_custom if you want to use custom type with special string
// convertion rules.
//
// Supported string formats are `start-end`, `start[:step]end`. start and end
// Supported string formats are `start-end` and `start[:step]end`. start and end
// values are sepatared by 'sep' which is hypen (`-`) by default. Single number
// will be interpreted as range of one element. Several ranges can be specified
// in a line, separated by 'group_sep' (comma by default). 'sep' and 'group_sep'

View File

@@ -145,3 +145,36 @@ fn test_range_from_string_custom_type() {
sep: '..'
)! == [ranges.range[Int](Int{0}, Int{10}, Int{1})]
}
fn test_range_reset() {
mut result := []int{}
mut iter := ranges.range(0, 5, 1)
for {
if elem := iter.next() {
result << elem
} else {
break
}
}
assert result == [0, 1, 2, 3, 4, 5]
iter.reset()
result = []int{}
for i in iter {
result << i
}
assert result == [0, 1, 2, 3, 4, 5]
}
fn test_range_new_with_step() {
mut result := []int{}
mut iter := ranges.range(0, 5, 1)
for i in iter.with_step(2) {
result << i
}
assert result == [0, 2, 4]
}

2
v.mod
View File

@@ -1,7 +1,7 @@
Module {
name: 'ranges'
description: 'Operating with ranges of numbers'
version: '0.2.0'
version: '0.3.0'
license: 'Unlicense'
dependencies: []
}