Files
monkey/internal/builtins/min.go
Chuck Smith fc6ceee02c
Some checks failed
Build / build (push) Failing after 5m21s
Publish Image / publish (push) Failing after 32s
Test / build (push) Failing after 5m8s
restructure project
2024-03-28 16:20:09 -04:00

32 lines
661 B
Go

package builtins
import (
"monkey/internal/object"
"monkey/internal/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 := []int{} //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])}
}