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

Skip to content
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 @@ -13,9 +13,9 @@ object ArbitraryStream {

val succeedingStream: Gen[Stream[String, T]] = genPureStream

val failingStreamEffect: Gen[StreamEffect[String, T]] = genFailingStreamEffect
val failingStreamEffect: Gen[StreamEffect[Any, String, T]] = genFailingStreamEffect

val succeedingStreamEffect: Gen[StreamEffect[String, T]] = genPureStreamEffect
val succeedingStreamEffect: Gen[StreamEffect[Any, String, T]] = genPureStreamEffect

Gen.oneOf(failingStream, succeedingStream, failingStreamEffect, succeedingStreamEffect)
}
Expand All @@ -33,10 +33,10 @@ object ArbitraryStream {
case (n, head :: rest) => IO.succeed(Some((head, (n - 1, rest))))
}

def genPureStreamEffect[T: ClassTag: Arbitrary]: Gen[StreamEffect[Nothing, T]] =
def genPureStreamEffect[T: ClassTag: Arbitrary]: Gen[StreamEffect[Any, Nothing, T]] =
Arbitrary.arbitrary[Iterable[T]].map(StreamEffect.fromIterable)

def genFailingStreamEffect[T: ClassTag: Arbitrary]: Gen[StreamEffect[String, T]] =
def genFailingStreamEffect[T: ClassTag: Arbitrary]: Gen[StreamEffect[Any, String, T]] =
for {
it <- Arbitrary.arbitrary[List[T]]
n <- Gen.choose(0, it.size)
Expand Down
82 changes: 41 additions & 41 deletions streams/shared/src/main/scala/zio/stream/StreamEffect.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ package zio.stream

import zio._

private[stream] class StreamEffect[+E, +A](val processEffect: Managed[E, () => A])
extends ZStream[Any, E, A](
private[stream] class StreamEffect[-R, +E, +A](val processEffect: ZManaged[R, E, () => A])
extends ZStream[R, E, A](
processEffect.map { thunk =>
UIO.effectTotal {
try UIO.succeed(thunk())
Expand All @@ -31,8 +31,8 @@ private[stream] class StreamEffect[+E, +A](val processEffect: Managed[E, () => A
}
) { self =>

override def collect[B](pf: PartialFunction[A, B]): StreamEffect[E, B] =
StreamEffect[E, B] {
override def collect[B](pf: PartialFunction[A, B]): StreamEffect[R, E, B] =
StreamEffect[R, E, B] {
self.processEffect.flatMap { thunk =>
Managed.effectTotal { () =>
{
Expand All @@ -48,8 +48,8 @@ private[stream] class StreamEffect[+E, +A](val processEffect: Managed[E, () => A
}
}

override def collectWhile[B](pred: PartialFunction[A, B]): StreamEffect[E, B] =
StreamEffect[E, B] {
override def collectWhile[B](pred: PartialFunction[A, B]): StreamEffect[R, E, B] =
StreamEffect[R, E, B] {
self.processEffect.flatMap { thunk =>
Managed.effectTotal {
var done = false
Expand All @@ -62,8 +62,8 @@ private[stream] class StreamEffect[+E, +A](val processEffect: Managed[E, () => A
}
}

override def dropWhile(pred: A => Boolean): StreamEffect[E, A] =
StreamEffect[E, A] {
override def dropWhile(pred: A => Boolean): StreamEffect[R, E, A] =
StreamEffect[R, E, A] {
self.processEffect.flatMap { thunk =>
Managed.effectTotal {
var drop = true
Expand All @@ -83,8 +83,8 @@ private[stream] class StreamEffect[+E, +A](val processEffect: Managed[E, () => A
}
}

override def filter(pred: A => Boolean): StreamEffect[E, A] =
StreamEffect[E, A] {
override def filter(pred: A => Boolean): StreamEffect[R, E, A] =
StreamEffect[R, E, A] {
self.processEffect.flatMap { thunk =>
Managed.effectTotal {
@annotation.tailrec
Expand All @@ -98,7 +98,7 @@ private[stream] class StreamEffect[+E, +A](val processEffect: Managed[E, () => A
}
}

final def foldLazyPure[S](s: S)(cont: S => Boolean)(f: (S, A) => S): Managed[E, S] =
final def foldLazyPure[S](s: S)(cont: S => Boolean)(f: (S, A) => S): ZManaged[R, E, S] =
processEffect.flatMap { thunk =>
def fold(): Either[E, S] = {
var state = s
Expand All @@ -121,17 +121,17 @@ private[stream] class StreamEffect[+E, +A](val processEffect: Managed[E, () => A
Managed.effectTotal(Managed.fromEither(fold())).flatten
}

override def map[B](f0: A => B): StreamEffect[E, B] =
StreamEffect[E, B] {
override def map[B](f0: A => B): StreamEffect[R, E, B] =
StreamEffect[R, E, B] {
self.processEffect.flatMap { thunk =>
Managed.effectTotal { () =>
f0(thunk())
}
}
}

override def mapAccum[S1, B](s1: S1)(f1: (S1, A) => (S1, B)): StreamEffect[E, B] =
StreamEffect[E, B] {
override def mapAccum[S1, B](s1: S1)(f1: (S1, A) => (S1, B)): StreamEffect[R, E, B] =
StreamEffect[R, E, B] {
self.processEffect.flatMap { thunk =>
Managed.effectTotal {
var state = s1
Expand All @@ -145,8 +145,8 @@ private[stream] class StreamEffect[+E, +A](val processEffect: Managed[E, () => A
}
}

override def mapConcat[B](f: A => Chunk[B]): StreamEffect[E, B] =
StreamEffect[E, B] {
override def mapConcat[B](f: A => Chunk[B]): StreamEffect[R, E, B] =
StreamEffect[R, E, B] {
self.processEffect.flatMap { thunk =>
Managed.effectTotal {
var chunk: Chunk[B] = Chunk.empty
Expand All @@ -165,18 +165,18 @@ private[stream] class StreamEffect[+E, +A](val processEffect: Managed[E, () => A
}
}

override def run[R, E1 >: E, A0, A1 >: A, B](sink: ZSink[R, E1, A0, A1, B]): ZIO[R, E1, B] =
override def run[R1 <: R, E1 >: E, A0, A1 >: A, B](sink: ZSink[R1, E1, A0, A1, B]): ZIO[R1, E1, B] =
sink match {
case sink: SinkPure[E1, A0, A1, B] =>
foldLazyPure[sink.State](sink.initialPure)(sink.cont)(sink.stepPure).use[Any, E1, B] { state =>
foldLazyPure[sink.State](sink.initialPure)(sink.cont)(sink.stepPure).use[R1, E1, B] { state =>
ZIO.fromEither(sink.extractPure(state).map(_._1))
}

case sink: ZSink[R, E1, A0, A1, B] => super.run(sink)
case sink: ZSink[R1, E1, A0, A1, B] => super.run(sink)
}

override def take(n: Int): StreamEffect[E, A] =
StreamEffect[E, A] {
override def take(n: Int): StreamEffect[R, E, A] =
StreamEffect[R, E, A] {
self.processEffect.flatMap { thunk =>
Managed.effectTotal {
var counter = 0
Expand All @@ -192,8 +192,8 @@ private[stream] class StreamEffect[+E, +A](val processEffect: Managed[E, () => A
}
}

override def takeWhile(pred: A => Boolean): StreamEffect[E, A] =
StreamEffect[E, A] {
override def takeWhile(pred: A => Boolean): StreamEffect[R, E, A] =
StreamEffect[R, E, A] {
self.processEffect.flatMap { thunk =>
Managed.effectTotal { () =>
{
Expand All @@ -205,12 +205,12 @@ private[stream] class StreamEffect[+E, +A](val processEffect: Managed[E, () => A
}
}

override def transduce[R, E1 >: E, A1 >: A, B](
sink: ZSink[R, E1, A1, A1, B]
): ZStream[R, E1, B] =
override def transduce[R1 <: R, E1 >: E, A1 >: A, B](
sink: ZSink[R1, E1, A1, A1, B]
): ZStream[R1, E1, B] =
sink match {
case sink: SinkPure[E1, A1, A1, B] =>
StreamEffect[E1, B] {
StreamEffect[R1, E1, B] {

self.processEffect.flatMap { thunk =>
Managed.effectTotal {
Expand Down Expand Up @@ -257,7 +257,7 @@ private[stream] class StreamEffect[+E, +A](val processEffect: Managed[E, () => A
}
}
}
case sink: ZSink[R, E1, A1, A1, B] => super.transduce(sink)
case sink: ZSink[R1, E1, A1, A1, B] => super.transduce(sink)
}
}

Expand All @@ -275,18 +275,18 @@ private[stream] object StreamEffect extends Serializable {

def fail[E, A](e: E): A = throw Failure(e)

final val empty: StreamEffect[Nothing, Nothing] =
StreamEffect[Nothing, Nothing] {
final val empty: StreamEffect[Any, Nothing, Nothing] =
StreamEffect[Any, Nothing, Nothing] {
Managed.effectTotal { () =>
end
}
}

final def apply[E, A](pull: Managed[E, () => A]): StreamEffect[E, A] =
new StreamEffect[E, A](pull)
final def apply[R, E, A](pull: ZManaged[R, E, () => A]): StreamEffect[R, E, A] =
new StreamEffect[R, E, A](pull)

final def fromChunk[@specialized A](c: Chunk[A]): StreamEffect[Nothing, A] =
StreamEffect[Nothing, A] {
final def fromChunk[@specialized A](c: Chunk[A]): StreamEffect[Any, Nothing, A] =
StreamEffect[Any, Nothing, A] {
Managed.effectTotal {
var index = 0
val len = c.length
Expand All @@ -302,17 +302,17 @@ private[stream] object StreamEffect extends Serializable {
}
}

final def fromIterable[A](as: Iterable[A]): StreamEffect[Nothing, A] =
StreamEffect[Nothing, A] {
final def fromIterable[A](as: Iterable[A]): StreamEffect[Any, Nothing, A] =
StreamEffect[Any, Nothing, A] {
Managed.effectTotal {
val thunk = as.iterator

() => if (thunk.hasNext) thunk.next() else end
}
}

final def unfold[S, A](s: S)(f0: S => Option[(A, S)]): StreamEffect[Nothing, A] =
StreamEffect[Nothing, A] {
final def unfold[S, A](s: S)(f0: S => Option[(A, S)]): StreamEffect[Any, Nothing, A] =
StreamEffect[Any, Nothing, A] {
Managed.effectTotal {
var state = s

Expand All @@ -327,8 +327,8 @@ private[stream] object StreamEffect extends Serializable {
}
}

final def succeed[A](a: A): StreamEffect[Nothing, A] =
StreamEffect[Nothing, A] {
final def succeed[A](a: A): StreamEffect[Any, Nothing, A] =
StreamEffect[Any, Nothing, A] {
Managed.effectTotal {
var done = false

Expand Down
28 changes: 14 additions & 14 deletions streams/shared/src/main/scala/zio/stream/StreamEffectChunk.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ package zio.stream

import zio._

private[stream] class StreamEffectChunk[+E, +A](override val chunks: StreamEffect[E, Chunk[A]])
extends ZStreamChunk[Any, E, A](chunks) { self =>
private[stream] class StreamEffectChunk[-R, +E, +A](override val chunks: StreamEffect[R, E, Chunk[A]])
extends ZStreamChunk[R, E, A](chunks) { self =>

override def drop(n: Int): StreamEffectChunk[E, A] =
override def drop(n: Int): StreamEffectChunk[R, E, A] =
StreamEffectChunk {
StreamEffect {
self.chunks.processEffect.flatMap { thunk =>
Managed.effectTotal {
ZManaged.effectTotal {
var counter = n

def pull(): Chunk[A] = {
Expand All @@ -45,9 +45,9 @@ private[stream] class StreamEffectChunk[+E, +A](override val chunks: StreamEffec
}
}

override def dropWhile(pred: A => Boolean): StreamEffectChunk[E, A] =
override def dropWhile(pred: A => Boolean): StreamEffectChunk[R, E, A] =
StreamEffectChunk {
StreamEffect[E, Chunk[A]] {
StreamEffect[R, E, Chunk[A]] {
self.chunks.processEffect.flatMap { thunk =>
Managed.effectTotal {
var keepDropping = true
Expand All @@ -73,24 +73,24 @@ private[stream] class StreamEffectChunk[+E, +A](override val chunks: StreamEffec
}
}

override def filter(pred: A => Boolean): StreamEffectChunk[E, A] =
override def filter(pred: A => Boolean): StreamEffectChunk[R, E, A] =
StreamEffectChunk(chunks.map(_.filter(pred)))

final def foldLazyPure[S](s: S)(cont: S => Boolean)(f: (S, A) => S): Managed[E, S] =
final def foldLazyPure[S](s: S)(cont: S => Boolean)(f: (S, A) => S): ZManaged[R, E, S] =
chunks.foldLazyPure(s)(cont) { (s, as) =>
as.foldLeftLazy(s)(cont)(f)
}

override def foldLeft[S](s: S)(f: (S, A) => S): IO[E, S] =
override def foldLeft[S](s: S)(f: (S, A) => S): ZIO[R, E, S] =
foldLazyPure(s)(_ => true)(f).use(UIO.succeed)

override def map[@specialized B](f: A => B): StreamEffectChunk[E, B] =
override def map[@specialized B](f: A => B): StreamEffectChunk[R, E, B] =
StreamEffectChunk(chunks.map(_.map(f)))

override def mapConcatChunk[B](f: A => Chunk[B]): StreamEffectChunk[E, B] =
override def mapConcatChunk[B](f: A => Chunk[B]): StreamEffectChunk[R, E, B] =
StreamEffectChunk(chunks.map(_.flatMap(f)))

override def take(n: Int): StreamEffectChunk[E, A] =
override def take(n: Int): StreamEffectChunk[R, E, A] =
StreamEffectChunk {
StreamEffect {
self.chunks.processEffect.flatMap { thunk =>
Expand All @@ -112,7 +112,7 @@ private[stream] class StreamEffectChunk[+E, +A](override val chunks: StreamEffec
}
}

override def takeWhile(pred: A => Boolean): StreamEffectChunk[E, A] =
override def takeWhile(pred: A => Boolean): StreamEffectChunk[R, E, A] =
StreamEffectChunk {
StreamEffect {
self.chunks.processEffect.flatMap { thunk =>
Expand All @@ -138,6 +138,6 @@ private[stream] class StreamEffectChunk[+E, +A](override val chunks: StreamEffec
}

private[stream] object StreamEffectChunk extends Serializable {
final def apply[E, A](chunks: StreamEffect[E, Chunk[A]]): StreamEffectChunk[E, A] =
final def apply[R, E, A](chunks: StreamEffect[R, E, Chunk[A]]): StreamEffectChunk[R, E, A] =
new StreamEffectChunk(chunks)
}