From 0cf96cb9e688624c201103df28d1a97d8e5ba40b Mon Sep 17 00:00:00 2001 From: Arman Bilge Date: Mon, 26 Jul 2021 13:04:55 +0000 Subject: [PATCH] Implement `chunks` (inverse of `unchunks`) --- core/shared/src/main/scala/fs2/Stream.scala | 4 ++++ core/shared/src/test/scala/fs2/StreamSuite.scala | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/core/shared/src/main/scala/fs2/Stream.scala b/core/shared/src/main/scala/fs2/Stream.scala index 0da712afd8..9f5a39df91 100644 --- a/core/shared/src/main/scala/fs2/Stream.scala +++ b/core/shared/src/main/scala/fs2/Stream.scala @@ -2596,6 +2596,10 @@ final class Stream[+F[_], +O] private[fs2] (private[fs2] val underlying: Pull[F, } } + /** Flattens a stream of chunks. Inverse of [[chunks]]. */ + def unchunks[O2](implicit ev: O <:< Chunk[O2]): Stream[F, O2] = + flatMap(Stream.chunk(_)) + /** Alias for [[filter]] * Implemented to enable filtering in for comprehensions */ diff --git a/core/shared/src/test/scala/fs2/StreamSuite.scala b/core/shared/src/test/scala/fs2/StreamSuite.scala index 4326812ddb..b0649485b3 100644 --- a/core/shared/src/test/scala/fs2/StreamSuite.scala +++ b/core/shared/src/test/scala/fs2/StreamSuite.scala @@ -133,10 +133,10 @@ class StreamSuite extends Fs2Suite { } } - property("chunks.flatMap(chunk) identity") { + property("chunks.unchunks identity") { forAll { (v: Vector[Vector[Int]]) => val s = if (v.isEmpty) Stream.empty else v.map(Stream.emits).reduce(_ ++ _) - assertEquals(s.chunks.flatMap(Stream.chunk).toVector, v.flatten) + assertEquals(s.chunks.unchunks.toVector, v.flatten) } } }