Files
monkey/internal/builtins/max.go
Chuck Smith 12d43c9835
Some checks failed
Publish Image / publish (push) Waiting to run
Test / build (push) Waiting to run
Build / build (push) Has been cancelled
Refactor Type to be an int
2024-03-29 11:01:15 -04:00

32 lines
669 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])}
}