lua-users home
lua-l archive

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


2016-08-02 4:37 GMT+02:00 Duane Leslie <parakleta@darkreality.org>:

> `pairs` is actually building me a custom iterator closure which
> it returns as its first result.

It may feel like that but in fact `pairs` simply returns `next,tbl,nil.`
That is why you don't really need __next.  You can simply say

    for k,v in mynext,tbl do ... end

`io.lines` and `string.gmatch` do make custom closures. You can do
things like

> f = ("The quick brown fox jumps over the lazy dog"):gmatch"%S*"
> repeat word = f() until word=='over'
> for word in f do print(word) end
the
lazy
dog

You can't do that with "pairs".