#FEATURE by cg
authorClaus Gittinger <cg@exept.de>
Fri, 09 Nov 2018 21:00:40 +0100
changeset 23526 412ac479842a
parent 23525 79ac77d7c400
child 23527 53033bc58a00
#FEATURE by cg class: Array added: #recursiveCollect:
Array.st
--- a/Array.st	Fri Nov 09 20:55:15 2018 +0100
+++ b/Array.st	Fri Nov 09 21:00:40 2018 +0100
@@ -305,7 +305,6 @@
     "Modified: 23.4.1996 / 15:55:06 / cg"
 ! !
 
-
 !Array methodsFor:'accessing'!
 
 at:index
@@ -1428,6 +1427,29 @@
     "
 !
 
+recursiveCollect:aBlock
+    "return a copy of the receiver where non-array elements
+     are replaced by whatever aBlock returns,
+     and array elements are recursively processed by this method again.
+     Useful to rewrite ui specs on the fly"
+     
+    ^ self collect:[:each |
+        each isArray ifTrue:[
+            each recursiveCollect:aBlock
+        ] ifFalse:[
+            aBlock value:each
+        ]
+     ].
+
+     "
+      #(1 2 3) recursiveCollect:[:el | el == 2 ifTrue:20 ifFalse:el]
+      #(1 2 (1 2 (1 2 (1 2 3) 3) 3) 3) recursiveCollect:[:el | el == 2 ifTrue:20 ifFalse:el]
+      #(1 2 (1 2 (1 2 (1 2 3) 3) 3) 3) recursiveCollect:[:el | el * 2]
+     "
+
+    "Created: / 09-11-2018 / 20:59:58 / Claus Gittinger"
+!
+
 reverseDo:aBlock
     "evaluate the argument, aBlock for each element in the collection in reverse order.
      - reimplemented for speed"