arrays: improve `range` function

pull/3213/head
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
fn range<T>(start, end T) []T {
mut res := [start]
for i := start + 1; i < end; i++ {
pub fn range<T>(start, end T) []T {
mut res := []T
for i := start; i < end; i++ {
res << i
}
return res
}

View File

@ -1,10 +1,28 @@
import arrays
fn test_range() {
/*
TODO
a := arrays.range(1, 10)
assert a[0] == 1
println(a)
*/
start_pos := 3
end_pos := 10
arr1 := arrays.range<int>(start_pos, end_pos)
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
}