commit 64106aab37d2ec0151ef87b83cbaa1dd2481d178
parent 0e082d47a6552b29857ab3fd1e54e60a73ebe61f
Author: bsandro <email@bsandro.tech>
Date: Tue, 28 Jun 2022 21:59:02 +0300
len() support for arrays
Diffstat:
2 files changed, 3 insertions(+), 0 deletions(-)
diff --git a/eval/builtins.go b/eval/builtins.go
@@ -11,6 +11,8 @@ var builtins = map[string]*object.Builtin{
switch arg := args[0].(type) {
case *object.String:
return &object.Integer{Value: int64(len(arg.Value))}
+ case *object.Array:
+ return &object.Integer{Value: int64(len(arg.Elements))}
default:
return newError("unsupported `len` argument, got %s", args[0].Type())
}
diff --git a/eval/eval_test.go b/eval/eval_test.go
@@ -263,6 +263,7 @@ func TestBuiltinFunctions(t *testing.T) {
{`len("four")`, 4},
{`len(1)`, "unsupported `len` argument, got INTEGER"},
{`len("one", "two")`, "wrong number of arguments (2 instead of 1)"},
+ {`len([1,2,3,4,5])`, 5},
}
for _, tt := range tests {