Collection standard methods
stéphane ducasse
ducasse at iam.unibe.ch
Sun Sep 3 06:46:58 UTC 2006
I think that it would be good to rethink and enlarge the collections
methods.
How explode is called in ruby?
Stef
On 2 sept. 06, at 19:25, Ron Teitelbaum wrote:
> Hello all,
>
> I have some of my own collection methods that I have to keep
> remembering not
> to use for shared code.
>
> What I need is a standard method for my Collection >> explode:
> aDelimiter
> method
>
> Collection >> explode: aDelimiter
> "explode the collection into a collection of collections broken by
> aDelimiter"
> "(#(#(1 2) #(3 4)) mergeDelimited: Character tab ) explode:
> Character tab = an OrderedCollection(#(1 2) #(3 4))
> 'abcdef' explode: 'cd' = an OrderedCollection('ab' 'ef')"
>
> | resultCollection starting aDelimiterPosition aDelimiterSize |
>
> self ifEmpty: [^self].
> resultCollection _ OrderedCollection new.
> aDelimiterSize _ aDelimiter isCollection ifTrue: [aDelimiter size]
> ifFalse: [1].
> starting _ 1.
> [aDelimiterPosition _ aDelimiter isCollection ifTrue: [self
> indexOfSubCollection: aDelimiter startingAt: starting] ifFalse: [self
> indexOf: aDelimiter startingAt: starting ifAbsent: [0]].
> aDelimiterPosition > 0] whileTrue: [
> resultCollection add: (self copyFrom: starting to:
> aDelimiterPosition - 1).
> starting _ aDelimiterPosition + aDelimiterSize.
> ].
> resultCollection add: (self copyFrom: starting to: self size).
> ^resultCollection
>
> For reference:
>
> Collection >> mergeDelimited: anObject
> "return to reciever a collection with each element concatenated to
> remove imbeded collections"
> "#(#(1 2) #(3 4)) mergeDelimited: Character tab = #(1 2 Character
> tab 3 4), #('ab' 'cd') mergeDelimited: Character cr = 'ab
> cd' "
> | returnCollection aSeperator |
> self ifEmpty: [^self].
> aSeperator _ anObject isCollection ifTrue: [anObject] ifFalse:
> [Array with: anObject].
> returnCollection _ self first species new.
> self copy from: 1 to: self size -1 do: [:a |
> a ifNotNil: [
> returnCollection _ returnCollection, a, aSeperator
> ].
> ].
> ^returnCollection, self last.
>
> And for completeness:
>
> Collection >> merge
> "return to reciever a collection with each element concatenated to
> remove imbeded collections"
> "#(#(1 2) #(3 4)) merge = #(1 2 3 4), #('ab' 'cd') merge = 'abcd'"
> | returnCollection |
> self ifEmpty: [^self].
> returnCollection _ self first species new.
> self do: [:a |
> a ifNotNil: [
> returnCollection _ returnCollection, a
> ].
> ].
> ^returnCollection
>
>
> I've discussed adding my methods on mantis previously but I think the
> reaction I got was collection was already too large.
>
> The replacement for merge is gather: but merge has an additional
> feature in
> that it creates collections in the type of the first element, which
> means it
> works for strings.
>
> #('a' 'b' 'c') gather: [:a | a] #($a $b $c)
> #('a' 'b' 'c') merge 'abc'
>
> Otherwise it is similar.
>
> #(#(1 2) #(3 4)) gather: [:a | a] #(1 2 3 4)
> #(#(1 2) #(3 4)) merge #(1 2 3 4)
>
>
> So back to my original question is there an explode: aDelimiter
> method out
> there that I missed? There is a
> Collection>>asStringOn: aStream delimiter: delimString
> but this doesn't meet my requirement for a collection of non string
> elements
> (including other collections).
>
> Thanks,
>
> Ron Teitelbaum
>
>
>
>
>
>
More information about the Squeak-dev
mailing list
|