SeqColl.st
changeset 356 6c5ce0e1e7a8
parent 348 5ac1b6b43600
child 359 b8df66983eff
--- a/SeqColl.st	Fri May 19 15:33:11 1995 +0200
+++ b/SeqColl.st	Wed May 24 14:44:58 1995 +0200
@@ -21,7 +21,7 @@
 COPYRIGHT (c) 1989 by Claus Gittinger
 	      All Rights Reserved
 
-$Header: /cvs/stx/stx/libbasic/Attic/SeqColl.st,v 1.28 1995-05-18 15:10:08 claus Exp $
+$Header: /cvs/stx/stx/libbasic/Attic/SeqColl.st,v 1.29 1995-05-24 12:44:09 claus Exp $
 '!
 
 !SequenceableCollection class methodsFor:'documentation'!
@@ -42,7 +42,7 @@
 
 version
 "
-$Header: /cvs/stx/stx/libbasic/Attic/SeqColl.st,v 1.28 1995-05-18 15:10:08 claus Exp $
+$Header: /cvs/stx/stx/libbasic/Attic/SeqColl.st,v 1.29 1995-05-24 12:44:09 claus Exp $
 "
 !
 
@@ -1771,6 +1771,28 @@
     "
 !
 
+pairWiseDo:aBlock
+    "evaluate the argument, aBlock for every pair of elements in the collection.
+     The block is called with 2 arguments for each 2 elements in the receiver.
+     An error will be reported, if the number of elements in the receiver
+     is not a multiple of 2."
+
+    |stop "{ Class:SmallInteger }"|
+
+    stop := self size.
+    1 to:stop by:2 do:[:index |
+	aBlock value:(self at:index) value:(self at:index+1).
+    ]
+    "
+     #(1 one 2 two 3 three 4 four 5 five 6 six) 
+     pairWiseDo:[:num :sym | Transcript show:num; show:' is: '; showCr:sym]
+
+
+     #(1 1  1 2  1 3  1 4  1 5) 
+     pairWiseDo:[:x :y | Transcript showCr:x@y]
+    "
+!
+
 keysAndValuesDo:aTwoArgBlock
     "evaluate the argument, aBlock for every element in the collection,
      passing both index and element as arguments."
@@ -1876,6 +1898,34 @@
     "
 !
 
+pairWiseCollect:aBlock
+    "evaluate the argument, aBlock for every pair of elements in the collection.
+     The block is called with 2 arguments for each 2 elements in the receiver.
+     An error will be reported, if the number of elements in the receiver
+     is not a multiple of 2.
+     Collect the results and return a new collection containing those."
+
+    |stop newCollection dstIdx "{ Class:SmallInteger }"|
+
+    stop := self size.
+    newCollection := self copyEmptyAndGrow:stop // 2.
+    dstIdx := 1.
+    1 to:stop by:2 do:[:index |
+	newCollection at:dstIdx put:(aBlock value:(self at:index) value:(self at:index+1)).
+	dstIdx := dstIdx + 1
+    ].
+    ^ newCollection
+
+    "
+     #(1 one 2 two 3 three 4 four 5 five 6 six) 
+     pairWiseCollect:[:num :sym | sym->num] 
+
+
+     #(1 1  1 2  1 3  1 4  1 5) 
+     pairWiseCollect:[:x :y | x@y] 
+    "
+!
+
 from:start to:stop collect:aBlock
     "evaluate the argument, aBlock for the elements indexed by start
      to stop in the collection and return a collection of the results"