-
Notifications
You must be signed in to change notification settings - Fork 5
/
range.v
77 lines (66 loc) · 1.05 KB
/
range.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
module range
struct Range[T] {
mut:
limit T
step T
curr T
}
fn (mut t Range[T]) next[T]() ?T {
if (t.step > 0 && t.curr >= t.limit) || (t.step < 0 && t.curr <= t.limit) {
return none
}
res := t.curr
t.curr += t.step
return res
}
pub struct Builder[T] {
pub:
from T
to T
step T
}
pub fn (t Builder[T]) from(v T) Builder[T] {
return Builder[T]{
from: v
to: t.to
step: t.step
}
}
pub fn (t Builder[T]) to(v T) Builder[T] {
return Builder[T]{
from: t.from
to: v
step: t.step
}
}
pub fn (t Builder[T]) step(v T) Builder[T] {
return Builder[T]{
from: t.from
to: t.to
step: v
}
}
pub fn (t Builder[T]) to_iterator() Range[T] {
return Range[T]{
limit: t.to
step: t.step
curr: t.from
}
}
pub fn (t Builder[T]) to_array() []T {
mut res := []T{}
mut it := t.to_iterator()
for v in it {
res << v
}
return res
}
pub fn new[T]() Builder[T] {
return Builder[T]{}
}
pub fn to_array[T](builder Builder[T]) []T {
return builder.to_array()
}
pub fn to_iterator[T](builder Builder[T]) Range[T] {
return builder.to_iterator()
}