Collection.st
changeset 22445 ff0ebbd11d41
parent 22334 8e2e3cb23910
child 22554 04becee61780
--- a/Collection.st	Wed Jan 17 15:30:31 2018 +0100
+++ b/Collection.st	Thu Jan 18 15:19:13 2018 +0100
@@ -3196,6 +3196,33 @@
     "Modified: / 22-01-2011 / 09:12:22 / cg"
 !
 
+flatDoWithParent:aBlock
+    "for each element of the collection, if it's a scalar, evaluate aBlock for it;
+     otherwise, recursively invoke flatWithParentDo: on the collection.
+     The block is called with two arguments, the element itself and its parent (owner),
+     thus implementing a depth-first enumeration"
+
+    self do:[:each |
+        (each isNonByteCollection) ifTrue:[
+            each flatDoWithParent:aBlock
+        ] ifFalse:[
+            aBlock value:each value:self
+        ].
+    ].
+
+    "
+     #(
+        (1 2 3)
+        4 5
+        (6)
+        7
+        (8 (9 10) 11 12 (13 (14 (15) 16)))
+     ) flatDoWithParent:[:el :parent | Transcript showCR:(parent -> el)]
+    "
+
+    "Modified: / 22-01-2011 / 09:12:22 / cg"
+!
+
 fold: binaryBlock
     "Evaluate the block with the first two elements of the receiver,
      then with the result of the first evaluation and the next element,