#FEATURE by stefan
authorStefan Vogel <sv@exept.de>
Thu, 01 Sep 2016 18:04:52 +0200
changeset 20324 767fdd53f30a
parent 20323 03cd0a52186e
child 20325 d8c3fcfa6a72
#FEATURE by stefan class: Bag added: #valuesAndCountsSelect:
Bag.st
--- 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"
 ! !