forked from clipperhouse/slice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsum.go
34 lines (31 loc) · 888 Bytes
/
sum.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
package sliceP
import "github.com/clipperhouse/typewriter"
var sum = &typewriter.Template{
Name: "Sum",
Text: `
// Sum sums {{.Type}} elements in {{.SliceName}}. See: http://clipperhouse.github.io/gen/#Sum
func (rcv {{.SliceName}}) Sum() (result {{.Type}}) {
for _, v := range rcv {
result += v
}
return
}
`,
TypeConstraint: typewriter.Constraint{Numeric: true},
}
var sumT = &typewriter.Template{
Name: "Sum",
Text: `
// Sum{{.TypeParameter.LongName}} sums {{.Type}} over elements in {{.SliceName}}. See: http://clipperhouse.github.io/gen/#Sum
func (rcv {{.SliceName}}) Sum{{.TypeParameter.LongName}}(fn func({{.Type}}) {{.TypeParameter}}) (result {{.TypeParameter}}) {
for _, v := range rcv {
result += fn(v)
}
return
}
`,
TypeParameterConstraints: []typewriter.Constraint{
// exactly one type parameter is required, and it must be numeric
{Numeric: true},
},
}