开发者

Best way to discard elements before an element in a Collection

开发者 https://www.devze.com 2023-03-23 23:12 出处:网络
Given any String like \'Lorem ipsum dolor sit amet, consectetuer adipiscing elit\' I\'d like to discard every word before sit. I\'ve checked methods in String but not found very useful for this one. T

Given any String like 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit' I'd like to discard every word before sit. I've checked methods in String but not found very useful for this one. This is my try:

| phrase newPhrase |
phrase := 'Lorem ipsum dolor sit amet, consectetuer adipiscing el开发者_JS百科it'.
newPhrase := phrase substrings.
phrase substrings do: [: word | word = 'sit' ifFalse: [ newPhrase := newPhrase allButFirst ] ifTrue: [ ^ nil ] ].
newPhrase joinUsing: String space

it is answering nil when evaluated in a Workspace and however there must be a clever way right?


The code:

phrase substrings do: [: word | word = 'sit' ifFalse: [ ... ] ifTrue: [ ^ nil ] ].

returns nil when the word sit is found.

Note that ^ in Smalltalk always return from the enclosing method. It doesn't break out of the do:.

Try this instead

To discard all words before sit:

words := phrase substrings.
newPhrase := words allButFirst: (words indexOf: 'sit') - 1

You may also find these collection methods interesting:

words copyAfter: 'sit'      "all words after sit"
words copyUpTo: 'sit'       "all words before sit"
words copyUpThrough: 'sit'  "words before and including sit"


I would go with Alexandre's first answer:

words := phrase substrings.
newPhrase := words copyAfter: 'sit'

It seems more intention revealing (i.e. clear) than either of the following, but just for fun, here's two other ways:

If, after profiling, you wanted more efficiency. This way is 25 times more efficient:

sitStart := phrase findWordStart: 'sit' startingAt: 1.
afterSit := sitStart + 4.
phrase copyFrom: afterSit to: phrase size.

PetitParser: 10 times less efficient, but a great tool to have in your pocket in more complex parsing situations:

sit := 'sit' asParser.
partToKeep := sit negate plus, sit, #any asParser plus flatten trim ==> [ :n | n third ].
partToKeep parse: phrase.

Also, you have to decide what to do if 'sit' is not found.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号