1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

arrays: improve range function

This commit is contained in:
Alexey 2019-12-25 15:52:27 +03:00 committed by Alexander Medvednikov
parent f2bab4ab9a
commit ccb041c831
2 changed files with 30 additions and 13 deletions

View File

@ -1,10 +1,9 @@
module arrays module arrays
fn range<T>(start, end T) []T { pub fn range<T>(start, end T) []T {
mut res := [start] mut res := []T
for i := start + 1; i < end; i++ { for i := start; i < end; i++ {
res << i res << i
} }
return res return res
} }

View File

@ -1,10 +1,28 @@
import arrays import arrays
fn test_range() { fn test_range() {
/* start_pos := 3
TODO end_pos := 10
a := arrays.range(1, 10)
assert a[0] == 1 arr1 := arrays.range<int>(start_pos, end_pos)
println(a) assert arr1.len == end_pos - start_pos
*/ for i, c in arr1 {
} assert c == i + start_pos
}
arr2 := arrays.range<f32>(start_pos, end_pos)
assert arr2.len == end_pos - start_pos
for i, c in arr2 {
assert c == f32(i + start_pos)
}
arr3 := arrays.range<int>(start_pos, start_pos - 1)
assert arr3.len == 0
arr4 := arrays.range<int>(start_pos, start_pos)
assert arr4.len == 0
arr5 := arrays.range<int>(start_pos, start_pos + 1)
assert arr5.len == 1
assert arr5[0] == start_pos
}