How to determine if all arguments are nil?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

How to determine if all arguments are nil?

Tim Hunter
I have a varargs function. What is the Lua idiomatic way of determining if all the arguments are nil? I've thought of two ways:

1. #{...} == 0
2. next{...} == nil

This function is called very frequently, so I'd prefer not to have to create a table if I can avoid it.

--
RMagick OS X Installer [http://rubyforge.org/projects/rmagick/]
RMagick Hints & Tips [http://rubyforge.org/forum/forum.php?forum_id=1618]
RMagick Installation FAQ [http://rmagick.rubyforge.org/install-faq.html]


Reply | Threaded
Open this post in threaded view
|

Re: How to determine if all arguments are nil?

Rici Lake-2

On 28-Jun-07, at 7:39 PM, Tim Hunter wrote:

I have a varargs function. What is the Lua idiomatic way of determining if all the arguments are nil? I've thought of two ways:

1. #{...} == 0

That won't work reliably. Try it on #{nil, true, nil}

2. next{...} == nil

That's probably the fastest.

This function is called very frequently, so I'd prefer not to have to create a table if I can avoid it.

Perhaps you need to rethink your design, then :) It's hard to do
non-trivial things with varargs in Lua without turning them into
a table. About the only other useful thing you can do with them
is pass them to another function.

If you knew the function would never be called with more than
three arguments (say), you could write it as a fixed arg
function:

  function foo(a, b, c)
    if (a or b or c) == nil then
      -- they're all nil
    else
      -- something's in there somewhere
    end
  end


Reply | Threaded
Open this post in threaded view
|

Re: How to determine if all arguments are nil?

Michael Broughton
In reply to this post by Tim Hunter
I suspect that 2 is the best (and probably the fastest) way to do this. If you really don't want to create a table, then try one of these:

function f1(...)
   local n = select("#", ...)
   local b
   b = function(i, f, ...)
       if f ~= nil then
           return false
       elseif i == n then
           return true
       end
       return b(i + 1, ...)
   end
   return b(1, ...)
end

function f2(...)
   for i = 1, select("#", ...) do
       if select(i, ...) ~= nil then
           return false
       end
   end
   return true
end




Tim Hunter wrote:
I have a varargs function. What is the Lua idiomatic way of determining if all the arguments are nil? I've thought of two ways:

1. #{...} == 0
2. next{...} == nil

This function is called very frequently, so I'd prefer not to have to create a table if I can avoid it.


Reply | Threaded
Open this post in threaded view
|

Re: How to determine if all arguments are nil?

Tim Hunter
Michael Broughton wrote:
I suspect that 2 is the best (and probably the fastest) way to do this. If you really don't want to create a table, then try one of these:

Thanks for your advice! I think I'll go with next{...} == nil for simplicity. My preference for avoiding table creation is simply a preference, not a requirement.

Reply | Threaded
Open this post in threaded view
|

Re: How to determine if all arguments are nil?

Romulo Bahiense
In reply to this post by Michael Broughton
Rici Lake wrote:
(...)
>   function foo(a, b, c)
>     if (a or b or c) == nil then
>       -- they're all nil
>     else
>       -- something's in there somewhere
>     end
>   end


Michael Broughton wrote:
(...)
function f2(...)
   for i = 1, select("#", ...) do
       if select(i, ...) ~= nil then
           return false
       end
   end
   return true
end


Perhaps both?

function emptyquad( a, b, c, d )
    return ( a or b or c or d ) == nil
end

local s = select
function emptyn( ... )
    for i = 1, s( "#", ... ), 4 do
        if not emptyquad( s( i, ... ) ) then
            return false
        end
    end
    return true
end

function foo( ... )
    if emptyn( ... ) then
        print("empty")
    else
        print("not empty")
    end
end

The good thing about it is that (IMHO) most functions receive less than four parameters(*), so you won't waste too much with function calls.

I myself would make an C function for this:

int l_emptyn( lua_State *L)
{
    int i, t = lua_gettop( L );
    for ( i = 1; i <= t; i++ )
        if ( !lua_isnil( L, i ) ) return 0;
    lua_pushboolean( L, 1 );
    return 1;
}

--rb


* If it is not your case, you cold tune to your heart's content.