Skip to content

Instantly share code, notes, and snippets.

@siddharth178
Created April 7, 2019 05:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save siddharth178/f36bf1146ab6329e50af383d1fe56d10 to your computer and use it in GitHub Desktop.
Save siddharth178/f36bf1146ab6329e50af383d1fe56d10 to your computer and use it in GitHub Desktop.
function composer
package main
import "fmt"
func makeAdder(x int) func(x int) int {
return func(n int) int {
return x + n
}
}
func mul3(x int) int {
return x * 3
}
func compose(f, g func(x int) int) func(x int) int {
return func(n int) int {
return f(g(n))
}
}
func main() {
fmt.Println(mul3(3))
add1 := makeAdder(1)
fmt.Println(add1(100))
fg := compose(mul3, add1)
fmt.Println(fg(10))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment