--- a/Collection.st Wed Dec 21 15:55:04 2011 +0100
+++ b/Collection.st Wed Dec 21 15:58:10 2011 +0100
@@ -3896,20 +3896,7 @@
allElementsHaveTheSameValue
"true if all elements of the receiver have the same value"
- |first theElement|
-
- first := true.
- self do:[:each |
- first ifTrue:[
- first := false.
- theElement := each.
- ] ifFalse:[
- theElement = each ifFalse:[
- ^ false.
- ].
- ].
- ].
- ^ true
+ ^ self sameValueFor:[:el | el]
"
#(1 2 3 5 6 7 8 9) allElementsHaveTheSameValue
@@ -4145,6 +4132,34 @@
#(1 4 6 8 4 1) occurrencesOfAny:#(2 5)
'hello world' occurrencesOfAny:'hel'
"
+!
+
+sameValueFor:aBlock
+ "true if aBlock answers the same value for all elements of the receiver"
+
+ |first valueForFirstElement|
+
+ first := true.
+ self do:[:each |
+ first ifTrue:[
+ first := false.
+ valueForFirstElement := aBlock value:each.
+ ] ifFalse:[
+ valueForFirstElement = (aBlock value:each) ifFalse:[
+ ^ false.
+ ].
+ ].
+ ].
+ ^ true
+
+ "
+ #(1 2 3 5 6 7 8 9) sameValueFor:[:el | el even]
+ #(1 1 1 1 1 1) sameValueFor:[:el | el even]
+ #(1 1 1.0 1.0 1) sameValueFor:[:el | el even]
+ #(1 3 3 15 1) sameValueFor:[:el | el even]
+ "
+
+ "Created: / 21-12-2011 / 15:57:08 / cg"
! !
!Collection methodsFor:'tracing'!
@@ -4167,11 +4182,11 @@
!Collection class methodsFor:'documentation'!
version
- ^ '$Header: /cvs/stx/stx/libbasic/Collection.st,v 1.268 2011-12-21 14:55:04 cg Exp $'
+ ^ '$Header: /cvs/stx/stx/libbasic/Collection.st,v 1.269 2011-12-21 14:58:10 cg Exp $'
!
version_CVS
- ^ '$Header: /cvs/stx/stx/libbasic/Collection.st,v 1.268 2011-12-21 14:55:04 cg Exp $'
+ ^ '$Header: /cvs/stx/stx/libbasic/Collection.st,v 1.269 2011-12-21 14:58:10 cg Exp $'
! !
Collection initialize!