Given these declarations like F # ...
type Message =
| MessageA
| MessageB
| MessageC
| MessageD
type State = {
Name:string
NextStateMap: Map<Message,State>
}
... there is an equally expressive definition of this particular state machine ...
let rec state0 = { Name = "0"; NextStateMap = Map.ofList [ (MessageA,state1); (MessageB,state2)] }
and state1 = { Name = "1"; NextStateMap = Map.ofList [ (MessageB,state3)] }
and state2 = { Name = "2"; NextStateMap = Map.ofList [ (MessageA,state3)] }
and state3 = { Name = "3"; NextStateMap = Map.ofList [ (MessageC,state4)] }
and state4 = { Name = "4"; NextStateMap = Map.ofList [ (MessageD,state5)] }
and state5 = { Name = "5"; NextStateMap = Map.empty}
... with Python?
Note that through “rec” we did not need to make assignments in the order defined by the topological type ... (for example, state0 is defined in terms of state1, although state 1 is defined later).
PS The ability to use strings as state identifiers ...
stateMachine = {
"0" : { "A":"1", "B":"2"},
"1" : { "B":"3" },
...
... leaves open a window with invalid keys (that is, invalid message qualifiers on the destination machine).
source
share