Created
April 7, 2019 05:38
-
-
Save siddharth178/f36bf1146ab6329e50af383d1fe56d10 to your computer and use it in GitHub Desktop.
function composer
This file contains 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" | |
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