May 27, 2010 Re: container stuff | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Don | Don:
> Yes, but if I understand correctly, the only reason to have removeAny _as a primitive_ is for speed. And iterating over the container followed by a single removal is almost always going to be much faster.
Most things in Python are designed to be handy first, and fast later. So I doubt performance has has any significance in this small piece of Python design.
In both Python and D is pop() is useful because it allows your collection to represent coherently its decreased or increased number of items at all times (Andrei ha shown an example where you have to add and remove items continuously).
Bye,
bearophile
| |||
May 27, 2010 Re: container stuff | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Don | On Thu, May 27, 2010 at 5:44 AM, Don <nospam@nospam.com> wrote:
> bearophile wrote:
>>
>> Don:
>>>
>>> When is it better to do it that way, rather than just iterating over all
>>> elements, and then completely empty the container?
>>> (Just curious -- I'm having trouble thinking of a use case for this
>>> feature).
>>
>> I'm having troubles understanding why two persons have troubles seeing use cases for this feature :-)
>>
>> Iterating over the container and then emptying the container is two operations, you have to keep in mind to empty it, while if you pop items out of it progressively you just need to keep in mind to do one thing, and you avoid forgetting the final cleaning.
>
> Yes, but if I understand correctly, the only reason to have removeAny _as a primitive_ is for speed. And iterating over the container followed by a single removal is almost always going to be much faster.
>
> If, however, speed is not critical, removeAny can be a generic function --
> call removeFront() if present, else call removeBack().
> And your examples would work just fine with that.
>
> I'm having trouble identifying a use case where it needs to be a primitive.
>
Think of a graph algorithm where you add all the nodes you know about to a Set. Pop one, process it, and then add any nodes it's connected to that you haven't seen yet back to the Set. Repeat until nothing left to pop.
--bb
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply