Files
monkey/internal/builtins/max.go
Charles Smith aebbe43999
Some checks failed
Build / build (push) Successful in 10m25s
Publish Image / publish (push) Failing after 39s
Test / build (push) Successful in 11m19s
Fix VM memory allocation optimizations by reducing what we allocate on the heap
2024-03-31 20:44:50 -04:00

32 lines
667 B
Go

package builtins
import (
"monkey/internal/object"
"monkey/internal/typing"
"sort"
)
// Max ...
func Max(args ...object.Object) object.Object {
if err := typing.Check(
"max", args,
typing.ExactArgs(1),
typing.WithTypes(object.ArrayType),
); 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=%d", n, e.Type())
}
}
sort.Ints(xs)
return object.Integer{Value: int64(xs[len(xs)-1])}
}