Collection.st
changeset 15721 8fa10d0e174c
parent 15612 d79538db6bd6
child 15722 a2f38668b888
--- a/Collection.st	Thu Aug 29 11:51:47 2013 +0200
+++ b/Collection.st	Thu Aug 29 11:52:54 2013 +0200
@@ -259,7 +259,6 @@
     ^ self withSize:n
 ! !
 
-
 !Collection class methodsFor:'Signal constants'!
 
 emptyCollectionSignal
@@ -531,7 +530,6 @@
     ].
 ! !
 
-
 !Collection methodsFor:'accessing'!
 
 anElement
@@ -2219,6 +2217,34 @@
     "
 !
 
+collect:collectBlock thenSelect:selectBlock as:aCollectionClass
+    "first apply collectBlock to each element, then pass the result to
+     selectBlock. 
+     Return a new collection with all elements from the receiver, 
+     for which the selectBlock evaluates to true.
+     Returns the same as if two separate collect+select messages were sent,
+     but avoids the creation of intermediate collections, so this is nicer for
+     big collections."
+
+    |newCollection|
+
+    newCollection := aCollectionClass new.
+    self do:[:each |
+        |rslt|
+
+        rslt := collectBlock value:each.
+        (selectBlock value:rslt) ifTrue:[newCollection add:rslt].
+    ].
+    ^ newCollection
+
+    "
+     #(1 2 3 4) select:[:e | e odd] thenCollect:[:e| e*e] as:OrderedCollection  
+     (1 to:10) select:[:e | e even] thenCollect:[:e| e*e] as:IdentitySet       
+    "
+
+    "Created: / 29-08-2013 / 09:56:20 / cg"
+!
+
 collectAll:aBlock
     "for each element in the receiver, evaluate the argument, aBlock.
      The block is supposed to return a collection, whose elements are collected.
@@ -3048,7 +3074,10 @@
 select:selectBlock thenCollect:collectBlock as:aCollectionClass
     "return a new collection with all elements from the receiver, for which
      the argument selectBlock evaluates to true.
-     Process the elements throgh collectBlock before adding."
+     Process the elements throgh collectBlock before adding.
+     Returns the same as if two separate collect+select messages were sent,
+     but avoids the creation of intermediate collections, so this is nicer for
+     big collections."
 
     |newCollection|
 
@@ -4842,11 +4871,11 @@
 !Collection class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/Collection.st,v 1.309 2013-08-10 11:14:45 stefan Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/Collection.st,v 1.310 2013-08-29 09:52:54 cg Exp $'
 !
 
 version_CVS
-    ^ '$Header: /cvs/stx/stx/libbasic/Collection.st,v 1.309 2013-08-10 11:14:45 stefan Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/Collection.st,v 1.310 2013-08-29 09:52:54 cg Exp $'
 ! !