-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path048_reduce.go
78 lines (73 loc) · 1.29 KB
/
048_reduce.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
70
71
72
73
74
75
76
77
78
package main
import (
"fmt"
R "reflect"
)
type Numeric interface {
~int | ~int8 | ~int16 | ~int32 | ~int64 |
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 |
~float32 | ~float64
}
type Iterable[T Numeric] interface {
Range(f func(int, T))
}
type Reducible[T Numeric] interface {
Reduce(func(T, T) T) T
}
func Reduce[T Numeric](c any, f func(T, T) T) (r T) {
switch c := c.(type) {
case Iterable[T]:
c.Range(func(i int, v T) {
r = f(r, v)
})
case Reducible[T]:
r = c.Reduce(f)
case string:
for _, x := range c {
r = f(r, T(x))
}
case T:
r = f(r, c)
case []T:
for _, x := range c {
r = f(r, x)
}
case map[int]T:
for _, x := range c {
r = f(r, x)
}
case func(int) (T, bool):
for i := 0; ; i++ {
if v, ok := c(i); ok {
r = f(r, v)
} else {
break
}
}
case chan T:
for x := range c {
r = f(r, x)
}
default:
switch c := R.ValueOf(c); c.Kind() {
case R.Map:
for i := c.MapRange(); i.Next(); {
r = f(r, i.Value().Interface().(T))
}
case R.Array:
for i := 0; i < c.Len(); i++ {
r = f(r, c.Index(i).Interface().(T))
}
case R.Func:
for i := 0; ; i++ {
p := []R.Value{R.ValueOf(i)}
if p = c.Call(p); p[1].Interface() == true {
r = f(r, p[0].Interface().(T))
} else {
break
}
}
}
}
return
}