Created
January 25, 2017 13:40
-
-
Save icalvo/fbad2c8ea99bdbdee9b611267ffa6eb7 to your computer and use it in GitHub Desktop.
F# Mealy FSM model for interactive console applications
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
// transition:('State -> 'Input -> 'State) -> isFinish:('State -> bool) -> initialState:'State -> input:seq<'Input> -> 'State list | |
let interactiveConsole transition isFinish initialState input = | |
input | |
|> Seq.scan transition initialState | |
|> Seq.takeWhile (not << isFinish) | |
|> Seq.toList | |
// unit -> seq<string> | |
let consoleInput = | |
Seq.initInfinite (fun _ -> Console.ReadLine()) | |
|> Seq.takeWhile ((<>) null) | |
// state:'State -> input:'Input -> 'State | |
let myTransition state input = | |
// Write your Mealy-style transition function here | |
state | |
// state:'State -> bool | |
let myIsFinish state = | |
// Just return true with your finish states | |
true | |
[<EntryPoint>] | |
let main argv = | |
let myInitialState = 0 | |
// Here we feed the FSM with console input. You can use any list of | |
// strings, for example for testing or replaying an execution. | |
// You may have some use for the final list of states, otherwise ignore it. | |
ignore <| interactiveConsole myTransition myIsFinish myInitialState consoleInput | |
0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment