container stuff

bearophile bearophileHUGS at lycos.com
Wed May 26 16:48:47 PDT 2010


Jonathan M Davis:
> There is one function, however, which makes no 
> sense to me: removeElement()/stableRemoveElement().

I have a similar function in my dlibs1, it's called pop (works with arrays and AAs), it's similar to the pop method function you find in Python sets and lists.

On lists it removes and returns the last item:

>>> l = [1, 2, 3]
>>> l
[1, 2, 3]
>>> l.pop()
3
>>> l.pop()
2
>>> l.pop()
1
>>> l
[]

But sets are not ordered, so it has to pop out items in a unpredictable way (but if you need items in a truly random order this is not the right thing to do):

>>> s = set(["ab", "cd", "ef"])
>>> s
set(['ab', 'ef', 'cd'])
>>> s.pop()
'ab'
>>> s.pop()
'ef'
>>> s
set(['cd'])
>>> s.pop()
'cd'
>>> s
set([])

So I think this is an useful method. But I think its name is too much long and not clear enough, because it's not a delete, it also returns the item. So I think I'd like to call it just pop()/stablePop() :-)

Bye,
bearophile


More information about the Digitalmars-d mailing list