lua-users home
lua-l archive

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


Fernando P. García wrote:
> Hello,
> 
> I'm pleased to announce that Nutria Seawolf is ready to your feedback.
> Nutria 0.6 is a PHP Standard Library, why not give it a try if you ever
> wanted to do your webscripts in your favorite programming language?

nice effort (even if i question the point of replicating PHP's vices).  I wanted to check the 'feel' of the code, and i don't know why, but i zeroed on the empty() implementation:

function empty(var)
    return
        (var == nil) or
        (type(var) == 'boolean' and var == false or false) or
        (type(var) == 'number' and var == 0 or false) or
        (type(var) == 'string' and (var == '' or var == '0') or false) or
        (type(var) == 'table' and next(var) == nil or false)
end

remember that in Lua, two values can only be equal if they're of the same type, so you don't have to check the parameter's type before comparing:

function empty(var)
    return
        var == nil or
        var == false or
        var == 0 or
        var == '' or
        var == '0' or
        (type(var)=='table' and next(var) == nil)
end

also, when you compare a single value to several options, another option is to use a local table:

do
    local falses = {
        [false] = true,
        [0] = true,
        [''] = true,
        ['0'] = true,
    }
    function empty (var)
        return not var or falses[var] or (type(var) == 'table' and next(var)==nil)
    end
end


-- 
Javier