import { concreteStream, StreamInternal } from "@effect/core/stream/Stream/operations/_internal/StreamInternal" import { RingBufferNew } from "@effect/core/support/RingBufferNew" /** * Drops the last specified number of elements from this stream. * * Note: this combinator keeps `n` elements in memory. Be careful with big * numbers. * * @tsplus static effect/core/stream/Stream.Aspects dropRight * @tsplus pipeable effect/core/stream/Stream dropRight */ export function dropRight(n: number) { return (self: Stream): Stream => { if (n <= 0) { return self } return Stream.sync(new RingBufferNew(n)).flatMap((queue) => { const reader: Channel< never, E, Chunk, unknown, E, Chunk, void > = Channel.readWith( (chunk: Chunk) => { const outs = chunk.collect((elem) => { const head = queue.head() queue.put(elem) return head }) return Channel.write(outs).flatMap(() => reader) }, (err) => Channel.fail(err), () => Channel.unit ) concreteStream(self) return new StreamInternal(self.channel >> reader) }) } }