Collection.st
changeset 11514 2512add96e15
parent 11498 df4f9e10b141
child 11599 4fd6f122b607
equal deleted inserted replaced
11513:97a94bac0991 11514:2512add96e15
   225     "return a new collection which really provides space for n elements.
   225     "return a new collection which really provides space for n elements.
   226      Kludges around the stupid definition of OrderedCollection>>new:"
   226      Kludges around the stupid definition of OrderedCollection>>new:"
   227 
   227 
   228     ^ self withSize:n
   228     ^ self withSize:n
   229 ! !
   229 ! !
       
   230 
   230 
   231 
   231 !Collection class methodsFor:'Signal constants'!
   232 !Collection class methodsFor:'Signal constants'!
   232 
   233 
   233 emptyCollectionSignal
   234 emptyCollectionSignal
   234     "return the signal used to report non-allowed operation on empty collections"
   235     "return the signal used to report non-allowed operation on empty collections"
   381     ^ self keysAndValuesDo:[:key :index |
   382     ^ self keysAndValuesDo:[:key :index |
   382         aTwoArgBlock value:index value:key
   383         aTwoArgBlock value:index value:key
   383     ].
   384     ].
   384 ! !
   385 ! !
   385 
   386 
       
   387 
   386 !Collection methodsFor:'accessing'!
   388 !Collection methodsFor:'accessing'!
   387 
   389 
   388 anElement
   390 anElement
   389     "return any element from the collection, 
   391     "return any element from the collection, 
   390      report an error if there is none"
   392      report an error if there is none"
  1048                          100 +/- 10 } sum = (130 +/- 16) ).
  1050                          100 +/- 10 } sum = (130 +/- 16) ).
  1049 
  1051 
  1050      TestCase assert: ( { (1 / 9).
  1052      TestCase assert: ( { (1 / 9).
  1051                           (1 / 7).
  1053                           (1 / 7).
  1052                         } sum = (16 / 63) ).
  1054                         } sum = (16 / 63) ).
       
  1055     "
       
  1056 !
       
  1057 
       
  1058 sum:aBlock
       
  1059     "for each element in the receiver, evaluate the argument, aBlock
       
  1060      and sum up the results. Return the total sum or 0 for an empty collection.
       
  1061      Similar to (self collect...) sum, but avoids creation of an intermediate collection."
       
  1062 
       
  1063     |sum|
       
  1064 
       
  1065     self do:[:element |
       
  1066         |thisValue|
       
  1067 
       
  1068         thisValue := aBlock value:element.
       
  1069         sum isNil ifTrue:[
       
  1070             sum := thisValue
       
  1071         ] ifFalse:[
       
  1072             sum := sum + thisValue
       
  1073         ].
       
  1074     ].
       
  1075     ^ sum ? 0
       
  1076 
       
  1077     "
       
  1078      ((1 to:10) collect:[:n | n squared]) sum 
       
  1079      ((1 to:10) sum:[:n | n squared])         
  1053     "
  1080     "
  1054 ! !
  1081 ! !
  1055 
  1082 
  1056 !Collection methodsFor:'converting'!
  1083 !Collection methodsFor:'converting'!
  1057 
  1084 
  3326 ! !
  3353 ! !
  3327 
  3354 
  3328 !Collection class methodsFor:'documentation'!
  3355 !Collection class methodsFor:'documentation'!
  3329 
  3356 
  3330 version
  3357 version
  3331     ^ '$Header: /cvs/stx/stx/libbasic/Collection.st,v 1.218 2009-01-29 20:21:46 cg Exp $'
  3358     ^ '$Header: /cvs/stx/stx/libbasic/Collection.st,v 1.219 2009-02-04 15:13:22 cg Exp $'
  3332 ! !
  3359 ! !
  3333 
  3360 
  3334 Collection initialize!
  3361 Collection initialize!