Created
October 16, 2013 09:36
-
-
Save gotohr/7005197 to your computer and use it in GitHub Desktop.
declarative function composition - golang
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" | |
type F func(i int) int | |
func (f F) compose(inner F) F { | |
return func(i int) int { return f(inner(i)) } | |
} | |
func main() { | |
var f1 F = func(i int) int { | |
return i * 2 | |
} | |
var f2 F = func(i int) int { | |
return i + 1 | |
} | |
var f3 F = func(i int) int { | |
return i + 10 | |
} | |
f := f1.compose(f2).compose(f3) | |
fmt.Println(f(2)) | |
fmt.Println("end") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Searching exactly this, thanks for sharing.