lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 2009-12-31, steve donovan <steve.j.donovan@gmail.com> wrote:
> On Thu, Dec 31, 2009 at 3:44 PM, Leo Razoumov <slonik.az@gmail.com> wrote:
>  > I wish that "map" would convert nil to false before pushing it into
>  > resultant array so that it becomes {10, false, 20, false} avoiding
>  > holes and preserving the length.
>
>
> So 'false' becomes a way to represent holes? But I don't see why it is
>  crucial to preserve length of the result.
>

It is not for the purpose of preserving the length of the result but
rather to preserve the iteration order. Using "pair" on Lua table does
not guarantee the order of iteration. "ipair" while guaranteeing the
order of iteration will choke on the first "nil" value. Replacing
"nil" with "false" allows to iterate with "ipair" over all the results
from "map" and skip the "false" values using "if" conditionals.

--Leo--