Last active
April 14, 2022 10:42
-
-
Save BruceChen7/662d5b6fd68935406d881cd0128b386c to your computer and use it in GitHub Desktop.
#golang#function#high-order
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
// 高阶函数 | |
func multiply(a int) func(int) int { | |
return func(i int) int { | |
return a * i | |
} | |
} | |
func subtract(a int) func(int) int { | |
return func(i int) int { | |
return i - a | |
} | |
} | |
func main() { | |
in := 10 // imagine this gives us something | |
m := multiply(4) // example data | |
s := subtract(10) // example data | |
// subtract then multiply | |
sm := func(i int) int { return m(s(i)) } | |
ms := func(i int) int { return s(m(i)) } | |
fmt.Printf("%v %v\n", ms(in), sm(in)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment