Skip to content

Commit f866a3c

Browse files
committedFeb 25, 2015
Iterate through cells slice with ranged loops
1 parent 1a7221a commit f866a3c

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed
 

‎lispy/lval.go

+16-16
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type lval struct {
2121
num int64 // lvalNumType
2222
err string // lvalErrType
2323
sym string // lvalSymType
24-
cell []*lval // lvalSexprType
24+
cells []*lval // lvalSexprType
2525
}
2626

2727
// lvalNum creates an lval number
@@ -52,12 +52,12 @@ func lvalSym(s string) *lval {
5252
func lvalSexpr() *lval {
5353
v := new(lval)
5454
v.ltype = lvalSexprType
55-
v.cell = make([]*lval, 0)
55+
v.cells = make([]*lval, 0)
5656
return v
5757
}
5858

5959
func (v *lval) cellCount() int {
60-
return len(v.cell)
60+
return len(v.cells)
6161
}
6262

6363
func (v *lval) ltypeString() string {
@@ -101,14 +101,14 @@ func (v *lval) lvalAdd(x *lval) {
101101
if x == nil {
102102
fmt.Println("ERROR: Failed to add lval, addition is nil")
103103
} else {
104-
v.cell = append(v.cell, x)
104+
v.cells = append(v.cells, x)
105105
}
106106
}
107107

108108
func (v *lval) lvalExprString(openChar string, closeChar string) string {
109109
s := openChar
110-
for i := 0; i < v.cellCount(); i++ {
111-
s += v.cell[i].lvalString()
110+
for i, cell := range v.cells {
111+
s += cell.lvalString()
112112
if i < v.cellCount()-1 {
113113
s += " "
114114
}
@@ -165,12 +165,12 @@ func lvalRead(tree mpc.MpcAst) *lval {
165165

166166
func (v *lval) lvalEvalSexpr() *lval {
167167
// Evaluate children
168-
for i := 0; i < v.cellCount(); i++ {
169-
v.cell[i] = v.cell[i].lvalEval()
168+
for i, cell := range v.cells {
169+
v.cells[i] = cell.lvalEval()
170170
}
171171
// Error checking
172-
for i := 0; i < v.cellCount(); i++ {
173-
if v.cell[i].ltype == lvalErrType {
172+
for i, cell := range v.cells {
173+
if cell.ltype == lvalErrType {
174174
return v.lvalTake(i)
175175
}
176176
}
@@ -199,10 +199,10 @@ func (v *lval) lvalEval() *lval {
199199
}
200200

201201
func (v *lval) lvalPop(i int) *lval {
202-
x := v.cell[i]
203-
copy(v.cell[i:], v.cell[i+1:])
204-
v.cell[len(v.cell)-1] = nil
205-
v.cell = v.cell[:len(v.cell)-1]
202+
x := v.cells[i]
203+
copy(v.cells[i:], v.cells[i+1:])
204+
v.cells[len(v.cells)-1] = nil
205+
v.cells = v.cells[:len(v.cells)-1]
206206
return x
207207
}
208208

@@ -212,8 +212,8 @@ func (v *lval) lvalTake(i int) *lval {
212212

213213
func builtinOp(a *lval, op string) *lval {
214214
// Ensure all arguments are numbers
215-
for i := 0; i < a.cellCount(); i++ {
216-
if a.cell[i].ltype != lvalNumType {
215+
for _, cell := range a.cells {
216+
if cell.ltype != lvalNumType {
217217
return lvalErr("Cannot operate on non-number!")
218218
}
219219
}

0 commit comments

Comments
 (0)
Please sign in to comment.