lua-users home
lua-l archive

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


Op Ma., 17 Sep. 2018 om 13:46 het Tom Sutcliffe <tomsci@me.com> geskryf:

> It struct me today that "dofile" must be the only function in the standard library that is entirely implementable using other stuff in the standard library, and as such is something of an oddity given there is very little redundancy and "more than one way to do it" anywhere else.

Several others:

'pairs' is equivalent to:

function(tbl)
    local meta = getmetatable(tbl)
    local _pairs = meta and rawget(meta,"__pairs)
    if _pairs then return _pairs(tbl)
    else return next,tbl
    end
end

In particular, if you know that you never set __ipairs, you can code
just "for k,v in next,tbl" rather than "for l,v in pairs(tbl)" and
save yourself three keypresses.

Also ipairs (obviously so; the manual practically spells out the
code), loadfile and print if you grant me the io library, tonumber and
tostring if you have not killed string metamethods, pcall if we still
have xpcall.

I suspect somebody will be able to fiddle some more if
debug.getregistry is intact.