Skip to content

Instantly share code, notes, and snippets.

@gotohr
Created October 16, 2013 09:36
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save gotohr/7005197 to your computer and use it in GitHub Desktop.
Save gotohr/7005197 to your computer and use it in GitHub Desktop.
declarative function composition - golang
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")
}
@gsscoder
Copy link

Searching exactly this, thanks for sharing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment