You know, this is an area where style opinions differ a lot.
What the message is suggesting you try is
aString splitOn: [:each | ' -_' includes: each]

That's all.  No need for anything more complicated.

But there is a trade-off.  Clarity vs efficiency.  You should make your
code clear and correct before you worry about efficiency, true.  But
you should not go out of your way to make it *less* efficient.
The test you have is perfectly readable and is as fast as it gets.
Using #includes: would make the code shorter, but not clearer, and
certainly slower.

You could also use
aString splitOn: '[-_ ]' asRegex
because #splitOn: is documented as treating strings as regular
expressions.  This is the briefest, the most confusing to read, and
the slowest.  It is also by far the most error-prone:
- forget "asRegex" and you are in trouble
- forget the square brackets and you are in trouble
- put the hyphen-minus second instead of first and ...

If only #split:indicesDo: were defined for Set.  I cannot imagine
why it is not.  Here is a method definition that goes in the (new)
'splitting' category of Set:

split: aSequenceableCollection indicesDo: aBlock
    "Perform an action specified as aBlock (with a start and end
argument) to each of the indices of aSequenceableCollection that have
been identified by taking the receiver as a splitter."
    | position |

    position := 1.
    aSequenceableCollection withIndexDo: [:element :idx |
        (self includes: element)
            ifTrue: [
                aBlock value: position value: idx - 1.
                position := idx + 1 ]].

    aBlock value: position value: aSequenceableCollection size

With that in place you can use
  aString splitOn: ' -_' asSet

On Sat, 28 Dec 2019 at 10:24, Roelof Wobben via Pharo-users
<pharo-users@lists.pharo.org> wrote:
>
> Hello,
>
>
> How can I get rid of the above error message with this code :
>
>
> abbreviatePhrase: aString
>     | splitted |
>     splitted := aString
>         splitOn: [ :char | char = Character space or: [ char = $- or: [ char 
> = $_ ] ] ].
>     ^ String
>         streamContents: [ :stream |
>             splitted
>                 reject: [ :word | word isEmpty ]
>                 thenDo: [ :word | stream nextPut: word first uppercase ] ]
>
>
> Regards,
>
> Roelof
>
'From Pharo7.0.2 of 15 March 2019 [Build information: Pharo-7.0.2+build.154.sha.9f17218676db0c1a0dd5d1b03226e660dbd674b6 (32 Bit)] on 28 December 2019 at 11:33:01.066244 am'!

!Set methodsFor: 'splitting' stamp: 'RichardOKeefe 12/28/2019 11:29'!
split: aSequenceableCollection indicesDo: aBlock
	"Perform an action specified as aBlock (with a start and end argument) to each of the indices of aSequenceableCollection that have been identified by taking the receiver as a splitter." 
	| position |
	
	position := 1.
	aSequenceableCollection withIndexDo: [:element :idx |
		(self includes: element) 
			ifTrue: [
				aBlock value: position value: idx - 1.
				position := idx + 1 ]].
		
	aBlock value: position value: aSequenceableCollection size! !


!Set reorganize!
(#'*OpalCompiler-Core' parseOptions:)
(#'*Monticello-Storing' comeFullyUpOnReload:)
(#copying postCopy)
(#adding add:)
(#accessing like:ifAbsent: like:)
(#converting asSet)
(#private rehash fixCollisionsFrom: noCheckAdd: scanFor: grow withArray: noCheckNoGrowFillFrom:)
(#'*Random-Core' atRandom:)
(#enumerating difference: collect: intersection: do:)
(#'*Reflectivity' metaLinkOptions)
(#'*ston-core' stonPostReferenceResolution)
(#comparing =)
(#testing isHealthy occurrencesOf: includes:)
(#removing remove:ifAbsent: copyWithout:)
(#'*GT-InspectorExtensions-Core' gtInspectorItemsIn:)
(#splitting split:indicesDo:)
(#'*Fuel-Core' fuelAfterMaterialization fuelAccept: addIfNotPresent:ifPresentDo:)
!

Reply via email to