#DOCUMENTATION by cg
authorClaus Gittinger <cg@exept.de>
Wed, 09 May 2018 00:04:21 +0200
changeset 22806 11fda09c16f7
parent 22805 bcf1140358c8
child 22807 2344fc7a4835
#DOCUMENTATION by cg class: Collection added: #readingElementsDo: comment/format in: #withIndexDo:
Collection.st
--- a/Collection.st	Tue May 08 23:50:57 2018 +0200
+++ b/Collection.st	Wed May 09 00:04:21 2018 +0200
@@ -368,7 +368,6 @@
     ^ self
 ! !
 
-
 !Collection methodsFor:'Compatibility-ANSI'!
 
 identityIncludes:anObject
@@ -3644,6 +3643,33 @@
     "Created: / 20-07-2011 / 00:54:41 / cg"
 !
 
+readingElementsDo:aBlock
+    "Create a read stream on the receiver, evaluate aBlock, passing that stream as arg,
+     and return the block's value.
+     This is performing much like fileName readingFileDo:.
+     The difference to the well known do: enumerators is that the block can decide
+     if more than one element is to be read (eg. to skip elements)."
+
+    |stream|
+
+    stream := self readStream.
+    ^ aBlock value:stream optionalArgument:[:ret | ^ ret].
+
+    "
+     increasing :=
+        #(10 20 15 30 40) readingElementsDo:[:s :exit |
+            |el1 el2|
+            
+            [s atEnd] whileFalse:[
+                el1 := s next.
+                el2 := s peek.
+                (el2 notNil and:[ el1 > el2]) ifTrue:[exit value:false]
+            ].
+            true
+        ].    
+    "
+!
+
 reduce:binaryBlock
     "Evaluate the block with the first two elements of the receiver,
      then with the result of the first evaluation and the next element,
@@ -4226,8 +4252,8 @@
      passing both element and index as arguments.
      Same as doWithIndex:, due to parallel evolution of different Smalltalk dialects"
 
-    ^ self keysAndValuesDo:[:key :index |
-        aTwoArgBlock value:index value:key
+    ^ self keysAndValuesDo:[:key :value |
+        aTwoArgBlock value:value value:key
     ].
 ! !