Tim Felgentreff uploaded a new version of Collections to project The Inbox:
http://source.squeak.org/inbox/Collections-tfel.131.mcz ==================== Summary ==================== Name: Collections-tfel.131 Author: tfel Time: 14 September 2009, 2:00:17 am UUID: 61c6b5e1-32e7-4ae0-8934-d347b7fa2f29 Ancestors: Collections-tfel.130 As the previous, just fixing for OrderedCollection subclasses. Tried to make it shorter than it can be =============== Diff against Collections-tfel.130 =============== Item was changed: ----- Method: SequenceableCollection>>flatten (in category 'converting') ----- flatten "Remove all nesting of myself from me (Strings are not flattened though). E.g.: {3 .4 .{2 .4 .{'hi'} .'ho'}} flatten = {3 .4 .2 .4 .'hi' .'ho'}" | flattened subFlattened index | + flattened := Array - flattened := self species new: (self inject: 0 into: [:subTotal :next | (next isCollection and: [next isString not]) ifTrue: [subTotal + next flatten size] ifFalse: [subTotal + 1]]). index := 1. self do: [:each | (each isCollection and: [each isString not]) ifTrue: [ subFlattened := each flatten. flattened atAll: (index to: index + subFlattened size - 1) putAll: subFlattened. index := index + subFlattened size] ifFalse: [ flattened at: index put: each. index := index + 1]]. + ^ self species newFrom: flattened! - ^ flattened! |
On 14.09.2009, at 14:00, [hidden email] wrote: > Tim Felgentreff uploaded a new version of Collections to project The > Inbox: > http://source.squeak.org/inbox/Collections-tfel.131.mcz > > ==================== Summary ==================== > > Name: Collections-tfel.131 > Author: tfel > Time: 14 September 2009, 2:00:17 am > UUID: 61c6b5e1-32e7-4ae0-8934-d347b7fa2f29 > Ancestors: Collections-tfel.130 > > As the previous, just fixing for OrderedCollection subclasses. Tried > to make it shorter than it can be > > =============== Diff against Collections-tfel.130 =============== > > Item was changed: > ----- Method: SequenceableCollection>>flatten (in category > 'converting') ----- > flatten > "Remove all nesting of myself from me (Strings are not flattened > though). E.g.: {3 .4 .{2 .4 .{'hi'} .'ho'}} flatten = {3 .4 .2 . > 4 .'hi' .'ho'}" > | flattened subFlattened index | > + flattened := Array > - flattened := self species > new: (self inject: 0 into: [:subTotal :next | > (next isCollection and: [next isString not]) > ifTrue: [subTotal + next flatten size] > ifFalse: [subTotal + 1]]). > index := 1. > self do: [:each | > (each isCollection and: [each isString not]) > ifTrue: [ > subFlattened := each flatten. > flattened > atAll: (index to: index + subFlattened size - 1) > putAll: subFlattened. > index := index + subFlattened size] > ifFalse: [ > flattened at: index put: each. > index := index + 1]]. > + ^ self species newFrom: flattened! > - ^ flattened! IMHO this should use a stream to flatten (using, e.g. #flattenOn:). Recursing *twice* into each subcollection is neither more memory nor time-efficient, but just needlessly complex. - Bert - |
Free forum by Nabble | Edit this page |