|
|
@ -1,15 +1,19 @@
|
|
|
|
package main
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type proc func(...interface{}) (interface{}, error)
|
|
|
|
type proc func(...interface{}) (interface{}, error)
|
|
|
|
|
|
|
|
|
|
|
|
func addition(vals ...interface{}) (interface{}, error) {
|
|
|
|
func addition(vals ...interface{}) (interface{}, error) {
|
|
|
|
a := accumulator{
|
|
|
|
a := accumulator{
|
|
|
|
name: "addition",
|
|
|
|
name: "addition",
|
|
|
|
floatFn: func(left, right float64) float64 {
|
|
|
|
floatFn: func(left, right float64) (float64, error) {
|
|
|
|
return left + right
|
|
|
|
return left + right, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
intFn: func(left, right int64) int64 {
|
|
|
|
intFn: func(left, right int64) (int64, error) {
|
|
|
|
return left + right
|
|
|
|
return left + right, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return a.total(vals...)
|
|
|
|
return a.total(vals...)
|
|
|
@ -18,11 +22,11 @@ func addition(vals ...interface{}) (interface{}, error) {
|
|
|
|
func subtraction(vals ...interface{}) (interface{}, error) {
|
|
|
|
func subtraction(vals ...interface{}) (interface{}, error) {
|
|
|
|
a := accumulator{
|
|
|
|
a := accumulator{
|
|
|
|
name: "subtraction",
|
|
|
|
name: "subtraction",
|
|
|
|
floatFn: func(left, right float64) float64 {
|
|
|
|
floatFn: func(left, right float64) (float64, error) {
|
|
|
|
return left - right
|
|
|
|
return left - right, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
intFn: func(left, right int64) int64 {
|
|
|
|
intFn: func(left, right int64) (int64, error) {
|
|
|
|
return left - right
|
|
|
|
return left - right, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return a.total(vals...)
|
|
|
|
return a.total(vals...)
|
|
|
@ -31,11 +35,30 @@ func subtraction(vals ...interface{}) (interface{}, error) {
|
|
|
|
func multiplication(vals ...interface{}) (interface{}, error) {
|
|
|
|
func multiplication(vals ...interface{}) (interface{}, error) {
|
|
|
|
a := accumulator{
|
|
|
|
a := accumulator{
|
|
|
|
name: "multiplication",
|
|
|
|
name: "multiplication",
|
|
|
|
floatFn: func(left, right float64) float64 {
|
|
|
|
floatFn: func(left, right float64) (float64, error) {
|
|
|
|
return left * right
|
|
|
|
return left * right, nil
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
intFn: func(left, right int64) (int64, error) {
|
|
|
|
|
|
|
|
return left * right, nil
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return a.total(vals...)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func division(vals ...interface{}) (interface{}, error) {
|
|
|
|
|
|
|
|
a := accumulator{
|
|
|
|
|
|
|
|
name: "division",
|
|
|
|
|
|
|
|
floatFn: func(left, right float64) (float64, error) {
|
|
|
|
|
|
|
|
if right == 0.0 {
|
|
|
|
|
|
|
|
return 0.0, errors.New("float division by zero")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return left / right, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
intFn: func(left, right int64) int64 {
|
|
|
|
intFn: func(left, right int64) (int64, error) {
|
|
|
|
return left * right
|
|
|
|
if right == 0 {
|
|
|
|
|
|
|
|
return 0, errors.New("int division by zero")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return left / right, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return a.total(vals...)
|
|
|
|
return a.total(vals...)
|
|
|
|