Add tons of builtin helpers and array operations.
Some checks failed
Test / build (push) Waiting to run
Build / build (push) Has been cancelled

This commit is contained in:
Chuck Smith
2024-03-24 12:11:46 -04:00
parent c9d96236dd
commit 99cea83f57
23 changed files with 413 additions and 49 deletions

27
object/builtin_join.go Normal file
View File

@@ -0,0 +1,27 @@
package object
import (
"strings"
)
// Join ...
func Join(args ...Object) Object {
if len(args) != 2 {
return newError("wrong number of arguments. got=%d, want=1",
len(args))
}
if arr, ok := args[0].(*Array); ok {
if sep, ok := args[1].(*String); ok {
a := make([]string, len(arr.Elements))
for i, el := range arr.Elements {
a[i] = el.String()
}
return &String{Value: strings.Join(a, sep.Value)}
} else {
return newError("expected arg #2 to be `str` got got=%T", args[1])
}
} else {
return newError("expected arg #1 to be `array` got got=%T", args[0])
}
}