Skip to content

Instantly share code, notes, and snippets.

@sysbot
Forked from gotohr/gist:7005197
Created October 6, 2015 07:34
Show Gist options
  • Save sysbot/cfafd22c2458036447ff to your computer and use it in GitHub Desktop.
Save sysbot/cfafd22c2458036447ff 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")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment