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