-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path63.go
52 lines (45 loc) · 842 Bytes
/
63.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"
"os"
"strconv"
)
func main() {
a := MakeAccumulator[int]()
for i, v := range os.Args[1:] {
x, _ := strconv.Atoi(v)
a = a.Add(x).Add(MakeAccumulator(i))
}
os.Exit(a.Int())
}
type Scalar interface {
int | int8 | int16 | int32 | int64 |
uint | uint8 | uint16 | uint32 | uint64 |
float32 | float64
}
type Accumulator[T Scalar] func(T) T
func MakeAccumulator[T Scalar](s ...T) (a Accumulator[T]) {
var y T
a = func(x T) T {
y += x
return y
}
for _, v := range s {
a.Add(v)
}
return
}
func (a Accumulator[T]) Int() int {
return int(a(0))
}
func (a Accumulator[T]) Add(x any) Accumulator[T] {
switch x := x.(type) {
case T:
a(x)
fmt.Printf("1>a + x == a + %v == %v\n", x, a(0))
case Accumulator[T]:
a(x(0))
fmt.Printf("2>a + x == a + %v == %v\n", x(0), a(0))
}
return a
}