[Pharo-project] union intersection difference
How should we handle the mathematical operations union / intersection / difference on different Collections? Right now the implementations are not completely consistent over the different types of collections: (#(1 1 2 2 3) union: #(2 2 3 4 4)) = #(1 2 3 4) asSet (#(1 1 2 2 3) difference: #(2 2 3 4 4)) = #(1 1) (#(1 1 2 2 3) intersection: #(2 2 3 4 4)) = #(2 2 3) I think it would make more sense to deal with this in the mathematical sense and remove duplicates and try to keep the type of the receiver: (#(1 1 2 2 3) union: #(2 2 3 4 4)) = #(1 2 3 4) (#(1 1 2 2 3) difference: #(2 2 3 4 4)) = #(1) (#(1 1 2 2 3) intersection: #(2 2 3 4 4)) = #(2 3) cami
+1 This is the implementation of union: we used to have as an extension in VW: Collection>>union: aSet "Answer the union of the receiver A and aSet B. The union is defined as { x | x in A or x in B }." | stream set | self isEmpty ifTrue: [^Set new]. stream := (self species new: self size) writeStream. "uses a stream to work also for Array." set := Set new. set addAll: aSet. set addAll: self. stream nextPutAll: set. ^stream contents Set>>union: aSet "Answer the union of the receiver A and aSet B. The union is defined as { x | x in A or x in B }." | set | self isEmpty ifTrue: [^aSet copy]. set := Set new. set addAll: aSet. set addAll: self. ^ set Doru On 8 Sep 2011, at 12:49, Camillo Bruni wrote:
How should we handle the mathematical operations union / intersection / difference on different Collections?
Right now the implementations are not completely consistent over the different types of collections:
(#(1 1 2 2 3) union: #(2 2 3 4 4)) = #(1 2 3 4) asSet (#(1 1 2 2 3) difference: #(2 2 3 4 4)) = #(1 1) (#(1 1 2 2 3) intersection: #(2 2 3 4 4)) = #(2 2 3)
I think it would make more sense to deal with this in the mathematical sense and remove duplicates and try to keep the type of the receiver:
(#(1 1 2 2 3) union: #(2 2 3 4 4)) = #(1 2 3 4) (#(1 1 2 2 3) difference: #(2 2 3 4 4)) = #(1) (#(1 1 2 2 3) intersection: #(2 2 3 4 4)) = #(2 3)
cami
-- www.tudorgirba.com "It's not how it is, it is how we see it."
participants (2)
-
Camillo Bruni -
Tudor Girba