Log In  


@zep

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.

1


"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


@thisismypassword

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...? ;)

@zep?

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


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.


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.

x=x-_logic(btnp(0))+_logic(btnp(1))


Woo! Cheers, zep! :)

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



[Please log in to post a comment]