[Cuis-dev] String>>findTokens: delimiters
Ron Teitelbaum
ron at 3dicc.com
Tue May 7 19:03:02 PDT 2019
I was thinking about not responding because of all the flak I got last time
I mentioned my methods that I use all the time.
But for what they are worth.
Here are my methods explode and mergeDelimited:
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
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
They are really great workhorses and work the way you would expect!
All the best,
*Ron Teitelbaum*
*Chief Executive Officer**3D Immersive Collaboration Consulting, LLC*
ron at 3dicc.com
www.3dicc.com
<https://www.facebook.com/3DICC> <https://twitter.com/RonTeitelbaum>
<https://www.linkedin.com/in/ronteitelbaum>
On Tue, May 7, 2019 at 7:01 PM Hernan Wilkinson via Cuis-dev <
cuis-dev at lists.cuis.st> wrote:
> Last Sunday I could not find it because I did not remember the name of the
> message!
> I agree that #findTokens: is not a good name.
> For compatibility with other smalltalks I would use #substrings:
>
>
> On Mon, May 6, 2019 at 8:45 PM Gastón Caruso via Cuis-dev <
> cuis-dev at lists.cuis.st> wrote:
>
>> Hi all, some minutes ago I’ve just asked in the telegram group about this
>> method, I knew that Pharo had a method called String>>#substrings:
>> character or something like that, with the same purpose.
>> Ruby has a method called String>>#split: a_string (which I think is the
>> best name).
>>
>> It don’t think the name intention revealing at all. If you agree I can
>> rename that method with all the senders of the base image, but still there
>> are some packages that uses this (like Network).
>>
>> What do you think? Do you agree that this name can be improved? Do you
>> think that split is a better name? if not, what do you suggest?
>>
>> Thanks,
>> Gastón
>> --
>> Cuis-dev mailing list
>> Cuis-dev at lists.cuis.st
>> https://lists.cuis.st/mailman/listinfo/cuis-dev
>>
>
>
> --
>
> *Hernán WilkinsonAgile Software Development, Teaching & Coaching*
> *Phone: +54-011*-4893-2057
> *Twitter: @HernanWilkinson*
> *site: http://www.10Pines.com <http://www.10pines.com/>*
> Address: Alem 896, Floor 6, Buenos Aires, Argentina
> --
> Cuis-dev mailing list
> Cuis-dev at lists.cuis.st
> https://lists.cuis.st/mailman/listinfo/cuis-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cuis.st/mailman/archives/cuis-dev/attachments/20190507/a6b1f74e/attachment.html>
More information about the Cuis-dev
mailing list