-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.go
100 lines (82 loc) · 1.6 KB
/
generate.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package main
import (
"bufio"
"os"
"strconv"
"strings"
"math"
)
const (
rootFolder = "./break/"
start = 1000
nMultiplier = 2
nStep =10
)
func main() {
if err := os.MkdirAll(rootFolder, 0777); err != nil {
panic(err)
}
for i := 1; i <= nStep; i++ {
nn := start * int((math.Pow(nMultiplier, float64(i))))
pkgName := "drink"+strconv.Itoa(nn)+"cups"
pkgName = strings.Replace(pkgName, "000000cups", "Mcups", -1)
pkgName = strings.Replace(pkgName, "000cups", "Kcups", -1)
if err := os.MkdirAll(rootFolder + pkgName, 0777); err != nil {
panic(err)
}
generateGenerics(rootFolder, pkgName, nn)
}
}
func generateGenerics(root, pkgName string, nn int) {
s := "package "+pkgName+"\n\n"
s += `
type number interface {
~int | ~int32 | ~int64 | ~float32 | ~float64 | ~uint
}
`
generate(root+pkgName+"/generics.go", s, nn,
func(i int) string {
return `
func MaxOfType` + strconv.Itoa(i) + `[T number](a, b T) T {
if a > b {
return a
}
return b
}
`
})
}
func generate(fileName, beginning string, nn int, f func(int) string) {
file, err := os.Create(fileName)
if err != nil {
panic(err)
}
defer file.Close()
writer := bufio.NewWriter(file)
_, err = writer.WriteString(beginning)
if err != nil {
panic(err)
}
defer writer.Flush()
for i := 0; i < nn; i++ {
_, err = writer.WriteString(f(i))
if err != nil {
panic(err)
}
}
}
type xhf string
func (s xhf) String() string {
return ""
}
type Stringer interface {
String() string
xhf
}
func Tos[T Stringer](s []T) []string {
var ret []string
for _, v := range s {
ret = append(ret, v.String())
}
return ret
}