Skip to content

Instantly share code, notes, and snippets.

@BruceChen7
Last active April 14, 2022 10:42
Show Gist options
  • Save BruceChen7/662d5b6fd68935406d881cd0128b386c to your computer and use it in GitHub Desktop.
Save BruceChen7/662d5b6fd68935406d881cd0128b386c to your computer and use it in GitHub Desktop.
#golang#function#high-order
// 高阶函数
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