#BUGFIX by stefan
class: Collection
comment/format in:
#union:
#xor:
changed:
# #difference:
#intersect:
#select: (send #speciesForAdding instead of #species)
make set operations work with Dictionaries
--- a/Collection.st Fri Jan 20 19:55:46 2017 +0100
+++ b/Collection.st Fri Jan 20 19:59:28 2017 +0100
@@ -343,7 +343,6 @@
^ self
! !
-
!Collection methodsFor:'Compatibility-ANSI'!
identityIncludes:anObject
@@ -426,11 +425,13 @@
difference: aCollection
"Answer the set-theoretic difference of two collections."
- ^ self reject:[:each | aCollection includes: each]
+ ^ self \ aCollection
"
#(0 2 4 6 8) difference:#(2 4)
"
+
+ "Modified: / 20-01-2017 / 19:21:35 / stefan"
!
gather:aBlock
@@ -3553,7 +3554,7 @@
|newCollection|
- newCollection := self species new.
+ newCollection := self speciesForAdding new.
self do:[:each |
(aBlock value:each) ifTrue:[newCollection add:each].
].
@@ -3565,6 +3566,7 @@
"
"Modified: / 07-08-2010 / 16:26:40 / cg"
+ "Modified: / 20-01-2017 / 17:42:33 / stefan"
!
select:aBlock as:aCollectionClass
@@ -5281,43 +5283,36 @@
which are NOT also contained in the aCollection
For large collections you better use a Set for aCollection"
-
- |newCollection|
-
- newCollection := self speciesForAdding new.
- self do:[:element |
- (aCollection includes:element) ifFalse:[
- newCollection add:element
- ]
- ].
- ^ newCollection
+ ^ self select:[:eachElement | (aCollection includes:eachElement) not].
"
#(0 1 2 3 4 5 6 7 8 9) \ #(1 2 3) asSet
#(0 1 2 3 4 5 6 7 8 9) \ #(1 2 3)
- ('hello' \ 'l') asString
- "
+ ('hello' \ 'l')
+
+ (Dictionary withKeysAndValues:#(1 'uno' 2 'due' 3 'tre' 4 'quatro'))
+ \ (Dictionary withKeysAndValues:#(1 'uno' 4 'quatro'))
+ "
+
+ "Modified (comment): / 20-01-2017 / 19:28:00 / stefan"
!
intersect:aCollection
"return a new set containing all elements of the receiver,
which are also contained in the argument collection.
- For large collections you better use a Set for self"
-
- |newCollection|
-
- newCollection := self speciesForAdding new.
- aCollection do:[:element |
- (self includes:element) ifTrue:[
- newCollection add:element
- ]
- ].
- ^ newCollection
+ For large collections you better use a Set for aCollection"
+
+ ^ aCollection select:[:eachElement | self includes:eachElement].
"
#(0 1 2 3 4 5 6 7 8 9) asSet intersect:#(1 2 3 11)
#(0 1 2 3 4 5 6 7 8 9) intersect:#(1 2 3 11)
- "
+
+ (Dictionary withKeysAndValues:#(1 'uno' 2 'due' 3 'tre' 4 'quatro'))
+ intersect:(Dictionary withKeysAndValues:#(1 'uno' 4 'quatro' 5 'cinque'))
+ "
+
+ "Modified: / 20-01-2017 / 19:33:14 / stefan"
!
union:aCollection
@@ -5334,7 +5329,12 @@
"
#(0 2 4 6 8) union:#(1 3 5 7)
#(0 2 4 6 8) union:#(0 1 3 5 7)
- "
+
+ (Dictionary withKeysAndValues:#(1 'uno' 2 'due' 3 'tre' 4 'quatro'))
+ union:(Dictionary withKeysAndValues:#(1 'uno' 4 'quatro' 5 'cinque'))
+ "
+
+ "Modified (comment): / 20-01-2017 / 19:35:23 / stefan"
!
xor:aCollection
@@ -5361,6 +5361,9 @@
"
#(0 1 2 3 4 5 6 7 8 9) xor:#(1 2 3 11)
+ (Dictionary withKeysAndValues:#(1 'uno' 2 'due' 3 'tre' 4 'quatro'))
+ xor:(Dictionary withKeysAndValues:#(1 'uno' 4 'quatro' 5 'cinque'))
+
"
"
@@ -5371,6 +5374,8 @@
c1 symmetricDifference:c2.
self assert:(c1 symmetricDifference:c2) asSet = (c2 symmetricDifference:c1) asSet
"
+
+ "Modified (comment): / 20-01-2017 / 19:37:58 / stefan"
! !
!Collection methodsFor:'sorting & reordering'!