Skip to content

Instantly share code, notes, and snippets.

@aquilax
Created December 31, 2021 09:37
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 aquilax/333f5847bf86da63814cc2314dcc831e to your computer and use it in GitHub Desktop.
Save aquilax/333f5847bf86da63814cc2314dcc831e to your computer and use it in GitHub Desktop.
Function composition with go
// https://go.dev/play/p/xmai9kPOwl3
// Function composition with go
// You can edit this code!
// Click here and start typing.
package main
func main() {
example()
}
type composable = func() error
func compose(c []composable) error {
for _, cf := range c {
if err := cf(); err != nil {
return err
}
}
return nil
}
func example() error {
a := 3
inc := func() error {
a = a + 1
return nil
}
if err := compose([]composable{inc, inc, inc, func() error {
a = a - 1
return nil
}}); err != nil {
return err
} else {
println(a)
return nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment