Skip to content
Snippets Groups Projects
Commit e9ef34f4 authored by Martin Schmollinger's avatar Martin Schmollinger
Browse files

Solution to exercise of unit 03-02

parent bc8039dd
No related branches found
No related tags found
No related merge requests found
module gitlab.reutlingen-university.de/go-exercises/go-starter/exercise_0302
go 1.20
package main
import "fmt"
type calcFunc func(a, b int) int
func compute(calc calcFunc, init int, a ...int) (result int) {
result = init
for _, n := range a {
result = calc(result, n)
}
return
}
func selectCalcFunc(calcOp rune) calcFunc {
return func(a, b int) int {
switch calcOp {
case '+':
return a + b
case '*':
return a * b
case '-':
return a - b
default:
panic("Illegal operation")
}
}
}
func main() {
fmt.Println(compute(selectCalcFunc('+'), 0, 1, 2, 3))
fmt.Println(compute(selectCalcFunc('-'), 0, 1, 2, 3))
fmt.Println(compute(selectCalcFunc('*'), 1, 1, 2, 3))
fmt.Println(compute(selectCalcFunc('?'), 1, 1, 2, 3))
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment