Coll.st
changeset 421 a0807a38319d
parent 404 e70aaf0e224f
child 528 a083413dfbe8
--- a/Coll.st	Sat Sep 02 18:08:30 1995 +0200
+++ b/Coll.st	Sun Sep 03 17:06:58 1995 +0200
@@ -22,7 +22,7 @@
 COPYRIGHT (c) 1989 by Claus Gittinger
 	      All Rights Reserved
 
-$Header: /cvs/stx/stx/libbasic/Attic/Coll.st,v 1.40 1995-08-22 13:30:17 claus Exp $
+$Header: /cvs/stx/stx/libbasic/Attic/Coll.st,v 1.41 1995-09-03 15:04:44 claus Exp $
 '!
 
 !Collection class methodsFor:'documentation'!
@@ -43,7 +43,7 @@
 
 version
 "
-$Header: /cvs/stx/stx/libbasic/Attic/Coll.st,v 1.40 1995-08-22 13:30:17 claus Exp $
+$Header: /cvs/stx/stx/libbasic/Attic/Coll.st,v 1.41 1995-09-03 15:04:44 claus Exp $
 "
 !
 
@@ -847,6 +847,38 @@
     "
 !
 
+detectLast:aBlock
+    "evaluate the argument, aBlock for each element in the receiver until
+     the block returns true; in this case return the element which caused
+     the true evaluation. The elements are processed in reverse order.
+     If none of the evaluations returns true, report an error"
+
+    ^ self detectLast:aBlock ifNone:[self errorNotFound]
+
+    "
+     #(1 2 3 4) detectLast:[:n | n odd]   
+     #(2 4 6 8) detectLast:[:n | n odd]  
+    "
+!
+
+detectLast:aBlock ifNone:exceptionBlock
+    "evaluate the argument, aBlock for each element in the receiver until
+     the block returns true; in this case return the element which caused
+     the true evaluation. The elements are processed in reverse order.
+     If none of the evaluations returns true, return the result of the
+     evaluation of the exceptionBlock"
+
+    self reverseDo:[:each | 
+	(aBlock value:each) ifTrue:[^ each].
+    ].
+    ^ exceptionBlock value
+
+    "
+     #(1 2 3 4) detectLast:[:n | n odd] ifNone:['sorry']    
+     #(2 4 6 8) detectLast:[:n | n odd] ifNone:['sorry']     
+    "
+!
+
 inject:thisValue into:binaryBlock
     |nextValue|