Skip to content

Commit

Permalink
Immutable append/prepend.
Browse files Browse the repository at this point in the history
  • Loading branch information
corywalker committed Jul 17, 2017
1 parent bcf2a71 commit 4ca3a66
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
11 changes: 7 additions & 4 deletions expreduce/builtin_list.go
Expand Up @@ -549,8 +549,9 @@ func GetListDefinitions() (defs []Definition) {
if !isExpr {
return this
}
expr.Parts = append(expr.Parts, this.Parts[2])
return expr
res := NewExpression(append([]Ex{}, expr.Parts...))
res.Parts = append(res.Parts, this.Parts[2])
return res
},
SimpleExamples: []TestInstruction{
&SameTest{"{a,b,c}", "Append[{a,b},c]"},
Expand All @@ -575,8 +576,10 @@ func GetListDefinitions() (defs []Definition) {
if !isExpr {
return this
}
expr.Parts = append(append([]Ex{expr.Parts[0]}, this.Parts[2]), expr.Parts[1:]...)
return expr
res := NewExpression([]Ex{expr.Parts[0]})
res.Parts = append(res.Parts, this.Parts[2])
res.Parts = append(res.Parts, expr.Parts[1:]...)
return res
},
SimpleExamples: []TestInstruction{
&SameTest{"{c,a,b}", "Prepend[{a,b},c]"},
Expand Down
8 changes: 8 additions & 0 deletions expreduce/ex_expression.go
Expand Up @@ -9,6 +9,7 @@ import "flag"
import "hash"

var printevals = flag.Bool("printevals", false, "")
var checkhashes = flag.Bool("checkhashes", false, "")

type Expression struct {
Parts []Ex
Expand Down Expand Up @@ -104,6 +105,7 @@ func (this *Expression) mergeSequences(es *EvalState, headStr string, shouldEval

func (this *Expression) Eval(es *EvalState) Ex {
lastExHash := uint64(0)
var lastEx Ex = this
currExHash := hashEx(this)
if currExHash == this.cachedHash {
return this
Expand All @@ -113,6 +115,12 @@ func (this *Expression) Eval(es *EvalState) Ex {
for currExHash != lastExHash {
lastExHash = currExHash
curr, isExpr := currEx.(*Expression)
if *checkhashes {
if isExpr && curr.cachedHash != 0 && currExHash != curr.cachedHash {
fmt.Printf("invalid cache: %v. Used to be %v\n", curr, lastEx)
}
lastEx = currEx
}

if isExpr && insideDefinition {
retVal, isReturn := tryReturnValue(curr)
Expand Down

0 comments on commit 4ca3a66

Please sign in to comment.