Collection.st
changeset 16777 6be6dfd33ec6
parent 16753 567f6b68e6e9
child 16778 554ad879a1c9
equal deleted inserted replaced
16776:fd62ae0ff6fa 16777:6be6dfd33ec6
   265      Kludges around the stupid definition of OrderedCollection>>new:"
   265      Kludges around the stupid definition of OrderedCollection>>new:"
   266 
   266 
   267     ^ self newWithSize:n
   267     ^ self newWithSize:n
   268 ! !
   268 ! !
   269 
   269 
   270 
       
   271 !Collection class methodsFor:'Signal constants'!
   270 !Collection class methodsFor:'Signal constants'!
   272 
   271 
   273 emptyCollectionSignal
   272 emptyCollectionSignal
   274     "return the signal used to report non-allowed operation on empty collections"
   273     "return the signal used to report non-allowed operation on empty collections"
   275 
   274 
   548 
   547 
   549     ^ self keysAndValuesDo:[:key :index |
   548     ^ self keysAndValuesDo:[:key :index |
   550         aTwoArgBlock value:index value:key
   549         aTwoArgBlock value:index value:key
   551     ].
   550     ].
   552 ! !
   551 ! !
   553 
       
   554 
   552 
   555 !Collection methodsFor:'accessing'!
   553 !Collection methodsFor:'accessing'!
   556 
   554 
   557 anElement
   555 anElement
   558     "return any element from the collection, 
   556     "return any element from the collection, 
  2310     ^ newCollection
  2308     ^ newCollection
  2311 
  2309 
  2312     "
  2310     "
  2313      #(one two three four five six) collect:[:element | element asUppercase] as:OrderedCollection
  2311      #(one two three four five six) collect:[:element | element asUppercase] as:OrderedCollection
  2314      'abcdef' collect:[:char | char digitValue] as:ByteArray
  2312      'abcdef' collect:[:char | char digitValue] as:ByteArray
       
  2313     "
       
  2314 !
       
  2315 
       
  2316 collect:collectBlock thenDetect:detectBlock ifNone:exceptionalValue
       
  2317     "first apply collectBlock to each element, then pass the result to
       
  2318      detectBlock. 
       
  2319      Return the first element from collectBlock for which detectBlock evaluates to true.
       
  2320      If none does, return the value of exceptionalValue, which is usually a block.
       
  2321      Returns the same as if two separate collect:+detect:ifNone: messages were sent,
       
  2322      but avoids the creation of intermediate collections, so this is nicer for
       
  2323      big collections."
       
  2324 
       
  2325     self do:[:each |
       
  2326         |rslt|
       
  2327 
       
  2328         rslt := collectBlock value:each.
       
  2329         (detectBlock value:rslt) ifTrue:[^ rslt].
       
  2330     ].
       
  2331     ^ exceptionalValue value
       
  2332 
       
  2333     "
       
  2334      ( #(1 2 3 4) collect:[:e | e squared] ) detect:[:e| e odd] ifNone:0
       
  2335      #(1 2 3 4) collect:[:e | e squared] thenDetect:[:e| e odd] ifNone:0
  2315     "
  2336     "
  2316 !
  2337 !
  2317 
  2338 
  2318 collect:collectBlock thenDo:aBlock
  2339 collect:collectBlock thenDo:aBlock
  2319     "combination of collect followed by do.
  2340     "combination of collect followed by do.
  5578 ! !
  5599 ! !
  5579 
  5600 
  5580 !Collection class methodsFor:'documentation'!
  5601 !Collection class methodsFor:'documentation'!
  5581 
  5602 
  5582 version
  5603 version
  5583     ^ '$Header: /cvs/stx/stx/libbasic/Collection.st,v 1.344 2014-07-10 13:28:46 cg Exp $'
  5604     ^ '$Header: /cvs/stx/stx/libbasic/Collection.st,v 1.345 2014-07-15 09:47:43 cg Exp $'
  5584 !
  5605 !
  5585 
  5606 
  5586 version_CVS
  5607 version_CVS
  5587     ^ '$Header: /cvs/stx/stx/libbasic/Collection.st,v 1.344 2014-07-10 13:28:46 cg Exp $'
  5608     ^ '$Header: /cvs/stx/stx/libbasic/Collection.st,v 1.345 2014-07-15 09:47:43 cg Exp $'
  5588 ! !
  5609 ! !
  5589 
  5610 
  5590 
  5611 
  5591 Collection initialize!
  5612 Collection initialize!