Bag.st
changeset 20324 767fdd53f30a
parent 19081 fce3c0a198c4
child 20345 68e5382ae472
child 20628 8679e14fa67d
--- 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"
 ! !