7 ms·
State Machine in Real Life
- loopz 7y agoState Machines, also called runtime programs. I think when people call it "State Machines", it's for when they know the slightest breeze will trip it up. It's so seductive, because your initial use case gets resolved fairly quickly.. Ironically, the places they have worked wonders, is actually game engines from early 90's, where they have specific design, purposes, and limitations.
- exdsq 7y agoThey work well for protocols too!
- heavenlyblue 7y agoA reasonably powerful subset of regex is a state machine.
- heavenlyblue 7y agoState machines don’t need to be runtime programs. They can be expressed exactly using a static type system - that’s kind of exact the proposition of them.
- jgwil2 7y agoI feel I've read a number of pieces on programming that talk about the evils of if statements (on which I more or less agree) and advocating their replacement by state machines. However I've never seen a super simple implementation of this that seems to obviously fulfill most branching use cases. The closest is the state/strategy pattern, which is great but if patterns (as it's sometimes said) reflect deficiencies in a language, what would a language with first-class state machines look like?
- janci 7y agoVHDL is close. If you define nextstate logic (logic expression that determines next state based on current state and current input) and nextstate latch (that transfers calculated next state to current state register) it gets recognized as finite state machine and optimized by the synthetizer.
- monocasa 7y agoState machines are pretty simple in rust. Pseudo code to get the gist: state = match (state, event) { (State1, Event1) => { State2 } (State1, Event2) => { ... } ... };
- jgwil2 7y agoDoes this end up replacing a large portion of if/else or switch statements?
- heavenlyblue 7y agoThis is basically a switch statement :)
- jgwil2 7y agoIf it evaluates top to bottom in an order-dependent fashion then I suppose so. If it is order independent then it's more like what I want from a state machine.
- wallflower 7y agoWhile there are less of these now (possibly because of the feature described here), if you find a store with an automatic door that swings out, standing on the pad that is outside to block the door from moving is a good way to annoy your friends who are behind you and teach them about simple state machines.
- jgwil2 7y agoAnother common one (at least once upon a time) is the coin-operated turnstile, which is basically (ab)* where a is inserting a coin and b is rotating.
- stevefan1999 7y agoEat, work, sleep, repeat is already a state machine