-
Notifications
You must be signed in to change notification settings - Fork 4
/
list.go
111 lines (89 loc) · 2.09 KB
/
list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package object
import (
"fmt"
"strings"
)
// A List is a dynamic mutable linked list.
type List struct {
defaults
Value []Object
}
func (l *List) String() string {
var strs []string
for _, item := range l.Value {
strs = append(strs, item.String())
}
return fmt.Sprintf("[%s]", strings.Join(strs, ", "))
}
// Type returns the type of an Object.
func (l *List) Type() Type {
return ListType
}
// Equals checks whether or not two objects are equal to each other.
func (l *List) Equals(other Object) bool {
switch o := other.(type) {
case *List:
left, right := l.Value, o.Value
if len(left) != len(right) {
return false
}
for i, item := range left {
if !item.Equals(right[i]) {
return false
}
}
return true
default:
return false
}
}
// Prefix applies a prefix operator to an object, returning the result. If the operation
// cannot be performed, (nil, false) is returned.
func (l *List) Prefix(op string) (Object, bool) {
if op == "," {
return &Tuple{Value: []Object{l}}, true
}
return nil, false
}
// Infix applies a infix operator to an object, returning the result. If the operation
// cannot be performed, (nil, false) is returned.
func (l *List) Infix(op string, right Object) (Object, bool) {
if op == "," {
return &Tuple{
Value: []Object{l, right},
}, true
}
if op == "+" {
other, ok := right.(*List)
if !ok {
return nil, false
}
return &List{Value: append(l.Value, other.Value...)}, true
}
return nil, false
}
// Items returns a slice containing all objects in an Object, or false otherwise.
func (l *List) Items() ([]Object, bool) {
return l.Value, true
}
// SetSubscript sets the value of a subscript of an Object, e.g. foo[bar] = baz.
// Returns false if it can't be done.
func (l *List) SetSubscript(index Object, to Object) bool {
num, ok := index.(*Number)
if !ok {
return false
}
i := int(num.Value)
if i < 0 || i >= len(l.Value) {
return false
}
l.Value[i] = to
return true
}
// Iter creates an iterable from an Object.
func (l *List) Iter() (Iterable, bool) {
return &ListIterable{
List: l,
Index: 0,
}, true
}