Collection.st
branchjv
changeset 17851 09d75924b034
parent 17847 62aa54f44969
child 17862 8a8ae3107950
--- a/Collection.st	Sat Aug 20 21:29:33 2011 +0100
+++ b/Collection.st	Mon Aug 22 11:53:10 2011 +0100
@@ -259,7 +259,6 @@
     ^ self withSize:n
 ! !
 
-
 !Collection class methodsFor:'Signal constants'!
 
 emptyCollectionSignal
@@ -314,6 +313,8 @@
     ^ self == Collection
 ! !
 
+
+
 !Collection methodsFor:'Compatibility-Dolphin'!
 
 identityIncludes:anObject
@@ -480,7 +481,6 @@
     ].
 ! !
 
-
 !Collection methodsFor:'accessing'!
 
 anElement
@@ -2137,6 +2137,50 @@
     "
 !
 
+detectMax: aBlock
+    "Evaluate aBlock with each of the receiver's elements as the argument. 
+    Answer the element for which aBlock evaluates to the highest magnitude.
+    If collection empty, return nil.  This method might also be called elect:."
+
+    | maxElement maxValue |
+    self do: [:each | | val | 
+        maxValue == nil
+            ifFalse: [
+                (val := aBlock value: each) > maxValue ifTrue: [
+                    maxElement := each.
+                    maxValue := val]]
+            ifTrue: ["first element"
+                maxElement := each.
+                maxValue := aBlock value: each].
+                "Note that there is no way to get the first element that works 
+                for all kinds of Collections.  Must test every one."].
+    ^ maxElement
+
+    "Created: / 20-08-2011 / 21:34:49 / cg"
+!
+
+detectMin: aBlock
+    "Evaluate aBlock with each of the receiver's elements as the argument. 
+    Answer the element for which aBlock evaluates to the lowest number.
+    If collection empty, return nil."
+
+    | minElement minValue |
+    self do: [:each | | val | 
+        minValue == nil
+            ifFalse: [
+                (val := aBlock value: each) < minValue ifTrue: [
+                    minElement := each.
+                    minValue := val]]
+            ifTrue: ["first element"
+                minElement := each.
+                minValue := aBlock value: each].
+                "Note that there is no way to get the first element that works 
+                for all kinds of Collections.  Must test every one."].
+    ^ minElement
+
+    "Created: / 20-08-2011 / 21:35:13 / cg"
+!
+
 do:aBlock
     "evaluate the argument, aBlock for each element"
 
@@ -4027,8 +4071,9 @@
 !Collection class methodsFor:'documentation'!
 
 version_CVS
-    ^ '§Header: /cvs/stx/stx/libbasic/Collection.st,v 1.261 2011/07/24 08:32:42 cg Exp §'
+    ^ '§Header: /cvs/stx/stx/libbasic/Collection.st,v 1.262 2011/08/20 20:20:06 cg Exp §'
 ! !
 
 Collection initialize!
 
+