#FEATURE by cg
authorClaus Gittinger <cg@exept.de>
Fri, 11 May 2018 14:21:33 +0200
changeset 22905 cf3ee274d51d
parent 22904 19e24d06132f
child 22906 68b93ca8fbec
#FEATURE by cg class: Collection added: #do:separatedBy:afterEachCount:do:
Collection.st
--- a/Collection.st	Fri May 11 13:52:50 2018 +0200
+++ b/Collection.st	Fri May 11 14:21:33 2018 +0200
@@ -297,8 +297,6 @@
     ^ self newWithSize:n
 ! !
 
-
-
 !Collection class methodsFor:'Signal constants'!
 
 emptyCollectionSignal
@@ -377,7 +375,6 @@
     ^ self includesIdentical:anObject.
 ! !
 
-
 !Collection methodsFor:'Compatibility-Squeak'!
 
 , aCollection
@@ -538,7 +535,6 @@
     ^ self ifEmpty:ifEmptyValue ifNotEmpty:ifNotEmptyValue
 ! !
 
-
 !Collection methodsFor:'accessing'!
 
 anElement
@@ -2986,6 +2982,66 @@
     "Modified: / 11.2.2000 / 11:23:15 / cg"
 !
 
+do:aBlock separatedBy:betweenBlock afterEachCount:afterEachCount do:afterEachBlock
+    "evaluate the argument, aBlock for each element.
+     Evaluate betweenBlock except after each count iteration and after the last,
+     Instead, after each count, but not at the end, the afterEachBlock is evaluated.
+     This is a utility helper for collection printers
+     (for example, to print a space between elements and a newline after each group)."
+
+    |first groupCount|
+
+    groupCount := 0.
+    first := true.
+    self do:[:element |
+        groupCount == afterEachCount ifTrue:[
+            afterEachBlock value.
+            groupCount := 0.
+        ] ifFalse:[
+            first ifFalse:[
+                betweenBlock value.
+            ].
+        ].
+        aBlock value:element.
+        first := false.
+        groupCount := groupCount + 1.
+    ].
+
+    "
+     #(1 2 3 4 5 6 7 8) 
+        do:[:el | Transcript show:el]
+        separatedBy:[ Transcript show:'-']
+        afterEachCount:3
+        do:[Transcript cr]
+
+     #(1 2 3 4 5 6 7 8) 
+        do:[:el | Transcript show:el]
+        separatedBy:[ Transcript show:'-']
+        afterEachCount:2
+        do:[Transcript cr]
+
+     #(1 2 3 4) 
+        do:[:el | Transcript show:el]
+        separatedBy:[ Transcript show:'-']
+        afterEachCount:5
+        do:[Transcript cr]
+
+     #() 
+        do:[:el | Transcript show:el]
+        separatedBy:[ Transcript show:'-']
+        afterEachCount:5
+        do:[Transcript cr]
+
+     #(1) 
+        do:[:el | Transcript show:el]
+        separatedBy:[ Transcript show:'-']
+        afterEachCount:5
+        do:[Transcript cr]
+    "
+
+    "Modified: / 11.2.2000 / 11:23:15 / cg"
+!
+
 do:aBlock whileTrue:whileBlock
     "evaluate the argument, aBlock for each element until whileBlock
      evaluates to false.
@@ -6233,7 +6289,6 @@
     ^ aVisitor visitCollection:self with:aParameter
 ! !
 
-
 !Collection class methodsFor:'documentation'!
 
 version