Marcel Taeumel uploaded a new version of Collections to project The Trunk:
http://source.squeak.org/trunk/Collections-mt.833.mcz ==================== Summary ==================== Name: Collections-mt.833 Author: mt Time: 13 May 2019, 9:46:28.73801 am UUID: 44057ed3-9b55-0b49-8472-71d5c3fdbeeb Ancestors: Collections-mt.832 Second iteration of #take: (and #any:). #any: relates to #anyOne and thus signals an error if the collection is not large enough. #take: uses #any: but accounts for smaller collections. =============== Diff against Collections-mt.832 =============== Item was added: + ----- Method: CharacterSet>>any: (in category 'accessing') ----- + any: numberOfElements + + ^ self any: numberOfElements as: Array! Item was added: + ----- Method: CharacterSet>>any:as: (in category 'accessing') ----- + any: numberOfElements as: aClass + + self canBeEnumerated + ifTrue: [^ super any: numberOfElements as: aClass] + ifFalse: [self shouldNotImplement]! Item was removed: - ----- Method: CharacterSet>>take: (in category 'accessing') ----- - take: n - - self shouldNotImplement.! Item was added: + ----- Method: Collection>>any: (in category 'accessing') ----- + any: numberOfElements + + ^ self any: numberOfElements as: self species! Item was added: + ----- Method: Collection>>any:as: (in category 'accessing') ----- + any: numberOfElements as: aClass + "Enumerate this colleciton and return the specified number of elements. Signals an error if this collection has not enough elements." + + | index result | + index := 0. + result := aClass new: numberOfElements. + + result fillFrom: self with: [:each | + (index := index + 1) > numberOfElements + ifTrue: [^ result] + ifFalse: [each]]. + + index = numberOfElements + ifFalse: [self error: 'Not enough elements in this collection.']. + + ^ result! Item was changed: ----- Method: Collection>>take: (in category 'accessing') ----- + take: maxNumberOfElements + "Returns maxNumberOfElements as a new collection or less if the collection is not large enough." - take: n - "Enumerate this collection and return the first n elements or less." + ^ self any: (maxNumberOfElements min: self size)! - | index result | - index := 1. - result := self species new: (n min: self size). - self associationsDo: [:each | - result add: each. - (index := index + 1) > n ifTrue: [^ result]]. - ^ result! Item was added: + ----- Method: SequenceableCollection>>any: (in category 'accessing') ----- + any: numberOfElements + + ^ self first: numberOfElements! Item was removed: - ----- Method: SequenceableCollection>>take: (in category 'accessing') ----- - take: n - - ^ self first: (n min: self size)! Item was added: + ----- Method: Stream>>any: (in category 'accessing') ----- + any: numberOfElements + "See Collection protocol." + + ^ self next: numberOfElements! Item was changed: ----- Method: Stream>>take: (in category 'accessing') ----- + take: maxNumberOfElements + "See Collection protocol." + + ^ self any: maxNumberOfElements! - take: n - - ^ self next: n! |
Free forum by Nabble | Edit this page |