lua-users home
lua-l archive

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

2013/11/1 Hisham <>:

> A simple idea crossed my mind and I thought some of you might enjoy,
> so I decided to share.
> After a number of times when I had to iterate arrays, mark some
> elements for removal, and then remove them in a second loop, I wished
> I'd be able to do it in one go.

The second-loop method is O(n).

> ipairs_remove returns the index and value as usual, plus a "remove"
> function that removes the current element from the array and adjusts
> the iteration so it keeps going (if you table.remove() the current
> element during an ipairs iteration, you end up skipping the next
> item). It never occured to me before to return a utility function like
> this through the iterator; it seems an elegant solution.

Elegant, but O(n^2).