range
Functionality of Python's range() in V.
range
Numeric ranges in V.
Why
-
a..bin V can only be in increasing order and not in negative order. - Lacks inbuilt
stepwhich most people need or want. - No support for
floattype. - Solution for vlang/v#5944.
Features
- Make
rangeeasily - Make ranges for
intandf32 - Positive as well as negative support!
- No need to write the whole for loop! (this maybe slower than the normal one)
- Use
rangefor functional programming - Support iterators. Long ranges without high memory allocation.
- Half open-open ranges
[from,to]
Installation
- Via
git clonegit clone https://github.com/Delta456/range
- Via
v installv install Delta456.range
- Via
vpkgvpkg install range
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…Why
-
a..bin V can only be in increasing order and not in negative order. - Lacks inbuilt
stepwhich most people need or want. - No support for
float.
Features
- Make
rangearrays easily - Make ranges for
intandf32 - Positive as well as Negative Support!
- No need to write the whole for loop! (this maybe slower than the normal one)
- Use
rangefor functional programming - Full Python's
range()functionality
Installation
- Via
git clonegit clone https://github.com/Delta456/range
- Via
v installv install range
- Via
vpkgvpkg install range
Usage
-
range.int(start:0, stop:value, step:1)
makes a range ofintwith the following parameters:-
start:startvalue of the range by default it's 0 -
stop:stopvalue of the range -
step:stepvalue of the range by default it's 1
-
-
range.float(start:0.0, stop:value, step:1.0)makes a range off32with the following parameters:-
start:startvalue of the range by default it's 0.0 -
stop:stopvalue of the range -
step:stepvalue of the range by default it's 1.0
-
Note: If range.int(step:0) or range.float(step:0) then an error will be raised because step cannot be zero.
In main.v
import delta456.range
fn main() {
// using range.int
for i in range.int(stop:10) {
println(i)
}
for i in range.int(start:1, stop:10) {
println(i)
}
for i in range.int(start:1, stop:10, step:2) {
println(i)
}
for i in range.int(start:10, stop:1, step:-1) {
println(i)
}
for i in range.int(start:-5, stop:-1, step: 1) {
println(i)
}
// using range.float
for i in in range.float(stop:10) {
println(i)
}
for i in in range.float(stop:10, step:0.2) {
println(i)
}
for i in in range.float(start:0.1, stop:10) {
println(i)
}
for i in in range.float(start:10, stop:1.0, step:-0.2) {
println(i)
}
for i in in range.float(start:-10, stop:-1.0, step:0.2) {
println(i)
}
}
Acknowledgments
I thank @Hungry Blue Dev for giving me the idea of using a struct with default parameters so that range function for each type can be the same.
License
Released under MIT
Top comments (0)