Created
April 5, 2020 13:59
-
-
Save foolishway/33783674c2004757c913d3374ef46f7d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"math" | |
) | |
type Calculator struct { | |
acc float64 | |
} | |
type OP func(v float64) float64 | |
func (c *Calculator) Do(op OP) float64 { | |
c.acc = op(c.acc) | |
return c.acc | |
} | |
func add(v float64) OP{ | |
return func(vv float64) float64 { | |
return v + vv | |
} | |
} | |
func sub(v float64) OP{ | |
return func(vv float64) float64 { | |
return vv - v | |
} | |
} | |
func squre(_ float64) OP{ | |
return func(vv float64) float64 { | |
return vv * vv | |
} | |
} | |
func main() { | |
var c Calculator | |
fmt.Println(c.Do(add(100))) | |
fmt.Println(c.Do(sub(50))) | |
fmt.Println(c.Do(squre(0))) | |
fmt.Println(c.Do(math.Sqrt)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment