Created
December 31, 2021 09:37
-
-
Save aquilax/333f5847bf86da63814cc2314dcc831e to your computer and use it in GitHub Desktop.
Function composition with go
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
// 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