Simplified Equality
This commit is contained in:
@@ -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])
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user