array builtins
This commit is contained in:
@@ -49,3 +49,31 @@ func (ao *Array) Equal(other Object) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
func (ao *Array) Copy() *Array {
|
||||
elements := make([]Object, len(ao.Elements))
|
||||
for i, e := range ao.Elements {
|
||||
elements[i] = e
|
||||
}
|
||||
return &Array{Elements: elements}
|
||||
}
|
||||
|
||||
func (ao *Array) Reverse() {
|
||||
for i, j := 0, len(ao.Elements)-1; i < j; i, j = i+1, j-1 {
|
||||
ao.Elements[i], ao.Elements[j] = ao.Elements[j], ao.Elements[i]
|
||||
}
|
||||
}
|
||||
|
||||
func (ao *Array) Len() int {
|
||||
return len(ao.Elements)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
@@ -26,3 +26,16 @@ func (b *Boolean) Equal(other Object) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
func (b *Boolean) Int() int64 {
|
||||
if b.Value {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (b *Boolean) Less(other Object) bool {
|
||||
if obj, ok := other.(*Boolean); ok {
|
||||
return b.Int() < obj.Int()
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -24,3 +24,9 @@ func (i *Integer) Equal(other Object) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
func (i *Integer) Less(other Object) bool {
|
||||
if obj, ok := other.(*Integer); ok {
|
||||
return i.Value < obj.Value
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -15,3 +15,6 @@ func (n *Null) Equal(other Object) bool {
|
||||
_, ok := other.(*Null)
|
||||
return ok
|
||||
}
|
||||
func (n *Null) Less(other Object) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ const (
|
||||
// Returns `true` iif the types and values are identical, `false` otherwise.
|
||||
type Comparable interface {
|
||||
Equal(other Object) bool
|
||||
Less(other Object) bool
|
||||
}
|
||||
|
||||
// Immutable is the interface for all immutable objects which must implement
|
||||
|
||||
@@ -24,3 +24,9 @@ func (s *String) Equal(other Object) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
func (s *String) Less(other Object) bool {
|
||||
if obj, ok := other.(*String); ok {
|
||||
return s.Value < obj.Value
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user