Skip to content

Commit

Permalink
simplification of binary operators
Browse files Browse the repository at this point in the history
  • Loading branch information
guamoko995 committed Apr 13, 2024
1 parent 317425e commit 3ff5185
Showing 1 changed file with 14 additions and 20 deletions.
34 changes: 14 additions & 20 deletions calcbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
// - The variable is represented by the symbol 'x' (code 120) and the variable index
// immediately following it. It is this index that will be passed to the getVals
// callback to obtain the value of this variable during calculation.
func BuildСalcFunc(expression string, getVar func(i int) float64) (calcFunc func() float64, err error) {
calcFunc, apendix, err := buildСalcFunc(expression, getVar)
func BuildСalcFunc(expression string, getVar func(i int) float64) (CalcFunc func() float64, err error) {
CalcFunc, apendix, err := buildСalcFunc(expression, getVar)
if err != nil {
return nil, err
}
Expand All @@ -36,26 +36,18 @@ func BuildСalcFunc(expression string, getVar func(i int) float64) (calcFunc fun
}

// Valid operators
var binaryOperators = map[string]func(x, y func() float64) func() float64{
"+": func(x, y func() float64) func() float64 {
return func() float64 {
return x() + y()
}
var binaryOperators = map[string]func(x, y float64) float64{
"+": func(x, y float64) float64 {
return x + y
},
"-": func(x, y func() float64) func() float64 {
return func() float64 {
return x() - y()
}
"-": func(x, y float64) float64 {
return x - y
},
"*": func(x, y func() float64) func() float64 {
return func() float64 {
return x() * y()
}
"*": func(x, y float64) float64 {
return x * y
},
"/": func(x, y func() float64) func() float64 {
return func() float64 {
return x() / y()
}
"/": func(x, y float64) float64 {
return x / y
},
}

Expand All @@ -81,7 +73,9 @@ func buildСalcFunc(expression string, getVar func(i int) float64) (calcFunc fun
return nil, "", err
}
}
return operator(operands[0], operands[1]), apendix, nil
return func() float64 {
return operator(operands[0](), operands[1]())
}, apendix, nil
}

// var
Expand Down

0 comments on commit 3ff5185

Please sign in to comment.