-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path34.go
69 lines (65 loc) · 1.28 KB
/
34.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
import (
"fmt"
"math"
)
func main() {
s := []int{0, 2, 4, 6, 8}
print_values[int](s)
print_values[int](func(i int) (v int, ok bool) {
if ok = (-1 < i) && (i < len(s)); ok {
v = s[i]
}
return
})
print_values[int](func(g func(i, v int) bool) {
for i, v := range s {
if !g(i, v) {
return
}
}
})
print_values[int](Sequence[int](s))
print_values[int](Generator[int](func(i int) (v int, ok bool) {
if ok = (-1 < i) && (i < len(s)); ok {
v = s[i]
}
return
}))
print_values[int](Iterator[int](func(g func(i, v int) bool) {
for i, v := range s {
if !g(i, v) {
return
}
}
}))
}
func print_values[T any](s any) {
switch s := s.(type) {
case Sequence[T]:
print_values[T]([]T(s))
case []T:
for i, v := range s {
fmt.Printf("1>%v: %v\n", i, v)
}
case Generator[T]:
print_values[T]((func(int) (T, bool))(s))
case func(int) (T, bool):
for i := range math.MaxInt {
if v, ok := s(i); ok {
fmt.Printf("2>%v: %v\n", i, v)
} else {
return
}
}
case Iterator[T]:
print_values[T]((func(func(int, T) bool))(s))
case func(func(int, T) bool):
for i, v := range s {
fmt.Printf("3>%v: %v\n", i, v)
}
}
}
type Sequence[T any] []T
type Generator[T any] func(int) (T, bool)
type Iterator[T any] func(func(int, T) bool)