Files
monkey/builtins/min.go
Chuck Smith 6d234099d1
Some checks failed
Build / build (push) Successful in 11m16s
Test / build (push) Failing after 17m0s
type checking and error handling for builtins improved.
2024-03-25 16:18:08 -04:00

32 lines
633 B
Go

package builtins
import (
"monkey/object"
"monkey/typing"
"sort"
)
// Min ...
func Min(args ...object.Object) object.Object {
if err := typing.Check(
"min", args,
typing.ExactArgs(1),
typing.WithTypes(object.ARRAY_OBJ),
); err != nil {
return newError(err.Error())
}
a := args[0].(*object.Array)
// 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])}
}