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" |
1038 The reason for this is that it is not known whether we are dealing with number |
1040 The reason for this is that it is not known whether we are dealing with number |
1039 (i.e. if 0 is a good initial value for the sum). |
1041 (i.e. if 0 is a good initial value for the sum). |
1040 Consider a collection of measurement or physical objects, 0 would be the unitless |
1042 Consider a collection of measurement or physical objects, 0 would be the unitless |
1041 value and would not be appropriate to add with the unit-ed objects." |
1043 value and would not be appropriate to add with the unit-ed objects." |
1042 |
1044 |
1043 self emptyCheck. |
1045 self isEmpty ifTrue:[^ 0]. |
|
1046 |
1044 ^ self |
1047 ^ self |
1045 inject:(self first class zero) |
1048 inject:(self first class zero) |
1046 into:[:accum :each | accum + each]. |
1049 into:[:accum :each | accum + each]. |
1047 |
1050 |
1048 |
1051 |
1049 " |
1052 " |
1050 TestCase should: [ { } sum ] raise:Error. |
1053 TestCase assert: ( { } sum = 0 ). |
1051 TestCase should: [ '' sum ] raise:Error. |
|
1052 |
|
1053 TestCase assert: ( { 1 } sum = 1 ). |
1054 TestCase assert: ( { 1 } sum = 1 ). |
1054 TestCase assert: ( { 1. 2. 3. 4. } sum = 10 ). |
1055 TestCase assert: ( { 1. 2. 3. 4. } sum = 10 ). |
1055 TestCase assert: ( (1 to:10) sum = 55 ). |
1056 TestCase assert: ( (1 to:10) sum = 55 ). |
1056 TestCase assert: ( 'abc' asByteArray sum = 294 ). |
1057 TestCase assert: ( 'abc' asByteArray sum = 294 ). |
1057 TestCase assert: ( { 10 +/- 2. |
1058 TestCase assert: ( { 10 +/- 2. |