Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 20 additions & 21 deletions streams/shared/src/main/scala/zio/stream/ZStream.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3188,28 +3188,27 @@ final class ZStream[-R, +E, +A] private (val channel: ZChannel[R, Any, Any, Any,
* Takes the last specified number of elements from this stream.
*/
def takeRight(n: => Int)(implicit trace: Trace): ZStream[R, E, A] =
ZStream.succeed(n).flatMap { n =>
if (n <= 0) ZStream.empty
else
new ZStream(
ZChannel.unwrap(
for {
queue <- ZIO.succeed(SingleThreadedRingBuffer[A](n))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure to understand why it was wrapped in a ZIO 🤔

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No clue either 🤷

} yield {
lazy val reader: ZChannel[Any, E, Chunk[A], Any, E, Chunk[A], Unit] = ZChannel.readWithCause(
(in: Chunk[A]) => {
in.foreach(queue.put)
reader
},
ZChannel.refailCause,
(_: Any) => ZChannel.write(queue.toChunk) *> ZChannel.unit
)
new ZStream(
ZChannel.suspend {
val n0 = n
if (n0 <= 0) ZChannel.unit
else {
val queue = SingleThreadedRingBuffer[A](n0)

(self.channel >>> reader)
}
)
)
}
lazy val reader: ZChannel[Any, E, Chunk[A], Any, E, Chunk[A], Unit] =
ZChannel.readWithCause(
in = (in: Chunk[A]) => {
in.foreach(queue.put)
reader
},
halt = ZChannel.refailCause,
done = (_: Any) => ZChannel.write(queue.toChunk) *> ZChannel.unit
)

self.channel >>> reader
}
}
)

/**
* Takes all elements of the stream until the specified predicate evaluates to
Expand Down
Loading