#REFACTORING by cg
authorClaus Gittinger <cg@exept.de>
Wed, 05 Jun 2019 18:35:08 +0200
changeset 24256 6dfccbe099d3
parent 24255 82a37ed5a27c
child 24257 3d3652e1f81c
#REFACTORING by cg class: Collection added: #writeStream class: Collection class added: #collect:usingEnumerator:
Collection.st
--- a/Collection.st	Wed Jun 05 18:33:16 2019 +0200
+++ b/Collection.st	Wed Jun 05 18:35:08 2019 +0200
@@ -102,6 +102,32 @@
 
 !Collection class methodsFor:'instance creation'!
 
+collect:aCollection usingEnumerator:aBlockOrEnumeratorSelector
+    "apply aBlock or enumeratorSelector to the receiver
+     and collect the enumerated elements.
+     If the enumerator is a symbol, it should be the name of an enumerator method (i.e. do:, reverseDo:, etc.).
+     If it is a block, it should be a two-arg block, expecting the collection first, and
+     a block to be applied to each element.
+     Can be used if the collection needs to be enumerated with a different enumerator
+     (eg. a tree, which implements aka. childrenDo:)"
+
+    |newCollection|
+
+    newCollection := self streamContents:[:s |
+        (aBlockOrEnumeratorSelector value:aCollection value:[:each | s nextPut:each])
+    ].
+    ^ newCollection
+
+    "
+     OrderedCollection collect:#(1 2 3 4 5) usingEnumerator:#do:
+     OrderedCollection collect:#(1 2 3 4 5) usingEnumerator:#reverseDo:
+     OrderedCollection collect:#(1 2 3 4 5) usingEnumerator:[:coll :block | coll do:block]
+     OrderedCollection collect:#(1 2 3 4 5) usingEnumerator:[:coll :block | coll reverseDo:block]
+    "
+
+    "Created: / 05-06-2019 / 14:18:29 / Claus Gittinger"
+!
+
 combiningEach:collection1 withEach:collection2 using:aTwoArgBlock
     "evaluate aTwoArgBlock for each combination of elements from collection1
      and collection2 and return an instance of the receiver containing all those elements"
@@ -360,6 +386,7 @@
     ^ self == Collection
 ! !
 
+
 !Collection methodsFor:'Compatibility-ANSI'!
 
 identityIncludes:anObject