swarm-support
[Top][All Lists]
Advanced

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

Re: remove list member during a forEach: traversal?


From: Roger M. Burkhart
Subject: Re: remove list member during a forEach: traversal?
Date: Tue, 20 Feb 1996 16:27:31 -0600

> Suppose I have a List alist, and suppose I want to
> send messages to all its members:
> 
>    [alist forEach: M(myMsg)];
> 
> My question is: can myMsg remove from alist a list member 
> to which it was sent (and still have the forEach carry on 
> to process the rest of the items in alist)?
> I was thinking myMsg could have access to alist,
> and perhaps it could include
> 
>    [alist remove: self];
> 
> Will this work?

At present, no.  It will fall afoul of the rule that you can't modify
a list right out from under an index positioned at an affected member.

The best workaround for now is to run your own loop (forEach doesn't
do anything more than automate the loop) with your own index and have
your myMsg remove the member using the index:

  [currentIndex remove];

There will end up being some sort of option to have indexes updated
automatically when change occurs to the collection by other means.
This option, if enabled on the list, would let the internal index of
forEach continue without mishap.  But it would always be a heavier weight
solution than remove on the index would be, however, so I'd always
recommend the more direct solution of working right on the one index.

[alist remove: member] is never particularly fast, since on a plain list
it involves searching the list for a matching member each time from the
beginning.  Support for a fast, direct member remove is there in current
releases (based on membership information stored within each member) but
is moving out of List into a new type OrderedSet since such an option
inherently assumes that a member can be included in a collection only once.

OrderedSet doesn't handle removal of members underneath indexes any better
than lists currently do, so that doesn't help your original question.  But
if you can keep your own index and remove using that, that always works
well on any kind of collection.

Roger


reply via email to

[Prev in Thread] Current Thread [Next in Thread]