|
|
||
|
Fernando P. GarcÃa wrote: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:
> 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?
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