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
|
||||
}
|
||||
|
||||
@@ -11,31 +11,29 @@ type Boolean struct {
|
||||
func (b *Boolean) Type() ObjectType {
|
||||
return BOOLEAN_OBJ
|
||||
}
|
||||
|
||||
func (b *Boolean) Inspect() string {
|
||||
return fmt.Sprintf("%t", b.Value)
|
||||
}
|
||||
|
||||
func (b *Boolean) Clone() Object {
|
||||
return &Boolean{Value: b.Value}
|
||||
}
|
||||
|
||||
func (b *Boolean) String() string {
|
||||
return b.Inspect()
|
||||
}
|
||||
func (b *Boolean) Equal(other Object) bool {
|
||||
if obj, ok := other.(*Boolean); ok {
|
||||
return b.Value == obj.Value
|
||||
}
|
||||
return false
|
||||
}
|
||||
func (b *Boolean) Int() int64 {
|
||||
|
||||
func (b *Boolean) Int() int {
|
||||
if b.Value {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (b *Boolean) Less(other Object) bool {
|
||||
func (b *Boolean) Compare(other Object) int {
|
||||
if obj, ok := other.(*Boolean); ok {
|
||||
return b.Int() < obj.Int()
|
||||
return b.Int() - obj.Int()
|
||||
}
|
||||
return false
|
||||
return 1
|
||||
}
|
||||
|
||||
@@ -47,6 +47,7 @@ type Hash struct {
|
||||
func (h *Hash) Type() ObjectType {
|
||||
return HASH_OBJ
|
||||
}
|
||||
|
||||
func (h *Hash) Inspect() string {
|
||||
var out bytes.Buffer
|
||||
|
||||
@@ -64,28 +65,29 @@ func (h *Hash) Inspect() string {
|
||||
func (h *Hash) String() string {
|
||||
return h.Inspect()
|
||||
}
|
||||
func (h *Hash) Equal(other Object) bool {
|
||||
|
||||
func (h *Hash) Compare(other Object) int {
|
||||
if obj, ok := other.(*Hash); ok {
|
||||
if len(h.Pairs) != len(obj.Pairs) {
|
||||
return false
|
||||
return -1
|
||||
}
|
||||
for _, pair := range h.Pairs {
|
||||
left := pair.Value
|
||||
hashed := left.(Hashable)
|
||||
right, ok := obj.Pairs[hashed.HashKey()]
|
||||
if !ok {
|
||||
return false
|
||||
return -1
|
||||
}
|
||||
cmp, ok := left.(Comparable)
|
||||
if !ok {
|
||||
return false
|
||||
return -1
|
||||
}
|
||||
if !cmp.Equal(right.Value) {
|
||||
return false
|
||||
if cmp.Compare(right.Value) != 0 {
|
||||
return cmp.Compare(right.Value)
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
return 0
|
||||
}
|
||||
return false
|
||||
return -1
|
||||
}
|
||||
|
||||
@@ -9,24 +9,29 @@ type Integer struct {
|
||||
func (i *Integer) Type() ObjectType {
|
||||
return INTEGER_OBJ
|
||||
}
|
||||
|
||||
func (i *Integer) Inspect() string {
|
||||
return fmt.Sprintf("%d", i.Value)
|
||||
}
|
||||
|
||||
func (i *Integer) Clone() Object {
|
||||
return &Integer{Value: i.Value}
|
||||
}
|
||||
|
||||
func (i *Integer) String() string {
|
||||
return i.Inspect()
|
||||
}
|
||||
func (i *Integer) Equal(other Object) bool {
|
||||
|
||||
func (i *Integer) Compare(other Object) int {
|
||||
if obj, ok := other.(*Integer); ok {
|
||||
return i.Value == obj.Value
|
||||
switch {
|
||||
case i.Value < obj.Value:
|
||||
return -1
|
||||
case i.Value > obj.Value:
|
||||
return 1
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
func (i *Integer) Less(other Object) bool {
|
||||
if obj, ok := other.(*Integer); ok {
|
||||
return i.Value < obj.Value
|
||||
}
|
||||
return true
|
||||
return -1
|
||||
}
|
||||
|
||||
@@ -5,16 +5,18 @@ type Null struct{}
|
||||
func (n *Null) Type() ObjectType {
|
||||
return NULL_OBJ
|
||||
}
|
||||
|
||||
func (n *Null) Inspect() string {
|
||||
return "null"
|
||||
}
|
||||
|
||||
func (n *Null) String() string {
|
||||
return n.Inspect()
|
||||
}
|
||||
func (n *Null) Equal(other Object) bool {
|
||||
_, ok := other.(*Null)
|
||||
return ok
|
||||
}
|
||||
func (n *Null) Less(other Object) bool {
|
||||
return false
|
||||
|
||||
func (n *Null) Compare(other Object) int {
|
||||
if _, ok := other.(*Null); ok {
|
||||
return 0
|
||||
}
|
||||
return 1
|
||||
}
|
||||
|
||||
@@ -22,8 +22,7 @@ const (
|
||||
// values. It is the responsibility of the caller (left) to check for types.
|
||||
// Returns `true` iif the types and values are identical, `false` otherwise.
|
||||
type Comparable interface {
|
||||
Equal(other Object) bool
|
||||
Less(other Object) bool
|
||||
Compare(other Object) int
|
||||
}
|
||||
|
||||
// Immutable is the interface for all immutable objects which must implement
|
||||
|
||||
@@ -9,24 +9,29 @@ type String struct {
|
||||
func (s *String) Type() ObjectType {
|
||||
return STRING_OBJ
|
||||
}
|
||||
|
||||
func (s *String) Inspect() string {
|
||||
return fmt.Sprintf("%#v", s.Value)
|
||||
}
|
||||
|
||||
func (s *String) Clone() Object {
|
||||
return &String{Value: s.Value}
|
||||
}
|
||||
|
||||
func (s *String) String() string {
|
||||
return s.Value
|
||||
}
|
||||
func (s *String) Equal(other Object) bool {
|
||||
|
||||
func (s *String) Compare(other Object) int {
|
||||
if obj, ok := other.(*String); ok {
|
||||
return s.Value == obj.Value
|
||||
switch {
|
||||
case s.Value < obj.Value:
|
||||
return -1
|
||||
case s.Value > obj.Value:
|
||||
return 1
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
func (s *String) Less(other Object) bool {
|
||||
if obj, ok := other.(*String); ok {
|
||||
return s.Value < obj.Value
|
||||
}
|
||||
return false
|
||||
return 1
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user