array builtins
This commit is contained in:
21
builtins/sorted.go
Normal file
21
builtins/sorted.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package builtins
|
||||
|
||||
import (
|
||||
"monkey/object"
|
||||
"sort"
|
||||
)
|
||||
|
||||
// Sorted ...
|
||||
func Sorted(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 {
|
||||
newArray := a.Copy()
|
||||
sort.Sort(newArray)
|
||||
return newArray
|
||||
}
|
||||
return newError("argument #1 to `sorted` expected to be `array` got=%T", args[0].Type())
|
||||
}
|
||||
Reference in New Issue
Block a user