Hello,
How can I get rid of the above error message with this code :
|
Since you're splitting a String, why not use a Regex to do this?
-- Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html |
In reply to this post by Pharo Smalltalk Users mailing list
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 <[hidden email]> 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 > Splitting-with-a-set.cs (1K) Download Attachment |
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 |
Are you aware of the ‘finder’ tool?, in particular the ‘examples' mode is useful. try: ‘aaa_bbb-ccc’. ‘_-‘. #(‘aaa’ ‘bbb’ ‘ccc’) Kasper
On 28 December 2019 at 08.12.03, Roelof Wobben via Pharo-users ([hidden email]) wrote:
|
yep. Im aware of this tool but on this
case nothing pops up.
Roelof Op 28-12-2019 om 09:22 schreef Kasper Østerbye:
|
I would go for
'Portable Network Graphics' findTokens: ' -_'. > On 28 Dec 2019, at 09:35, Roelof Wobben via Pharo-users <[hidden email]> wrote: > > > From: Roelof Wobben <[hidden email]> > Subject: Re: [Pharo-users] uses or instead of a searching literal > Date: 28 December 2019 at 09:35:17 GMT+1 > To: [hidden email] > > > yep. Im aware of this tool but on this case nothing pops up. > > Roelof > > > > > Op 28-12-2019 om 09:22 schreef Kasper Østerbye: >> Are you aware of the ‘finder’ tool?, in particular the ‘examples' mode is useful. >> >> try: ‘aaa_bbb-ccc’. ‘_-‘. #(‘aaa’ ‘bbb’ ‘ccc’) >> >> >> Best, >> >> Kasper >> >> On 28 December 2019 at 08.12.03, Roelof Wobben via Pharo-users ([hidden email]) wrote: >> >>> 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 >>> >>> > > > |
Hello Sven,
Thanks. this is what im looking for. Roelof Op 28-12-2019 om 10:46 schreef Sven Van Caekenberghe: > I would go for > > 'Portable Network Graphics' findTokens: ' -_'. > >> On 28 Dec 2019, at 09:35, Roelof Wobben via Pharo-users <[hidden email]> wrote: >> >> >> From: Roelof Wobben <[hidden email]> >> Subject: Re: [Pharo-users] uses or instead of a searching literal >> Date: 28 December 2019 at 09:35:17 GMT+1 >> To: [hidden email] >> >> >> yep. Im aware of this tool but on this case nothing pops up. >> >> Roelof >> >> >> >> >> Op 28-12-2019 om 09:22 schreef Kasper Østerbye: >>> Are you aware of the ‘finder’ tool?, in particular the ‘examples' mode is useful. >>> >>> try: ‘aaa_bbb-ccc’. ‘_-‘. #(‘aaa’ ‘bbb’ ‘ccc’) >>> >>> >>> Best, >>> >>> Kasper >>> >>> On 28 December 2019 at 08.12.03, Roelof Wobben via Pharo-users ([hidden email]) wrote: >>> >>>> 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 >>>> >>>> >> >> > |
Free forum by Nabble | Edit this page |