--- a/Collection.st Thu Aug 19 21:39:34 2010 +0100
+++ b/Collection.st Thu Aug 26 11:12:57 2010 +0100
@@ -458,6 +458,39 @@
self emptyCollectionError.
!
+at:aKey ifNilOrAbsentPut:valueBlock
+ "try to fetch the element at aKey. If either the key is invalid (as in a Dictionary)
+ or there is no element stored under that key (as in an Array), set the element
+ from the valueBlock and return it.
+ Useful for lazy initialization of collections."
+
+ |val|
+
+ val := self at:aKey ifAbsent:[ self at:aKey put:valueBlock value ].
+ val isNil ifTrue:[
+ self at:aKey put:(val := valueBlock value).
+ ].
+ ^ val
+
+ "
+ |d|
+
+ d := Dictionary new.
+ d at:#foo ifNilOrAbsentPut:[ 'hello' ].
+ d
+ "
+
+ "
+ |a|
+
+ a := Array new:10.
+ a at:1 ifNilOrAbsentPut:[ 'hello' ].
+ a
+ "
+
+ "Created: / 24-08-2010 / 17:07:21 / cg"
+!
+
atAll:indexCollection put:anObject
"put anObject into all indexes from indexCollection in the receiver.
This abstract implementation requires that the receiver supports
@@ -1233,8 +1266,8 @@
!
sum:aBlock
- "for each element in the receiver, evaluate the argument, aBlock
- and sum up the results. Return the total sum or 0 for an empty collection.
+ "for each element in the receiver, evaluate the argument, aBlock and sum up the results.
+ Return the total sum or 0 for an empty collection.
Similar to (self collect...) sum, but avoids creation of an intermediate collection."
|sum|
@@ -1255,6 +1288,8 @@
((1 to:10) collect:[:n | n squared]) sum
((1 to:10) sum:[:n | n squared])
"
+
+ "Modified: / 23-08-2010 / 18:19:42 / cg"
! !
!Collection methodsFor:'converting'!
@@ -2267,6 +2302,14 @@
"
!
+keysDo:aBlock
+ "evaluate the argument, aBlock for every key in the collection."
+
+ self keysAndValuesDo:[:k :v | aBlock value:k]
+
+ "Created: / 24-08-2010 / 10:12:14 / cg"
+!
+
map:selector
"for lisp fans - similar to collect"
@@ -3167,6 +3210,32 @@
"Modified: / 11-07-2010 / 17:05:25 / cg"
!
+maxApplying:aBlock
+ "return the maximum value from applying aBlock to each element in the receiver collection,
+ using < to compare elements.
+ Raises an error, if the receiver is empty."
+
+ self emptyCheck.
+
+ ^ self
+ inject:nil
+ into:[:maxSoFar :this |
+ |v|
+
+ v := aBlock value:this.
+ (maxSoFar isNil or:[maxSoFar < v])
+ ifTrue:[v]
+ ifFalse:[maxSoFar]
+ ]
+
+ "
+ #(15 1 -9 -20 10 5) max -> 15
+ #(15 1 -9 -20 10 5) maxApplying:[:el | el abs] -> 20
+ "
+
+ "Created: / 23-08-2010 / 11:02:50 / cg"
+!
+
min
"return the minimum value in the receiver collection,
using < to compare elements.
@@ -3192,6 +3261,32 @@
"Modified: / 11-07-2010 / 17:06:38 / cg"
!
+minApplying:aBlock
+ "return the minimum value from applying aBlock to each element in the receiver collection,
+ using < to compare elements.
+ Raises an error, if the receiver is empty."
+
+ self emptyCheck.
+
+ ^ self
+ inject:nil
+ into:[:minSoFar :this |
+ |v|
+
+ v := aBlock value:this.
+ (minSoFar isNil or:[v < minSoFar])
+ ifTrue:[v]
+ ifFalse:[minSoFar]
+ ]
+
+ "
+ #(15 -1 -9 10 5) min -> -9
+ #(15 -1 -9 10 5) minApplying:[:el | el abs] -> 1
+ "
+
+ "Created: / 23-08-2010 / 11:01:42 / cg"
+!
+
minMax
"return the minimum and maximum values in the receiver collection
as a two element array, using #< to compare elements.
@@ -3736,15 +3831,15 @@
!Collection class methodsFor:'documentation'!
version
- ^ '$Id: Collection.st 10564 2010-08-10 08:55:15Z vranyj1 $'
+ ^ '$Id: Collection.st 10570 2010-08-26 10:12:57Z vranyj1 $'
!
version_CVS
- ^ 'Header: /cvs/stx/stx/libbasic/Collection.st,v 1.246 2010/08/07 17:18:25 cg Exp '
+ ^ 'Header: /cvs/stx/stx/libbasic/Collection.st,v 1.250 2010/08/24 15:10:11 cg Exp '
!
version_SVN
- ^ '$Id: Collection.st 10564 2010-08-10 08:55:15Z vranyj1 $'
+ ^ '$Id: Collection.st 10570 2010-08-26 10:12:57Z vranyj1 $'
! !
Collection initialize!
@@ -3752,3 +3847,4 @@
+