3 ms·
In languages with first class functions and hash tables, you can use this instead (here in Lua): state1.goto = { state2 = function() ... end,
by pygy_ 15y ago
In languages with first class functions and hash tables, you can use this instead (here in Lua):
state1.goto = {
state2 = function() ... end,
state3 = function() ... end
}
state1.goto.state2()
Much cleaner (even though the Lua syntax for function literals is a bit heavy. It would look much nicer in CoffeeScript...).
- justincormack 15y agoYeah, state as functions is really nice, but it gets messy in languages that don't have tail recursion elimination (eg Javascript), as you have to have a trampoline mechanism to call the next state, I generally return the new state function and get the trampoline to call it. Which is less readable then just calling the new state as in your Lua example...