Hi Folks.
If I have an OrderedCollection and convert it to Set via asSet, am I guaranteed that the resulting Set contains elements in the same order as the OrderedCollection?
Here is my problem.
I have an OrderedCollection with the same element in it twice. By same element, I mean they are the same object.
*an OrderedCollection( A NESTED TABLE CELL
B A NESTED TABLE CELL
) *
Above, the first and third elements are the same XMLElement. I now this because if I inspect them and then change one, the other changes.
if I run asSet on the OrderedCollection, I get what I need:
*a Set( A NESTED TABLE CELL
B)*
In that conversion, can I rely on that Ordering being preserved?
If not, a pointer to the correct select/reject/inject/detect...sort of thing would be much appreciated.
cheers.
-- Sent from: http://forum.world.st/Squeak-Beginners-f107673.html
Hi tty,
Set does not guarantee the order of elements. It's an implementation detail. If you only need to remove duplicates, have a look at #withoutDuplicates.
Does this help? :-)
Best, Christoph ________________________________ Von: Beginners beginners-bounces@lists.squeakfoundation.org im Auftrag von tty gettimothy@zoho.com Gesendet: Dienstag, 11. Februar 2020 16:07:15 An: beginners@lists.squeakfoundation.org Betreff: [Newbies] Does Set respect ordering?
Hi Folks.
If I have an OrderedCollection and convert it to Set via asSet, am I guaranteed that the resulting Set contains elements in the same order as the OrderedCollection?
Here is my problem.
I have an OrderedCollection with the same element in it twice. By same element, I mean they are the same object.
*an OrderedCollection( A NESTED TABLE CELL
B A NESTED TABLE CELL
) *
Above, the first and third elements are the same XMLElement. I now this because if I inspect them and then change one, the other changes.
if I run asSet on the OrderedCollection, I get what I need:
*a Set( A NESTED TABLE CELL
B)*
In that conversion, can I rely on that Ordering being preserved?
If not, a pointer to the correct select/reject/inject/detect...sort of thing would be much appreciated.
cheers.
-- Sent from: http://forum.world.st/Squeak-Beginners-f107673.html _______________________________________________ Beginners mailing list Beginners@lists.squeakfoundation.org http://lists.squeakfoundation.org/mailman/listinfo/beginners
The implementation of withoutDuplicates is brilliant.
*withoutDuplicates "Answer a copy of the receiver that preserves order but eliminates any duplicates." | seen | seen := Set new: self size. ^self select: [:each| seen ifAbsentAdd: each]*
The Set acts as a growing "mask" of what is allowed in the OrderedCollection returned by select.
I have never seen this idiom. Truly inspiring.
-- Sent from: http://forum.world.st/Squeak-Beginners-f107673.html
Hi,
The "set" is a mathematical object which is defined by what it contains, there isn't any order concept in it.
So, by definition of set: {1,2,3} = {2,3,1}
It makes not even sense to ask what is the first element of a set, because there is no order in it.
So, I guess Christoph is giving you the right hint, keep working with OrderedCollection.
Then, you may open your Squeak, on top of the Window select Help -> Terse Guide To Squeak and check ouy "Bag" and "Set" sections.
bye n.
On 2/11/20 7:11 AM, Thiede, Christoph wrote:
Hi tty,
Set does not guarantee the order of elements. It's an implementation detail. If you only need to remove duplicates, have a look at #withoutDuplicates.
Does this help? :-)
Best, Christoph
*Von:* Beginners beginners-bounces@lists.squeakfoundation.org im Auftrag von tty gettimothy@zoho.com *Gesendet:* Dienstag, 11. Februar 2020 16:07:15 *An:* beginners@lists.squeakfoundation.org *Betreff:* [Newbies] Does Set respect ordering? Hi Folks.
If I have an OrderedCollection and convert it to Set via asSet, am I guaranteed that the resulting Set contains elements in the same order as the OrderedCollection?
Here is my problem.
I have an OrderedCollection with the same element in it twice. By same element, I mean they are the same object.
*an OrderedCollection( A NESTED TABLE CELL
B A NESTED TABLE CELL
)
Above, the first and third elements are the same XMLElement. I now this because if I inspect them and then change one, the other changes.
if I run asSet on the OrderedCollection, I get what I need:
*a Set( A NESTED TABLE CELL
B)*
In that conversion, can I rely on that Ordering being preserved?
If not, a pointer to the correct select/reject/inject/detect...sort of thing would be much appreciated.
cheers.
-- Sent from: http://forum.world.st/Squeak-Beginners-f107673.html _______________________________________________ Beginners mailing list Beginners@lists.squeakfoundation.org http://lists.squeakfoundation.org/mailman/listinfo/beginners
Beginners mailing list Beginners@lists.squeakfoundation.org http://lists.squeakfoundation.org/mailman/listinfo/beginners
beginners@lists.squeakfoundation.org