-
Notifications
You must be signed in to change notification settings - Fork 0
/
math.go
120 lines (105 loc) · 3.21 KB
/
math.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// AUTOGENERATED FILE. DO NOT EDIT.
package tplfuncs
import (
htmlTemplate "html/template"
textTemplate "text/template"
)
// MathHelpers returns a text template FuncMap with math related functions
func MathHelpers() textTemplate.FuncMap {
return textTemplate.FuncMap{
// functions working on int values
"addInt": addIntFunc,
"subtractInt": subtractIntFunc,
"subtractFromInt": subtractFromIntFunc,
"multiplyInt": multiplyIntFunc,
"divideIntBy": divideIntByFunc,
// functions working on float values
"addFloat": addFloatFunc,
"subtractFloat": subtractFloatFunc,
"subtractFromFloat": subtractFromFloatFunc,
"multiplyFloat": multiplyFloatFunc,
"divideFloatBy": divideFloatByFunc,
}
}
// MathHelpersHTML returns an HTML template FuncMap with math related functions
func MathHelpersHTML() htmlTemplate.FuncMap {
return htmlTemplate.FuncMap(MathHelpers())
}
// Doc: `addInt` adds a number of int values and returns the total sum.
func addIntFunc(inputs ...int) int {
var sum int
for _, input := range inputs {
sum += input
}
return sum
}
// Doc: `subtractInt` subtracts a number of int values from the first one and returns the remaining value.
func subtractIntFunc(start int, inputs ...int) int {
sum := start
for _, input := range inputs {
sum -= input
}
return sum
}
// Doc: `subtractFromInt` subtracts a number of int values from the last one and returns the remaining value.
func subtractFromIntFunc(inputs ...int) int {
if len(inputs) == 0 {
return 0
}
sum := inputs[len(inputs)-1]
for i := 0; i < len(inputs)-1; i++ {
sum -= inputs[i]
}
return sum
}
// Doc: `multiplyInt` multiplies a number of int values and returns the total value.
func multiplyIntFunc(inputs ...int) int {
var sum int = 1
for _, input := range inputs {
sum *= input
}
return sum
}
// Doc: `divideIntBy` divides a int value by another one. Note the inverted order to make `24 | divideBy 12` nicely expressive.
func divideIntByFunc(divisor, value int) int {
return value / divisor
}
// Doc: `addFloat` adds a number of float64 values and returns the total sum.
func addFloatFunc(inputs ...float64) float64 {
var sum float64
for _, input := range inputs {
sum += input
}
return sum
}
// Doc: `subtractFloat` subtracts a number of float64 values from the first one and returns the remaining value.
func subtractFloatFunc(start float64, inputs ...float64) float64 {
sum := start
for _, input := range inputs {
sum -= input
}
return sum
}
// Doc: `subtractFromFloat` subtracts a number of float64 values from the last one and returns the remaining value.
func subtractFromFloatFunc(inputs ...float64) float64 {
if len(inputs) == 0 {
return 0
}
sum := inputs[len(inputs)-1]
for i := 0; i < len(inputs)-1; i++ {
sum -= inputs[i]
}
return sum
}
// Doc: `multiplyFloat` multiplies a number of float64 values and returns the total value.
func multiplyFloatFunc(inputs ...float64) float64 {
var sum float64 = 1
for _, input := range inputs {
sum *= input
}
return sum
}
// Doc: `divideFloatBy` divides a float64 value by another one. Note the inverted order to make `24 | divideBy 12` nicely expressive.
func divideFloatByFunc(divisor, value float64) float64 {
return value / divisor
}