Log In  


Hey! Remember when I suggested putting next in the globals, since we could get to it anyway by doing next=pairs{}? You did! And that's been great!

Would it be okay to get inext the same way? It seems to be present, since I can get it by (similarly) doing inext=ipairs{}, and it seems to work the same way, just limited to index,value instead of key,value.

It's just not officially exposed.

I'm asking because there's a token saved when you switch from the first to the second implementation here:

for k,v in pairs(t) do ... end
for i,v in ipairs(t) do ... end

-- these are a little less clear to the reader but they work the same and save a token
for k,v in next,t do ... end
for i,v in inext,t do ... end

Technically it also shaves a couple of cycles off of the loop setup by inlining the return tuple from pairs()/ipairs(), but it's really the token savings that would be handy in some cases.

P#109951 2022-04-09 09:47 ( Edited 2022-04-09 10:57)

"inext" requires an explicit 0 as its 2nd argument (nil doesn't work), so iterating over inext,t won't work - you'd need to use inext,t,0

P#109970 2022-04-09 17:16 ( Edited 2022-04-09 17:16)


Ah, you're right. Mind you, PICO-8 has a penchant for treating nil as 0 when it makes sense, and in this case it makes sense, no? Perhaps the PICO-8 version of inext() could accept a nil in lieu of a 0...? ;)


(honestly, that would just make it orthogonal with next(), which also has to detect a nil starting arg)

P#110074 2022-04-11 20:18 ( Edited 2022-04-11 20:19)

I can't see why not. The only reservation I have is that "for i,v in inext,t" is a slightly less familiar way to write it, but -- same for next, and any cart that cares enough to do this will likely have much scarier things in it. I do like the efficiency gains and giving it the same official status as next.

Treating nil as 0 seems totally reasonable to me. In fact -- I wonder if there is a reason that's not the case in standard Lua. I guess we'll find out the hard way!

It's currently operational in the web builds -- you can try it at pico-8-edu.com, and unless someone can point out how this is going to end badly, I'll leave it in for 0.2.4d.

P#110441 2022-04-19 02:51

Now I would like that, @zep. For NIL to be optionally treated as zero. It could be done with a command. nil=0 or defnil(0)

Of course for that matter logic would be good too.


P#110443 2022-04-19 02:54

Woo! Cheers, zep! :)

Also, you're right. Standard Lua should do the same. I bet it's an oversight.

P#110483 2022-04-19 14:05 ( Edited 2022-04-19 14:06)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-04-20 00:09:33 | 0.015s | Q:17