Files
monkey/builtins/min.go
Chuck Smith fea9fb9f64
Some checks failed
Build / build (push) Successful in 14m31s
Test / build (push) Failing after 17m13s
array builtins
2024-03-24 16:29:18 -04:00

30 lines
679 B
Go

package builtins
import (
"monkey/object"
"sort"
)
// Min ...
func Min(args ...object.Object) object.Object {
if len(args) != 1 {
return newError("wrong number of arguments. got=%d, want=1",
len(args))
}
if a, ok := args[0].(*object.Array); ok {
// TODO: Make this more generic
xs := make([]int, len(a.Elements))
for n, e := range a.Elements {
if i, ok := e.(*object.Integer); ok {
xs = append(xs, int(i.Value))
} else {
return newError("item #%d not an `int` got=%s", n, e.Type())
}
}
sort.Ints(xs)
return &object.Integer{Value: int64(xs[0])}
}
return newError("argument #1 to `min` expected to be `array` got=%T", args[0].Type())
}