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
12 changes: 12 additions & 0 deletions streams-tests/shared/src/test/scala/zio/stream/ZStreamSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,18 @@ object ZStreamSpec extends ZIOBaseSpec {
.map(assert(_)(isLeft(equalTo("Boom"))))
}
),
testM("changes") {
checkM(pureStreamOfInts) { stream =>
for {
actual <- stream.changes.runCollect.map(_.toList)
expected <- stream.runCollect.map { as =>
as.foldLeft[List[Int]](Nil) { (s, n) =>
if (s.isEmpty || s.head != n) n :: s else s
}.reverse
}
} yield assert(actual)(equalTo(expected))
}
},
testM("collect") {
assertM(ZStream(Left(1), Right(2), Left(3)).collect { case Right(n) =>
n
Expand Down
30 changes: 30 additions & 0 deletions streams/shared/src/main/scala/zio/stream/ZStream.scala
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,36 @@ abstract class ZStream[-R, +E, +O](val process: ZManaged[R, Nothing, ZIO[R, Opti
): ZStream[R1, E1, O1] =
catchAllCause(pf.applyOrElse[Cause[E], ZStream[R1, E1, O1]](_, ZStream.halt(_)))

/**
* Returns a new stream that only emits elements that are not equal to the
* previous element emitted, using natural equality to determine whether two
* elements are equal.
*/
def changes: ZStream[R, E, O] =
Copy link
Member

Choose a reason for hiding this comment

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

Would dedup be a better name? (Not sure about that.)

Copy link
Contributor Author

@adamgfraser adamgfraser Mar 16, 2021

Choose a reason for hiding this comment

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

Yes I wondered about that too. This is distinctUntilChanged in reactive streams and removeDuplicates in some other frameworks so deduplicate is definitely reasonable. The slightly tricky thing is it doesn't necessary remove all duplicates in the sense that if the stream emits A, A, B, A the resulting stream will be A, B, A so you will still have two A values just not two consecutive ones.

Copy link
Member

Choose a reason for hiding this comment

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

I'm ok with changes especially if there is no clear pattern.

changesWith(_ == _)

/**
* Returns a new stream that only emits elements that are not equal to the
* previous element emitted, using the specified function to determine
* whether two elements are equal.
*/
def changesWith(f: (O, O) => Boolean): ZStream[R, E, O] =
ZStream {
for {
ref <- Ref.makeManaged[Option[O]](None)
p <- self.process
pull = for {
z0 <- ref.get
c <- p
(z1, chunk) = c.foldLeft[(Option[O], Chunk[O])]((z0, Chunk.empty)) {
case ((Some(o), os), o1) if (f(o, o1)) => (Some(o1), os)
case ((_, os), o1) => (Some(o1), os :+ o1)
}
_ <- ref.set(z1)
} yield chunk
} yield pull
}

/**
* Re-chunks the elements of the stream into chunks of
* `n` elements each.
Expand Down