lua-users home
lua-l archive

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


I didn't say it can't already be done. However, what if I want to iterate over something more complex than a table+pairs dataset? The generic for loop is much more powerful than that:
stat ::= for namelist in explist do block end
Any flavor of namelist/explist would require a "specialized generalization" similar to what is described in chapter 3.3.5 of the documentation, to extract the iterator function, for each length of namelist. This would defeat what I am after: a simple way of expressing my intent. I am not saying it's important, just that it would help write expressive code in that kind of situation without having to jump through hoops. After all, we have the numerical for loop in the language:
stat ::= for Name ‘=’ exp ‘,’ exp [‘,’ exp] do block end
Is it important? Maybe not because Lua could offer a range function to perform numerical loops like so, just like it offers pairs()/next() to iterate over table contents:

for i in range(from,to,step) do
    print(i)
end

However the numerical loop exists. (And yes, I understand that I am the devil's advocate here, as the numerical loop saves function calls, so it is beneficial for performance reasons).

Anyway, no big deal. Either Lua authors think it's worthwhile, or not. The most likely is that they already had that idea on their own and decided against it, for reasons of minimalism: don't do it if it's not needed, just like you said. Time to stop the noise :-).


Le mer. 16 mars 2022 à 20:03, Lars Müller <appgurulars@gmx.de> a écrit :

Is there really a need for syntactic sugar here though? There already is 'assert' to simplify your if-condition (erroring instead of printing though):

assert(next(named_args), "no arguments, did you forget something?")
for k, v in pairs(named_args) do print(k, v) end

already seems pretty concise to me. If you want it even shorter (or if you want different side effects), what about using functions to define your own iterator that has a special side effect if the list is empty:

local function arguments(args)
	if next(args) == nil then print"no arguments, did you forget something?" end
	return next, args
end
-- You can then use this as follows:
for k, v in arguments(named_args) do -- this will magically print the error message if the argument table is empty
	print(k, v)
end

You can even generalize this as follows:

local function pairs_else(table, else_clause(...))
	if next(table) == nil then else_clause(...) end
	return next, table
end
for k, v in pairs_else(named_args, print, "no arguments, did you forget something?") do
	print(k, v)
end

In conclusion: There are many options to deduplicate this code. Syntactic sugar doesn't seem necessary at all. Also consider some wrapper function that does the emptiness check of the arguments for you.

On 16.03.22 15:49, Benoit Germain wrote:
Usually it has to do with named arguments processing and catching user errors, like so:

local function foo(...)
   local named_args = ...
   if not next(named_args) then
        print "no arguments, did you forget something?"
     else
         for k,v in pairs(named_args) do
            print(k,v)
        end
     end
end

foo{ oeu='bob', bycoej='cfy'}
foo{}

this would be much cleaner like so:

local function bar(...)
     for k,v in pairs(...) do
        print(k,v)
    else
        print "no arguments, did you forget something?"
    end
end




Le mer. 16 mars 2022 à 15:01, Flyer31 Test <flyer31@googlemail.com> a écrit :
Interesting. Can you say a bit more of the typical possible
applications, just for me as "also Lua newbee" to judge, whether this
REALLY happens often or not?

(I until now never was in such a situation... Usually if I do for
loop, I am always quite sure that something in my container... and if
nothing inside, then I would just be happy that "nothing happens" - so
this standard working principle of Lua has been perfectly nice for me
here ... so far...).

On Wed, Mar 16, 2022 at 9:36 AM Benoit Germain <bnt.germain@gmail.com> wrote:


--
Benoit.


--
Benoit.