dropWhileRight
Drops the longest run at the end of a source where a predicate holds — trimming a trailing run.
Lecture
This is the trim operator. Trailing zeros, trailing blank lines, a tail of
placeholder rows — anything you want gone from the end without counting it
first, which is all dropRight
can do.
Only the run that reaches the end is dropped. A matching run in the middle
of the source is not a suffix and stays exactly where it is — that is the
one thing worth checking against your intuition, since
filter and
reject would have removed it.
Unlike takeWhileRight, this
one streams. A matching run is held back rather than emitted, because it
might turn out to be the suffix; the first element that fails the
predicate proves it was not, and releases the whole run at once. So the
memory cost is the longest run, not the length of the source, and values
keep flowing while the source is still open.
Over a List the run is found by walking backwards from the
end, so the predicate only sees that run; over any other source every
element is tested in order. Keep the predicate pure.
Demo 1 · Basics
Demo 2 · Async, and the split
Try it yourself
Exercise: drop the trailing zeros and keep the rest.
takeWhileRight — the complement: keep that same run ·
dropRight — trim a trailing slice by count ·
skipWhile — the same idea from the front ·
whereNot — removes matches anywhere, not just at the end