Collection.st
changeset 16777 6be6dfd33ec6
parent 16753 567f6b68e6e9
child 16778 554ad879a1c9
--- a/Collection.st	Tue Jul 15 11:37:25 2014 +0200
+++ b/Collection.st	Tue Jul 15 11:47:43 2014 +0200
@@ -267,7 +267,6 @@
     ^ self newWithSize:n
 ! !
 
-
 !Collection class methodsFor:'Signal constants'!
 
 emptyCollectionSignal
@@ -551,7 +550,6 @@
     ].
 ! !
 
-
 !Collection methodsFor:'accessing'!
 
 anElement
@@ -2315,6 +2313,29 @@
     "
 !
 
+collect:collectBlock thenDetect:detectBlock ifNone:exceptionalValue
+    "first apply collectBlock to each element, then pass the result to
+     detectBlock. 
+     Return the first element from collectBlock for which detectBlock evaluates to true.
+     If none does, return the value of exceptionalValue, which is usually a block.
+     Returns the same as if two separate collect:+detect:ifNone: messages were sent,
+     but avoids the creation of intermediate collections, so this is nicer for
+     big collections."
+
+    self do:[:each |
+        |rslt|
+
+        rslt := collectBlock value:each.
+        (detectBlock value:rslt) ifTrue:[^ rslt].
+    ].
+    ^ exceptionalValue value
+
+    "
+     ( #(1 2 3 4) collect:[:e | e squared] ) detect:[:e| e odd] ifNone:0
+     #(1 2 3 4) collect:[:e | e squared] thenDetect:[:e| e odd] ifNone:0
+    "
+!
+
 collect:collectBlock thenDo:aBlock
     "combination of collect followed by do.
      Avoids the creation of intermediate garbage"
@@ -5580,11 +5601,11 @@
 !Collection class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/Collection.st,v 1.344 2014-07-10 13:28:46 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/Collection.st,v 1.345 2014-07-15 09:47:43 cg Exp $'
 !
 
 version_CVS
-    ^ '$Header: /cvs/stx/stx/libbasic/Collection.st,v 1.344 2014-07-10 13:28:46 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/Collection.st,v 1.345 2014-07-15 09:47:43 cg Exp $'
 ! !