-
Notifications
You must be signed in to change notification settings - Fork 1
/
min.go
43 lines (36 loc) · 780 Bytes
/
min.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
package arithmetic
import (
"errors"
"fmt"
)
func init() {
RegisterFunction("min", min)
}
// min is a function that returns the lowest of the provided inputs.
func min(args ...interface{}) (interface{}, error) {
if len(args) == 0 {
return nil, errors.New("min requires at least one argument")
}
// Ensure each argument is a float, or a "variable" float.
var m, f float64
var def bool
for _, a := range args {
switch t := a.(type) {
case float64:
f = t
case variable:
v, ok := t.value.(float64)
if !ok {
return nil, fmt.Errorf("min requires numeric arguments, %s given", t)
}
f = v
default:
return nil, fmt.Errorf("min requires numeric arguments, %v given", a)
}
if f < m || !def {
def = true
m = f
}
}
return m, nil
}