--- a/Bag.st Thu Sep 01 17:24:49 2016 +0200
+++ b/Bag.st Thu Sep 01 18:04:52 2016 +0200
@@ -432,6 +432,34 @@
^ contents keysAndValuesDo:aTwoArgBlock
"Modified: 1.3.1996 / 21:42:44 / cg"
+!
+
+valuesAndCountsSelect:aTwoArgBlock
+ "evaluate the block for all distinct elements in the collection,
+ passing both the element and the occurrence count as arguments.
+ If that returns true, add the element to the OrderedCollection.
+ Answer the OrderedCollection.
+
+ WARNING: do not add/remove elements while iterating over the receiver.
+ Iterate over a copy to do this."
+
+ |collected|
+
+ collected := OrderedCollection new.
+ self valuesAndCountsDo:[:eachValue :eachCount|
+ (aTwoArgBlock value:eachValue value:eachCount) ifTrue:[
+ collected add:eachValue
+ ].
+ ].
+ ^ collected
+
+ "
+ #(10 20 20 30 40) asBag
+ valuesAndCountsSelect:[:eachValue :eachCount | eachCount > 1]
+ "
+
+
+ "Modified: 1.3.1996 / 21:42:44 / cg"
! !