#FEATURE by cg
authorClaus Gittinger <cg@exept.de>
Wed, 09 Jan 2019 17:39:50 +0100
changeset 4780 2d9224d441a6
parent 4779 9832d89b9543
child 4781 a4d11bd443dc
#FEATURE by cg class: Iterator changed: #collect:as: care for fixedSize result collection (i.e. Array)
Iterator.st
--- a/Iterator.st	Sat Dec 22 04:37:15 2018 +0000
+++ b/Iterator.st	Wed Jan 09 17:39:50 2019 +0100
@@ -1,3 +1,5 @@
+"{ Encoding: utf8 }"
+
 "
  COPYRIGHT.
  The above file is a Manchester Goodie protected by copyright.
@@ -312,22 +314,39 @@
 !Iterator methodsFor:'enumerating'!
 
 collect:aBlock as:aClass
-    "Reimplemented here as Iterator does not support #size"
+    "Reimplemented here as Iterator does not support #size.
+     aClass must be a non-fixed collection."
 
-    |newCollection|
+    |newCollection sz|
 
     newCollection := aClass new.
+    newCollection isFixedSize ifFalse:[
+        newCollection := OrderedCollection new.
+        self do:[:el |
+            newCollection add:(aBlock value:el).
+        ].
+        sz := newCollection size.
+        ^ ((aClass new:sz) grow:sz) replaceFrom:1 with:newCollection.  
+    ].
+    
     self do:[:el |
         newCollection add:(aBlock value:el).
     ].
     ^ newCollection
 
     "
-     #(one two three four five six) collect:[:element | element asUppercase] as:OrderedCollection
-     'abcdef' collect:[:char | char digitValue] as:ByteArray
+     (Iterator on:[:whatToDo| 'abcd' do:whatToDo]) 
+        collect:[:element | element asUppercase] as:OrderedCollection
+
+     (Iterator on:[:whatToDo| 'abcd' do:whatToDo]) 
+        collect:[:element | element asUppercase] as:Array
+
+     (Iterator on:[:whatToDo| '1234' do:whatToDo]) 
+        collect:[:char | char digitValue] as:ByteArray
     "
 
     "Created: / 21-10-2013 / 16:07:53 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 09-01-2019 / 17:41:50 / Claus Gittinger"
 !
 
 detectLast:aBlock ifNone:anExceptionValue