Hacker News new | past | comments | ask | show | jobs | submit login

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...).



Yeah, 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...




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: