Skip to content

Commit 87f3229

Browse files
committed
Docs for conflate functions
1 parent 33320fa commit 87f3229

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

core/shared/src/main/scala/fs2/Stream.scala

+21-4
Original file line numberDiff line numberDiff line change
@@ -568,32 +568,49 @@ final class Stream[+F[_], +O] private[fs2] (private[fs2] val underlying: Pull[F,
568568
Stream.eval(fstream)
569569
}
570570

571+
/** Pulls up to the specified number of chunks from the source stream while concurrently allowing
572+
* downstream to process emitted chunks. Unlike `prefetchN`, all accumulated chunks are emitted
573+
* as a single chunk upon downstream pulling.
574+
*
575+
* The `chunkLimit` parameter controls backpressure on the source stream.
576+
*/
571577
def conflateChunks[F2[x] >: F[x]: Concurrent](chunkLimit: Int): Stream[F2, Chunk[O]] =
572578
Stream.eval(Channel.bounded[F2, Chunk[O]](chunkLimit)).flatMap { chan =>
573579
val producer = chunks.through(chan.sendAll)
574580
val consumer = chan.stream.chunks.map(_.combineAll)
575581
consumer.concurrently(producer)
576582
}
577583

584+
/** Like `conflateChunks` but uses the supplied `zero` and `f` values to combine the elements of
585+
* each output chunk in to a single value.
586+
*/
578587
def conflate[F2[x] >: F[x]: Concurrent, O2](chunkLimit: Int, zero: O2)(
579588
f: (O2, O) => O2
580589
): Stream[F2, O2] =
581590
conflateChunks[F2](chunkLimit).map(_.foldLeft(zero)(f))
582591

592+
/** Like `conflate` but combines elements of the output chunk with the supplied function.
593+
*/
583594
def conflate1[F2[x] >: F[x]: Concurrent, O2 >: O](chunkLimit: Int)(
584595
f: (O2, O2) => O2
585596
): Stream[F2, O2] =
586597
conflateChunks[F2](chunkLimit).map(c => c.drop(1).foldLeft(c(0): O2)((x, y) => f(x, y)))
587598

599+
/** Like `conflate1` but combines elements using the semigroup of the output type.
600+
*/
588601
def conflateSemigroup[F2[x] >: F[x]: Concurrent, O2 >: O: Semigroup](
589602
chunkLimit: Int
590603
): Stream[F2, O2] =
591604
conflate1[F2, O2](chunkLimit)(Semigroup[O2].combine)
592605

593-
def conflateMap[F2[x] >: F[x]: Concurrent, O2: Semigroup](chunkLimit: Int)(
594-
f: O => O2
595-
): Stream[F2, O2] =
596-
map(f).conflateSemigroup[F2, O2](chunkLimit)
606+
/** Conflates elements and then maps the supplied function over the output chunk and combines the results using a semigroup.
607+
*/
608+
def conflateMap[F2[x] >: F[x]: Concurrent, O2: Semigroup](
609+
chunkLimit: Int
610+
)(f: O => O2): Stream[F2, O2] =
611+
conflateChunks[F2](chunkLimit).map(c =>
612+
c.drop(1).foldLeft(f(c(0)))((x, y) => Semigroup[O2].combine(x, f(y)))
613+
)
597614

598615
/** Prepends a chunk onto the front of this stream.
599616
*

0 commit comments

Comments
 (0)