Created
January 6, 2015 12:32
-
-
Save kevinlebrun/d07249f3c87205cbf473 to your computer and use it in GitHub Desktop.
A simple state machine using recursive type in Go as shared here: http://talks.golang.org/2014/compiling.slide#6
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" | |
type F func(*State) F | |
type State int | |
func Begin(s *State) F { | |
*s = 1 | |
return Middle | |
} | |
func Middle(s *State) F { | |
*s++ | |
if *s >= 10 { | |
return End | |
} | |
return Middle | |
} | |
func End(s *State) F { | |
fmt.Println(*s) | |
return nil | |
} | |
func main() { | |
var f F = Begin | |
var s State | |
for f != nil { | |
f = f(&s) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment