Imagine the state of the world if Brendan Eich had known about Lua and spent those nine days integrating that into Netscape. I believe it had a compatible license, even back in 1995.
Ah, Lua... That programming language that still invites you to implement a split function a zillion ways and get it wrong: http://lua-users.org/wiki/SplitJoin
Found this out when I had to implement a snippet in nginx... Avoided at all costs since then.
Ah, lua-users.org... That wiki which pretty much looks like the comment section in the PHP manual but with a less chance to be correct for many reasons. And I'm not sure if there's other (preferably more trusted) source of snippets.
Back in time when I messed with Lua [1], I needed a version of `pairs` that returns keys in the sorted order. Lua-users has a page for that [2], so I looked it up, and found that it has a subtle but important enough bug---it relies on `__orderedIndex` stored in the original table, which never gets updated nor GCed. I'm sure many enough people had been and will be tripped up by this snippet.
` t.__orderedIndex = nil` looks like it cleans up at the end of the iteration, am I missing something? Mutating a table while iterating over it is fairly hazardous anyway
Ah, yeah, the problem won't be visible with a normal loop. However if you break out of the loop without hitting the very last key, or remove a key during the iteration [1], or loop against the same table inside another loop, then the snippet will fail in unexpected ways.
The correct way to do this is as follows. First you collect all keys and sort them as usual, and then return an iterator function that contains those keys as an upvalue. Essentially you need to create a single-use `pairs` function that holds all necessary states.
[1] You can't insert a key during the iteration, but removing an existing key is guaranteed to work correctly, no matter if you've already past the deleted key or not: https://www.lua.org/manual/5.4/manual.html#pdf-next
Despite being linked on the website, no one should consider Lua Users wiki as any sort of canonical source of information. If you have questions use the mailing list, not that splitting a string is something that should require too much thinking, nor should we pretend there aren't a handful of mature utility libraries should you not be inclined to write a few lines of code yourself.
There's alot of similarities - weak dynamic typing, closures from day one, both came with mechanisms to do something like self-style prototype OO, only have a double for a numeric data type, both fairly small langs designed for "simple" scripting (though JS has grown).
Lua is wonderful language, especially if you need to embed it within some asynchronous workflow. Built an API gateway long time back, which used async network io and it was a pain to allow ability to modify request/response without providing state saving and callbacks. Once integrated with Lua, it became possible to give users the illusion of blocking, single threaded code which was taken off the thread and restarted with complete call stack once it was ready for scheduling. Even wrote one memcached replacement scriptable object cache. https://github.com/iamrohit/cacheismo