[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Table of functions as state machine
- From: "Duncan Cross" <duncan.cross@...>
- Date: Fri, 18 Jul 2008 08:03:45 +0100
On Fri, Jul 18, 2008 at 6:26 AM, Mike Crowe <email@example.com> wrote:
> 2) I ran across something about tail calls, or tail recursion, does that
You can use tail calls to approximate state machines, but it will mean
the system is no longer asynchronous in the same way and will run
until the terminating state is hit, unless you make use of coroutines.
It also doesn't help with your meaningful-names-for-debugging problem.
It does mean that you can use arbitrary arguments appropriate for the
state instead of always a single data argument, however.
--- code start
-- local variables need to be predeclared so they
-- can be referred to as upvalues before their own
-- function value is defined
local prepare, process1, process2
prepare = function(data)
process1 = function(data)
process2 = function(data)
--- code end
Tail calls mean that one of these states could return with a call to
itself, an unlimited number of times, without overflowing the stack.