Simplified Equality
Some checks failed
Build / build (push) Successful in 10m48s
Test / build (push) Failing after 17m11s

This commit is contained in:
Chuck Smith
2024-03-24 17:11:48 -04:00
parent fea9fb9f64
commit 1c99d2198b
11 changed files with 109 additions and 133 deletions

View File

@@ -1,6 +1,9 @@
package builtins
import "monkey/object"
import (
"monkey/object"
"sort"
)
import (
"strings"
@@ -21,15 +24,14 @@ func Find(args ...object.Object) object.Object {
return newError("expected arg #2 to be `str` got got=%T", args[1])
}
} else if haystack, ok := args[0].(*object.Array); ok {
needle := args[1]
index := -1
for i, el := range haystack.Elements {
if cmp, ok := el.(object.Comparable); ok && cmp.Equal(needle) {
index = i
break
}
needle := args[1].(object.Comparable)
i := sort.Search(len(haystack.Elements), func(i int) bool {
return needle.Compare(haystack.Elements[i]) == 0
})
if i < len(haystack.Elements) && needle.Compare(haystack.Elements[i]) == 0 {
return &object.Integer{Value: int64(i)}
}
return &object.Integer{Value: int64(index)}
return &object.Integer{Value: -1}
} else {
return newError("expected arg #1 to be `str` or `array` got got=%T", args[0])
}