[ZeroBrane Studio] Re: Newbie question on stateless iterators example in zerobrane

  • From: Paul K <paul@xxxxxxxxxxxxx>
  • To: "zerobrane@xxxxxxxxxxxxx" <zerobrane@xxxxxxxxxxxxx>
  • Date: Sat, 22 Jul 2017 09:23:17 -0700

Hi Niko,

running this in zerobrane gives two instances for the iterator returned by 
fromto as if it were a closure, however doing the same on the command line 
(lua 5.2 OS-X) works as expected, i.e. single instance for the function (and 
two different tables). What am I missing?

ZeroBrane Studio is using LuaJIT internally, which is compatible with
Lua 5.1 and both show consistent results:

LuaJIT
function: 0x00027f50    table: 0x00027f68
function: 0x00021ba8    table: 0x00021c30

Lua 5.1
function: 0002C078    table: 00029EA0
function: 0002C098    table: 0002A080

Lua 5.2 and Lua 5.3 indeed show the same function, so it looks like
this behavior changed in Lua 5.2+:
function: 0003BC40    table: 000395A0
function: 0003BC40    table: 00039618

Paul.

On Sat, Jul 22, 2017 at 6:18 AM, Niko Neufeld <niko.neufeld@xxxxxxxxx> wrote:
Dear all,

Please forgive what is for sure a total newbie question: I am going through 
the the LUA course using zero-brane and I remarked that when I play with 
stateless iterators like so
function fromto(a, b)
 return function (state)
if state[1] > state[2] then
 return nil
else
 state[1] = state[1] + 1
 return state[1] - 1
end
end, { a, b }
end

print(fromto(2, 5))
print(fromto(4, 7))

running this in zerobrane gives two instances for the iterator returned by 
fromto as if it were a closure, however doing the same on the command line 
(lua 5.2 OS-X) works as expected, i.e. single instance for the function (and 
two different tables). What am I missing?

Many thanks,


Other related posts: