Home

Awesome

range

Numeric ranges in V.

Why

Features

Installation

Usage

Use an iterator if you need a large range but don't want to allocate space in memory for all numbers in the range.

import delta456.range

mut iter := range.to_iterator(from: 0, to: 1000000, step: 2)
for v in iter {
  println(v)
}
$ v run main.v
0
2
4

Use an array when you need it and the memory allocation for all values in the range is not a problem.

import delta456.range

arr := range.to_array(from: 10, to: 0, step: -1)
println(arr)
v run main.v
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

If you prefer you can use the builder syntax.

mut iter := range.new[int]().from(0).to(10).step(1).to_iterator()

// or

arr := range.new[int]().from(0).to(10).step(1).to_array()

// or

mut iter := range.Builder[int]{ from: 0, to: 10, step: 1 }.to_iterator()

// or

arr := range.Builder[int]{ from: 0, to: 10, step: 1 }.to_array()

The builder is immutable! You can reuse it as many times as you want.

zero_to_nine := range.new[int]().from(0).to(10).step(1)

// print from 0 to 9
for v in zero_to_nine.to_iterator() {
  println(v)
}

// print from 0 to 9 again, no problems
for v in zero_to_nine.to_iterator() {
  println(v)
}

License

Released under MIT