lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

> > expList is a function which must return at least another function, and
> > can return a state (for persistance of data) and an initial value.
> It doesn't need to be a function. It is an expList, so it can also be
> two (or three) expressions. It is exactly the same as in a multiple
> assignment:
>   a,b = f()
>   a,b = e1, e2

Silly me, the name should have been a tip... And the readline example
tripped me.
You say it must be at least two expressions, but in your example, lines()
returns only a function, no state.

> > t = { "a", "b", "c" }
> > for i, k in nexti(t) do print(i, k) end
> > for k, v in next, t do print(k, v) end
> > for k, v in t do print(k, v) end
> > The results are identical. I am not sure to understand the second syntax
> > (with 'next').
> See previous remark. In the second syntax, you explicitly give the
> iteration function (`next') and the state (`t'). The third syntax is
> only a kind of syntactic sugar.

And it preserves backward compatibility, a good thing.

> > It seems I cannot write "for k in next do ... end", Lua reports 'next'
> > expecting a parameter, but which one it gets in the above line?
> According to your translation, uou are calling _func(_state, var1), where
> _func=next, but _state=nil.

If I understand correctly, actually 'nexti' is just a function returning
'next' and its parameter.

> Assert returns its argument (if it is not false); in the example this is

A point I didn't saw in Lua 4.0 manual (the given pseudo-code don't return
Either that's an evolution, or that's a point of documentation that can be
improved :-)
Likewise, I cannot see what closefile() (or now f:close()) returns.

> > But I am no longer sure of the advantage of this state over the previous
> > version...
> > Well, it looks more like OO, and I am probably missing some side effect.
> This is a matter of taste. The big advantage of using the state is when
> you don't need to create any new "object" (table, closure, etc.) to run
> the for. The `nexti' is an example.
> In "heavier" loops, the cost of an extra object is negligible. E.g.,
> in the file example, you already have to open the file, create a file
> handler, create several strings (the lines), etc. etc. An extra closure
> (or table) makes little difference to the total bill.

Plus when I wrote that, I wasn't aware of the real nature of expList... The
state has a life of its own.

Thank you for answering, Roberto. I admit I was expecting your feedback :-)

> BTW, you should add your explanation into the Wiki.

I wrote it a bit with this idea in mind :-) I add your anwsers and I will
paste it soon...

OK, I done it before sending this mail. I have put it in the "Learning Lua"
page, but it can be moved it is doesn't fit this place. It is located there:


Philippe Lhoste (Paris -- France)
Professional programmer and amateur artist

GMX - Die Kommunikationsplattform im Internet.