lua-users home
lua-l archive

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


Lots of thanks Javier!, just opted for second option, and will include it for next release.

Blessings.

2010/1/23 Javier Guerra <javier@guerrag.com>
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



--
Fernando P. García, http://www.develcuy.com
Developer - Analista de Sistemas
+51 1 9 8991 7871, Calle Santa Catalina Ancha #377, Cusco -Perú

** Antes de imprimir este mensaje piensa en tu compromiso con el medio ambiente, protegerlo depende de tí.