-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path49.go
52 lines (43 loc) · 744 Bytes
/
49.go
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
package main
import "fmt"
func main() {
s := &Range[int]{0, 2, Limit(5)}
print_values(s)
print_values(s)
}
func print_values[T any](s Iterable[T]) (i int) {
s.Each(func(v T) {
fmt.Printf("%v: %v\n", i, v)
i++
})
return i
}
type Iterable[T any] interface {
Each(func(T))
}
type Ordinal interface {
int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64
}
type Range[T Ordinal] struct {
start T
step T
Limit
}
func (r Range[T]) Each(f func(T)) {
if r.step == 1 {
for i := range int(r.Limit - Limit(r.start)) {
f(T(i))
}
} else {
r.Limit.Each(func(i int) {
f(r.start)
r.start += r.step
})
}
}
type Limit int
func (i Limit) Each(f func(int)) {
for v := range i {
f(int(v))
}
}