[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Why do we have ipairs?
- From: Sean Conner <sean@...>
- Date: Thu, 26 Jun 2014 17:39:51 -0400
It was thus said that the Great William Ahern once stated:
>
> What about label references? That is, if Lua allowed directly taking a
> reference to a label? Then you could use them directly, as when writing a
> classic state machine, or build your own jump tables which mapped keys to
> labels. Although you might have to add an optimization to memoize constant
> tables in the latter case.
>
> The label reference would have to be a new native type. But it might be a
> little more elegant at the syntax level.
>
> State machine example
For state machines, you can use functions, because Lua supports tail call
optimizations, which are essentially gotos. I implemented TFTP using that
method:
local read_data
local send_data
local receive_ack
local receive_data
local write_data
local send_ack
read_data = function() return send_data() end -- [1]
send_data = function() return receive_ack() end
receive_ack = function() return read_data() end
receive_data = fucntion() return write_data() end
write_data = function() return send_ack() end
send_ack = function() return receive_data() end
Both the client and the server can use the same state machine---where they
start depends on who is sending the data, and who is receiving the data.
-spc (I like this much better than using a switch or goto statements)
[1] Obviously this is just a sample of the code. There's a bit more
to it, like error checking, resending, etc.
- References:
- Re: Why do we have ipairs?, Thiago L.
- Re: Why do we have ipairs?, Daurnimator
- Re: Why do we have ipairs?, Thiago L.
- Re: Why do we have ipairs?, Axel Kittenberger
- Re: Why do we have ipairs?, Jay Carlson
- Re: Why do we have ipairs?, Axel Kittenberger
- Re: Why do we have ipairs?, Andrew Starks
- Re: Why do we have ipairs?, Axel Kittenberger
- Re: Why do we have ipairs?, Paige DePol
- Re: Why do we have ipairs?, William Ahern