Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/Delta456/range
Functionality of Python's range() in V
https://github.com/Delta456/range
array python range vlang
Last synced: about 1 month ago
JSON representation
Functionality of Python's range() in V
- Host: GitHub
- URL: https://github.com/Delta456/range
- Owner: Delta456
- License: mit
- Created: 2020-05-26T07:33:30.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2023-03-19T15:40:22.000Z (almost 2 years ago)
- Last Synced: 2024-11-15T19:56:19.756Z (about 1 month ago)
- Topics: array, python, range, vlang
- Language: V
- Homepage:
- Size: 17.6 KB
- Stars: 33
- Watchers: 2
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-v - range - Functionality of Python's range() in V. (Libraries / Utility)
README
# range
Numeric ranges in V.
## Why
- `a..b` in V can only be in increasing order and not in negative order.
- Lacks inbuilt `step` which most people need or want.
- No support for `float` type.
- Solution for [vlang/v#5944](https://github.com/vlang/v/issues/5944).## Features
- Make `range` easily
- Make ranges for `int` and `f32`
- Positive as well as negative support!
- No need to write the whole for loop! (*this maybe slower than the normal one*)
- Use `range` for functional programming
- Support iterators. Long ranges without high memory allocation.
- Half open-open ranges `[from,to]`## Installation
- Via `git clone`
- `git clone https://github.com/Delta456/range`
- Via `v install`
- `v install Delta456.range`
- Via `vpkg`
- `vpkg 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.
```v
import delta456.rangemut 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.
```v
import delta456.rangearr := 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.
```v
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.
```v
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](LICENSE)