Given an OrderedCollection like this:
noise1
noise2
noise3
signal1
signal1
signal1
signal1
randomButInteresting
noise4
noise5
i want to select to a new OrderedCollection all the objects "signal1" and the object that comes after this series of "signal1"s, "randomButInteresting". (a series of one and the same signal occurs only once per Collection.)
What is the most elegant way to do this?
The straight method is something like
| data result lastWasSignal |
data := #( #noise1 #noise2 #noise3 #signal1 #signal1 #signal1 #signal1 #randomButInteresting #noise4 #noise5 ).
lastWasSignal := false.
result := data select: [ :value |
| isElementAppropriate |
isElementAppropriate := value = #signal1 or: [ lastWasSignal ].
lastWasSignal := value = #signal1.
isElementAppropriate
].
result
It's O(n). More clever would be to find bounds of signal group, which occurs only once, using binary search.
Using Lukas' version with PetitParser, but keeping all the 'signal1' in the result:
" the parser that accepts the symbol #signal1 "
signal := PPPredicateObjectParser expect: #signal1.
" the parser that accepts many symbols #signal1 followed by something else "
pattern := signal plus , signal negate.
data := #(noise1 noise2 noise3 signal1 signal1 signal1 signal1 randomButInteresting noise4 noise5).
pattern flatten matchesSkipIn: data -> an OrderedCollection(#(#signal1 #signal1 #signal1 #signal1 #randomButInteresting))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With