Very much like Pcollect, with one difference:
- Pcollect retrieves values from the source pattern one at a time, and applies the same function to every value.
- PcollectFinal applies one function to every value except the last one. A separate function is applied to the final value.
This is useful for embedding phrase patterns in a longer stream, where you want the final note of the phrase to be shorter.
PcollectFinal must look ahead in the stream by one value. It starts by getting two values and yielding the first. On the second call, it gets the third value and yields the second, and so on. This is the only way to know if the next value will be nil
.
Potential problems:
- If the source pattern uses Pkey to refer to values calculated outside PcollectFinal, the Pkey values will be out of sync (delayed by one event). The workaround is to wrap a Pbind inside PcollectFinal. See the examples.
- If the source pattern refers to the current clock time, this will be out of sync.
- If the source pattern generates a Rest, it may be applied incorrectly.
WARNING: These caveats suggest that this may not be an ideal solution. If I find a better approach later, I may remove this class. Avoid relying on it too much.
Arguments:
func |
The function that will be applied to the first n - 1 items. |
pattern |
The source pattern. |
finalFunc |
The function that will be applied to the final item. |