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

@@ -13,6 +13,7 @@ type Array struct {
func (ao *Array) Type() ObjectType {
return ARRAY_OBJ
}
func (ao *Array) Inspect() string {
var out bytes.Buffer
@@ -30,25 +31,34 @@ func (ao *Array) Inspect() string {
func (ao *Array) String() string {
return ao.Inspect()
}
func (ao *Array) Equal(other Object) bool {
func (a *Array) Less(i, j int) bool {
if cmp, ok := a.Elements[i].(Comparable); ok {
return cmp.Compare(a.Elements[j]) == -1
}
return false
}
func (ao *Array) Compare(other Object) int {
if obj, ok := other.(*Array); ok {
if len(ao.Elements) != len(obj.Elements) {
return false
return -1
}
for i, el := range ao.Elements {
cmp, ok := el.(Comparable)
if !ok {
return false
return -1
}
if !cmp.Equal(obj.Elements[i]) {
return false
if cmp.Compare(obj.Elements[i]) != 0 {
return cmp.Compare(obj.Elements[i])
}
}
return true
return 0
}
return false
return -1
}
func (ao *Array) Copy() *Array {
elements := make([]Object, len(ao.Elements))
for i, e := range ao.Elements {
@@ -70,10 +80,3 @@ func (ao *Array) Len() int {
func (ao *Array) Swap(i, j int) {
ao.Elements[i], ao.Elements[j] = ao.Elements[j], ao.Elements[i]
}
func (ao *Array) Less(i, j int) bool {
if cmp, ok := ao.Elements[i].(Comparable); ok {
return cmp.Less(ao.Elements[j])
}
return false
}