-
Notifications
You must be signed in to change notification settings - Fork 1
/
040_reduce.go
52 lines (46 loc) · 910 Bytes
/
040_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
package main
import "fmt"
type Numeric interface {
~int | ~float32
}
func Reduce[T Numeric](c any, f func(T, T) T) (r T) {
switch c := c.(type) {
case []T:
for _, x := range c {
r = f(r, x)
}
case map[int]T:
for _, x := range c {
r = f(r, x)
}
case chan T:
for x := range c {
r = f(r, x)
}
}
return
}
func Pump[T Numeric](ic chan<- T, v ...T) {
for _, v := range v {
ic <- v
}
close(ic)
}
func main() {
is := []int{0, 1, 2, 3, 4}
ir := Reduce(is, func(x, v int) int {
return x + v
})
fmt.Printf("Reduce(%v, f()) = %v [%T]\n", is, ir, ir)
im := map[int]int{0: 0, 1: 1, 2: 2, 3: 3, 4: 4}
ir = Reduce(im, func(x, v int) int {
return x + v
})
fmt.Printf("Reduce(%v, f()) = %v [%T]\n", im, ir, ir)
ic := make(chan int)
go Pump(ic, 0, 1, 2, 3, 4)
ir = Reduce(ic, func(x, v int) int {
return x + v
})
fmt.Printf("(chan ic).Reduce(f()) = %v [%T]\n", ir, ir)
}