Created
January 3, 2022 12:12
-
-
Save roytouw7/24855246118cd56a183063a0627139e6 to your computer and use it in GitHub Desktop.
This file contains 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" | |
func alfa(i int) (int, error) { | |
if i > 1 { | |
return i, nil | |
} | |
return 0, fmt.Errorf("i was greater than 1") | |
} | |
func beta(i int) (int, error) { | |
if i % 2 == 0 { | |
return i, nil | |
} | |
return 0, fmt.Errorf("i was odd") | |
} | |
func gamma(i int) (int, error) { | |
if i < 100 { | |
return i, nil | |
} | |
return 0, fmt.Errorf("i was less than 100") | |
} | |
func doWithoutError(i int, err error, f func(int) (int, error)) (int, error) { | |
if err != nil { | |
return i, err | |
} | |
return f(i) | |
} | |
func regular(i int) int { | |
i, err := alfa(i) | |
if err != nil { | |
panic(err) | |
} | |
i, err = beta(i) | |
if err != nil { | |
panic(err) | |
} | |
i, err = gamma(i) | |
if err != nil { | |
panic(err) | |
} | |
return i | |
} | |
func railway(i int) int { | |
i, err := alfa(i) | |
i, err = doWithoutError(i, err, beta) | |
i, err = doWithoutError(i, err, gamma) | |
if err != nil { | |
panic(err) | |
} | |
return i | |
} | |
// https://www.journal.petrausch.info/post/2020/05/must-pattern/ | |
func main() { | |
input := 2 | |
fmt.Println(regular(input)) | |
fmt.Println(railway(input)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment