[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: table library changes (was Re: table.new in 5.3?)
- From: Kevin Martin <kev82@...>
- Date: Mon, 25 Nov 2013 20:03:37 +0000
On 25 Nov 2013, at 15:24, "John Hind" <firstname.lastname@example.org> wrote:
> The need for that dummy variable to receive the index rather spoils the 'niceness' IMHO!
I don't think it does, because throwing away certain arguments, using _ as a dummy variable is a pretty standard technique within Lua, and is really required in any system with multiple return values. If it is ugly (which I disagree with) why is it any more ugly for use in ipairs than anywhere else?
> for element in elements(list) do
What about that tells you that the elements are ordered, or not ordered. I have a clear association in my head that ipairs is ordered and pairs is not. Similarly, does elements fetch values with non-natural indices?
> Why privilege the one specific case 'ipairs' does - what about indexing the list in reverse, or between two limits? Pretty soon you are falling back on numeric 'for' anyway because 'ipairs' does not do exactly what you want.
I think an examination of real life code would be interesting, because I don't agree that the usage frequencies make your argument.
I've just run the following in one particular project of mine and got these results:
kev82@wheezy2:~/lc$ grep -R 'for.*ipairs.*do' 2>/dev/null | wc -l
kev82@wheezy2:~/lc$ grep -R 'for.*#.*do' 2>/dev/null | wc -l
Of the 6 that use #t, 4 of them are for i=1,#t do, which is the same as ipairs.
So that's 112 for the ipairs use case, vs 2 for other cases. Personally, in my case, I'm not falling back to the numeric for that often.
Maybe ipairs would be redundant with your iterator patch (which admittedly I haven't looked at), but in standard Lua, I definitely think it fulfils a role over for i=1,#t
> These are certainly good cases for specialised iterators, but why implement them by hijacking 'ipairs'? Again, write your own iterator factory and call it something appropriate.
Because in standard Lua, I don't have your patch, but I do have ipairs.
I'm not saying that ipairs is the best solution, only that in standard Lua, without your patch, it is not redundant.
However, this message has been the kick I need and when I have some time available, I will be giving some of the power patches a go.