Iterating Sparse Arrays in Lua 5.1
The Lua programming language is a scripting language that is light-weight (in terms of both syntax and size), fast, and easily embedded. I won’t spend a lot of time introducing Lua here (though I may do that in another article). If you haven’t used it, but you’re interested in getting started, a good place to look is the well-written Lua documentation.
I’m currently writing a library of higher-order functions in Lua called lua-functional. One of the early challenges was how to allow iteration over sparse arrays. Out of the box, Lua doesn’t stops iteration after reaching the first nil value. This article explores a two possible solutions to this problem and their consequences.