8000 Add ZStream.branchAfter and pipeline by regiskuckaertz · Pull Request #5951 · zio/zio · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add ZStream.branchAfter and pipeline #5951

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 31, 2021
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,91 @@ object ZStreamSpec extends ZIOBaseSpec {
} yield assert(leftAssoc -> rightAssoc)(equalTo(true -> true))
)
),
suite("branchAfter")(
test("switches pipelines") {
check(Gen.chunkOf(Gen.int)) { data =>
val test =
ZStream
.fromChunk(0 +: data)
.branchAfter(1) { values =>
values.toList match {
case 0 :: Nil => ZPipeline[Int]
case _ => ZPipeline.fail("boom")
}
}
.runCollect
assertM(test.exit)(succeeds(equalTo(data)))
}
},
// test("finalizes transducers") {
// check(Gen.chunkOf(Gen.int)) { data =>
// val test =
// Ref.make(0).flatMap { ref =>
// ZStream
// .fromChunk(data)
// .branchAfter(1) { values =>
// values.toList match {
// case _ =>
// ZTransducer {
// Managed.acquireReleaseWith(
// ref
// .update(_ + 1)
// .as[Option[Chunk[Int]] => UIO[Chunk[Int]]] {
// case None => ZIO.succeedNow(Chunk.empty)
// case Some(c) => ZIO.succeedNow(c)
// }
// )(_ => ref.update(_ - 1))
// }
// }
// }
// .runDrain *> ref.get
// }
// assertM(test.exit)(succeeds(equalTo(0)))
// }
// },
// test("finalizes transducers - inner transducer fails") {
// check(Gen.chunkOf(Gen.int)) { data =>
// val test =
// Ref.make(0).flatMap { ref =>
// ZStream
// .fromChunk(data)
// .branchAfter(1) { values =>
// values.toList match {
// case _ =>
// ZTransducer {
// Managed.acquireReleaseWith(
// ref
// .update(_ + 1)
// .as[Option[Chunk[Int]] => IO[String, Chunk[Int]]] { case _ =>
// ZIO.fail("boom")
// }
// )(_ => ref.update(_ - 1))
// }
// }
// }
// .runDrain
// .ignore *> ref.get
// }
// assertM(test.exit)(succeeds(equalTo(0)))
// }
// },
test("emits data if less than n are collected") {
val gen =
for {
data <- Gen.chunkOf(Gen.int)
n <- Gen.int.filter(_ > data.length)
} yield (data, n)

check(gen) { case (data, n) =>
val test =
ZStream
.fromChunk(data)
.branchAfter(n)(ZPipeline.prepend)
.runCollect
assertM(test.exit)(succeeds(equalTo(data)))
}
}
),
suite("broadcast")(
test("Values") {
ZStream
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ object ZPipeline {
*/
def apply[I]: ZPipeline[Any, Nothing, I, I] = identity[I]

def branchAfter[R, E, I](n: Int)(f: Chunk[I] => ZPipeline[R, E, I, I]): ZPipeline[R, E, I, I] =
new ZPipeline[R, E, I, I] {
def apply[R1 <: R, E1 >: E](stream: ZStream[R1, E1, I])(implicit trace: ZTraceElement): ZStream[R1, E1, I] =
stream.branchAfter(n)(f)
}

/**
* Creates a pipeline that collects elements with the specified partial function.
*
Expand Down Expand Up @@ -397,6 +403,15 @@ object ZPipeline {
stream.mapZIO(f)
}

/**
* Emits the provided chunk before emitting any other value.
*/
def prepend[A](values: Chunk[A]): ZPipeline[Any, Nothing, A, A] =
new ZPipeline[Any, Nothing, A, A] {
def apply[R, E](stream: ZStream[R, E, A])(implicit trace: ZTraceElement): ZStream[R, E, A] =
ZStream.fromChunk(values) ++ stream
}

/**
* Creates a pipeline that always succeeds with the specified value.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,13 @@ class ZStream[-R, +E, +A](val channel: ZChannel[R, Any, Any, Any, E, Chunk[A], A
def >>=[R1 <: R, E1 >: E, A2](f0: A => ZStream[R1, E1, A2])(implicit trace: ZTraceElement): ZStream[R1, E1, A2] =
flatMap(f0)

// /**
// * Symbolic alias for [[ZStream#transduce]].
// */
// def >>>[R1 <: R, E1 >: E, A2 >: A, A3](transducer: ZTransducer[R1, E1, A2, A3]) =
// transduce(transducer)
/**
* Symbolic alias for [[ZStream#via]].
*/
def >>>[R1 <: R, E1 >: E, A2 >: A, A3](pipeline: ZPipeline[R1, E1, A2, A3])(implicit
trace: ZTraceElement
): ZStream[R1, E1, A3] =
via(pipeline)

/**
* Symbolic alias for [[[zio.stream.ZStream!.run[R1<:R,E1>:E,B]*]]].
Expand Down Expand Up @@ -245,6 +247,44 @@ class ZStream[-R, +E, +A](val channel: ZChannel[R, Any, Any, Any, E, Chunk[A], A
def as[A2](A2: => A2)(implicit trace: ZTraceElement): ZStream[R, E, A2] =
map(_ => A2)

/**
* Reads the first n values from the stream and uses them to choose the pipeline that will be
* used for the remainder of the stream.
*/
def branchAfter[R1 <: R, E1 >: E, A1 >: A](
n: Int
)(f: Chunk[A1] => ZPipeline[R1, E1, A1, A1])(implicit trace: ZTraceElement): ZStream[R1, E1, A1] = {
def collecting(buf: Chunk[A1]): ZChannel[R1, E1, Chunk[A1], Any, E1, Chunk[A1], Any] =
ZChannel.readWithCause(
(chunk: Chunk[A1]) => {
val newBuf = buf ++ chunk
if (newBuf.length >= n) {
val (is, is1) = newBuf.splitAt(n)
val pipeline = f(is)
pipeline(ZStream.fromChunk(is1)).channel *> emitting(pipeline)
} else
collecting(newBuf)
},
(cause: Cause[E1]) => ZChannel.failCause(cause),
(_: Any) =>
if (buf.isEmpty)
ZChannel.unit
else {
val pipeline = f(buf)
pipeline(ZStream.empty).channel
}
)

def emitting(pipeline: ZPipeline[R1, E1, A1, A1]): ZChannel[R1, E1, Chunk[A1], Any, E1, Chunk[A1], Any] =
ZChannel.readWithCause(
(chunk: Chunk[A1]) => pipeline(ZStream.fromChunk(chunk)).channel *> emitting(pipeline),
(cause: Cause[E1]) =& 9DFA gt; ZChannel.failCause(cause),
(_: Any) => ZChannel.unit
)

new ZStream(self.channel >>> collecting(Chunk.empty))
}

/**
* Returns a stream whose failure and success channels have been mapped by
* the specified pair of functions, `f` and `g`.
Expand Down Expand Up @@ -3776,7 +3816,16 @@ class ZStream[-R, +E, +A](val channel: ZChannel[R, Any, Any, Any, E, Chunk[A], A
*/
final def via[R2, E2, A2](f: ZStream[R, E, A] => ZStream[R2, E2, A2])(implicit
trace: ZTraceElement
): ZStream[R2, E2, A2] = f(self)
): ZStream[R2, E2, A2] =
f(self)

/**
* Threads the stream through a transformation pipeline.
*/
final def via[R2 <: R, E2 >: E, A2 >: A, A3](pipeline: ZPipeline[R2, E2, A2, A3])(implicit
trace: ZTraceElement
): ZStream[R2, E2, A3] =
pipeline(self)

/**
* Returns this stream if the specified condition is satisfied, otherwise returns an empty stream.
Expand Down
0