Re: [Pharo-users] uses or instead of a searching literal
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
Op 27-12-2019 om 23:33 schreef Richard O'Keefe:
aString splitOn: ' -_' asSet
Hello Richard, Thanks again , I find this "aString splitOn: '_- ` asSet "  much cleaner but on some way I does not split for example 'Portable Network Graphics' into " #(Portable, Network, Graphics) ". When I debug it , it seems there is no splitting at all. So today time to dive into it why it does not work and how to solve it. Roelof
participants (2)
-
Richard O'Keefe -
Roelof Wobben