" Smalltalk/Collection.st -*- Smalltalk -*- " Collection : Object () Collection new [ ^self new: 2 ] Collection with: anObject [ ^self new add: anObject; yourself ] Collection emptyCheck [ self isEmpty ifTrue: [self errorEmptyCollection] ] Collection isEmpty [ ^self size = 0 ] Collection notEmpty [ ^self isEmpty not ] Collection addAll: aCollection [ aCollection do: [ :element | self add: element ]. ^aCollection ] Collection includes: anObject [ self do: [ :element | element = anObject ifTrue: [ ^true ] ]. ^false ] Collection do: unaryBlock [ self subclassResponsibility ] Collection doWithIndex: binaryBlock [ | index | index := 0. self do: [ :element | binaryBlock value: element value: index. index := index + 1 ] ] Collection inject: initialValue into: binaryBlock [ | nextValue | nextValue := initialValue. self do: [ :element | nextValue := binaryBlock value: nextValue value: element ]. ^nextValue ] Collection collect: unaryBlock [ | newCollection | newCollection := self species new. self do: [:element | newCollection add: (unaryBlock value: element)]. ^newCollection ] Collection asArray [ | array | array := Array new: self size. self doWithIndex: [ :element :index | array at: index put: element ]. ^array ] Collection errorEmptyCollection [ ^self error: 'this collection is empty' ] Collection errorCannotModify [ ^self error: 'this collection cannot be modified' ] Collection printOn: aStream [ self printNameOn: aStream; printElementsOn: aStream ] Collection printNameOn: aStream [ super printOn: aStream ] Collection printElementsOn: aStream [ aStream nextPutAll: '('. self do: [:element | aStream print: element; space]. self isEmpty ifFalse: [aStream skip: -1]. aStream nextPutAll: ')' ] Collection print [ | first | super print. '(' print. first := true. self do: [ :element | first ifTrue: [first := false] ifFalse: [' ' print]. element print ]. ')' print. ]