Collection standard methods

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Collection standard methods

Ron Teitelbaum
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



 


Reply | Threaded
Open this post in threaded view
|

Re: Collection standard methods

stéphane ducasse-2
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
>
>
>
>
>
>


Reply | Threaded
Open this post in threaded view
|

Re: Collection standard methods

Edgar J. De Cleene
stéphane ducasse puso en su mail :

> I think that it would be good to rethink and enlarge the collections
> methods.
> How explode is called in ruby?
>
> Stef

'abcdef'  findTokens: 'cd' an OrderedCollection('ab' 'ef')
And was in Squeak for a loooong time.

>
> 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
>>
>>
>>
>>
>>
>>
>
>



       
       
               
__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas


Reply | Threaded
Open this post in threaded view
|

Re: Re: Collection standard methods

Damien Pollet
In reply to this post by stéphane ducasse-2
On 9/3/06, stéphane ducasse <[hidden email]> wrote:
> I think that it would be good to rethink and enlarge the collections
> methods.
> How explode is called in ruby?

There is String#split which takes as separator either a string or a
regexp, but it doesn't work with characters and isn't implemented on
collections :)

--
 Damien Pollet
 type less, do more


Reply | Threaded
Open this post in threaded view
|

Re: Collection standard methods

stéphane ducasse-2
In reply to this post by Edgar J. De Cleene
but findTokens: is not really a good name

it could answer the number of tokens 'cd' in the string for example.
So may be we should have a method called splitBy: or cutUsing: to  
express the same.


> stéphane ducasse puso en su mail :
>
>> I think that it would be good to rethink and enlarge the collections
>> methods.
>> How explode is called in ruby?
>>
>> Stef
>
> 'abcdef'  findTokens: 'cd' an OrderedCollection('ab' 'ef')
> And was in Squeak for a loooong time.
>
>>
>> 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
>>>
>>>
>>>
>>>
>>>
>>>
>>
>>
>
>
>
>
>
>
> __________________________________________________
> Preguntá. Respondé. Descubrí.
> Todo lo que querías saber, y lo que ni imaginabas,
> está en Yahoo! Respuestas (Beta).
> ¡Probalo ya!
> http://www.yahoo.com.ar/respuestas
>
>
>


Reply | Threaded
Open this post in threaded view
|

RE: Collection standard methods

Ron Teitelbaum
In reply to this post by Edgar J. De Cleene
Perfect!!  Thanks!

Ron

> -----Original Message-----
> From: [hidden email] [mailto:squeak-dev-
> [hidden email]] On Behalf Of Lic. Edgar J. De Cleene
> Sent: Sunday, September 03, 2006 3:20 AM
> To: squeakdev
> Subject: Re: Collection standard methods
>
> stéphane ducasse puso en su mail :
>
> > I think that it would be good to rethink and enlarge the collections
> > methods.
> > How explode is called in ruby?
> >
> > Stef
>
> 'abcdef'  findTokens: 'cd' an OrderedCollection('ab' 'ef')
> And was in Squeak for a loooong time.
>
> >
> > 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
> >>
> >>
> >>
> >>
> >>
> >>
> >
> >
>
>
>
>
>
>
> __________________________________________________
> Preguntá. Respondé. Descubrí.
> Todo lo que querías saber, y lo que ni imaginabas,
> está en Yahoo! Respuestas (Beta).
> ¡Probalo ya!
> http://www.yahoo.com.ar/respuestas
>



Reply | Threaded
Open this post in threaded view
|

Re: Collection standard methods

Edgar J. De Cleene
Ron Teitelbaum puso en su mail :

> Perfect!!  Thanks!
>
> Ron
Ron, If you need more collection methods, I see if I can found some adds to
collection very useful.
Exists some from John Pierce and others from Ned Kontz , all I need is found
in what old images or CD backups I have it.
None of this useful methods exist in image today.

Edgar



       
       
               
__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas


Reply | Threaded
Open this post in threaded view
|

RE: Collection standard methods

Ron Teitelbaum
Actually I don't need methods I was looking to use the methods in the image
so that I didn't need to package up my methods and ship them to!

Thanks!

Ron

> -----Original Message-----
> From: [hidden email] [mailto:squeak-dev-
> [hidden email]] On Behalf Of Lic. Edgar J. De Cleene
> Sent: Monday, September 04, 2006 4:40 AM
> To: squeakdev
> Subject: Re: Collection standard methods
>
> Ron Teitelbaum puso en su mail :
>
> > Perfect!!  Thanks!
> >
> > Ron
> Ron, If you need more collection methods, I see if I can found some adds
> to
> collection very useful.
> Exists some from John Pierce and others from Ned Kontz , all I need is
> found
> in what old images or CD backups I have it.
> None of this useful methods exist in image today.
>
> Edgar
>
>
>
>
>
>
> __________________________________________________
> Pregunta. Respondi. Descubrm.
> Todo lo que quermas saber, y lo que ni imaginabas,
> esta en Yahoo! Respuestas (Beta).
> !Probalo ya!
> http://www.yahoo.com.ar/respuestas
>
>