# HG changeset patch # User Claus Gittinger # Date 1405417663 -7200 # Node ID 6be6dfd33ec6d2c31df90a959091ee21bdf77396 # Parent fd62ae0ff6fa72bbd23064d05173a87bbcb5060b class: Collection added: #collect:thenDetect:ifNone: diff -r fd62ae0ff6fa -r 6be6dfd33ec6 Collection.st --- 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 $' ! !