2007-06-05 Stephen Compall * kernel/Collection.st: Add #join: class method. * kernel/ArrayColl.st: Specialize. * kernel/SeqCollect.st: Add #flatten: and #flatten methods. --- orig/kernel/ArrayColl.st +++ mod/kernel/ArrayColl.st @@ -117,6 +117,19 @@ at: 4 put: element4; at: 5 put: element5; yourself +! + +join: aCollection + "Where aCollection is a collection of SequenceableCollections, + answer a new instance with all the elements therein, in order." + | newInst | + newInst := self new: (aCollection inject: 0 into: + [:size :each | size + each size]). + aCollection inject: 1 into: [:start :subColl | | nextStart | + newInst replaceFrom: start to: (nextStart := start + subColl size) - 1 + with: subColl. + nextStart]. + ^newInst ! ! --- orig/kernel/Collection.st +++ mod/kernel/Collection.st @@ -93,6 +93,17 @@ they were passed" ^self new add: firstObject; add: secondObject; add: thirdObject; add: fourthObject; add: fifthObject; yourself +! + +join: aCollection + "Answer a collection formed by treating each element in + aCollection as a `withAll:' argument collection to be added to a + new instance." + | newInst | + newInst := self new: (aCollection inject: 0 into: + [:size :each | size + each size]). + aCollection do: [:each | newInst addAll: each]. + ^newInst ! ! --- orig/kernel/SeqCollect.st +++ mod/kernel/SeqCollect.st @@ -543,6 +543,31 @@ +!SequenceableCollection methodsFor: 'concatenating'! + +flatten: aSpecies + "Answer a new collection of aSpecies, which should respond to + #join:, with all the elements of all my elements, which should be + collections, in order." + ^aSpecies join: self +! + +flatten + "Answer a new collection like my first element, with all the + elements (in order) of all my elements, which should be + collections. + + I use my first element instead of myself as a prototype because + my elements are more likely to share the desired properties than + I am, such as in: + + #('hello, ' 'world') flatten => 'hello, world'" + ^self isEmpty ifTrue: [#()] + ifFalse: [self flatten: self first species] +! ! + + + !SequenceableCollection methodsFor: 'enumerating'! readStream