0
  // Simulated external API that synchronously returns elements one at a time indefinitely.
  def externalApiGet[A](): A = ???

  // This wraps with the proper fs2 stream that will indefinitely return values.
  def wrapGetWithFS2[A](): Stream[Task, A] = Stream.eval(Task.delay(externalApiGet))

  // Simulated external API that synchronously returns "chunks" of elements at a time indefinitely.
  def externalApiGetSeq[A](): Seq[A] = ???

  // How do I wrap this with a stream that hides the internal chunks and just provides a stream of A values.
  // The following doesn't compile. I need help fixing this.
  def wrapGetSeqWithFS2[A](): Stream[Task, A] = Stream.eval(Task.delay(externalApiGetSeq))
clay
  • 18,138
  • 28
  • 107
  • 192

1 Answers1

3

You need to mark the sequence as a Chunk and then use flatMap to flatten the stream.

def wrapGetSeqWithFS2[A](): Stream[Task, A] =
  Stream.eval(Task.delay(externalApiGetSeq()))
    .flatMap(Stream.emits)

(Edited to simplify solution)

sortega
  • 1,128
  • 9
  • 15
  • Answer is of course correct. If I may suggest sth, there is a method `def emits[F[_],A](a: Seq[A]): Stream[F,A] = chunk(Chunk.seq(a))` so the code simplifies to: `Stream.eval(Task.delay(externalApiGetSeq())).flatMap (Stream.emits)` – almendar Dec 28 '16 at 23:26