-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path036_reduce.go
42 lines (38 loc) · 924 Bytes
/
036_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
package main
import (
"fmt"
R "reflect"
)
func Reduce(s any, f func(any, any) any) (r any) {
if s := R.ValueOf(s); s.Kind() == R.Slice {
T := s.Type().Elem()
V := R.New(T)
E := V.Elem()
switch T.Kind() {
case R.Int, R.Int8, R.Int16, R.Int32, R.Int64:
for i := 0; i < s.Len(); i++ {
v := R.ValueOf(f(E.Interface(), s.Index(i).Interface()))
E.SetInt(v.Int())
}
case R.Float32, R.Float64:
for i := 0; i < s.Len(); i++ {
v := R.ValueOf(f(E.Interface(), s.Index(i).Interface()))
E.SetFloat(v.Float())
}
}
r = E.Interface()
}
return
}
func main() {
is := []int{0, 1, 2, 3, 4}
ir := Reduce(is, func(x any, v any) any {
return x.(int) + v.(int)
})
fmt.Printf("Reduce(%v, f()) = %v [%T]\n", is, ir, ir)
fs := []float32{0, 1, 2, 3, 4}
fr := Reduce(fs, func(x any, v any) any {
return x.(float32) + v.(float32)
})
fmt.Printf("Reduce(%v, f()) = %v [%T]\n", fs, fr, fr)
}