如何在另一个计算中重用队列?流Scala

如何在另一个计算中重用队列?流Scala,scala,queue,code-reuse,fs2,Scala,Queue,Code Reuse,Fs2,我得到的错误是Unit而不是Stream[IO,String]。 我正在尝试在下一个队列中重用队列的结果 import cats.effect.{ExitCode, IO, IOApp, Timer} import fs2.Stream import fs2.concurrent.Queue import scala.concurrent.duration._ import scala.util.Random class StreamTypeIntToDouble(q1: Queue[IO,

我得到的错误是
Unit
而不是
Stream[IO,String]
。 我正在尝试在下一个队列中重用队列的结果

import cats.effect.{ExitCode, IO, IOApp, Timer}
import fs2.Stream
import fs2.concurrent.Queue

import scala.concurrent.duration._
import scala.util.Random

class StreamTypeIntToDouble(q1: Queue[IO, Int], q2: Queue[IO, String])(
  implicit timer: Timer[IO]
) {

  def storeInQueueFirst: Stream[IO, Unit] = {
    Stream(1, 2, 3)
      .covary[IO]
      .evalTap(n => IO.delay(println(s"Pushing $n to Queue First")))
      .metered(Random.between(1, 20).seconds)
      .through(q1.enqueue)

  }
  def getFromQueueFirst: Stream[IO, Unit] = {
    q1.dequeue
      .evalMap(n => IO.delay(println(s"Pulling from queue Second $n")))

  }
  def storeInQueueSecond(s: Stream[IO, Int]): Stream[IO, Unit] = {
    s.map { n =>
        n.toString
      }
      .metered(Random.between(1, 20).seconds)
      .through(q2.enqueue)
  }

  def getFromQueueSecond: Stream[IO, Unit] = {
    q2.dequeue
      .evalMap(n => IO.delay(println(s"Pulling from queue second $n")))
  }
}

object Five extends IOApp {
  override def run(args: List[String]): IO[ExitCode] = {
    val program = for {
      q1 <- Queue.bounded[IO, Int](10)
      q2 <- Queue.bounded[IO, String](10)

      b = new StreamTypeIntToDouble(q1, q2)
      _ <- b.storeInQueueFirst.compile.drain.start
      a <- b.getFromQueueFirst.compile.drain
      _ <- b.storeInQueueSecond(a).compile.drain
      _ <- b.getFromQueueSecond.compile.drain
    } yield ()
    program.as(ExitCode.Success)
  }
}
导入cats.effect.{ExitCode,IO,IOApp,Timer}
导入fs2.Stream
导入fs2.concurrent.Queue
导入scala.concurrent.duration_
导入scala.util.Random
类StreamTypeIntToDouble(q1:Queue[IO,Int],q2:Queue[IO,String])(
隐式计时器:计时器[IO]
) {
def storeInQueueFirst:流[IO,单位]={
溪流(1、2、3)
.covary[IO]
.evalTap(n=>IO.delay(println(s“先将$n推送到队列”))
.计量(随机。介于(1,20)秒之间)
.至(q1.排队)
}
def getFromQueueFirst:流[IO,单位]={
q1.退出队列
.evalMap(n=>IO.delay(println(s“从队列中拉出第二个$n”))
}
def storeInQueueSecond(s:Stream[IO,Int]):Stream[IO,Unit]={
s、 映射{n=>
n、 托斯特林
}
.计量(随机。介于(1,20)秒之间)
.至(第2季度排队)
}
def getFromQueueSecond:流[IO,单位]={
问题2.退出队列
.evalMap(n=>IO.delay(println(s“从队列中拉出第二个$n”))
}
}
对象5扩展了IOApp{
覆盖def运行(args:List[String]):IO[ExitCode]={
val程序=用于{

q1尝试更改
getFromQueueFirst
,使其生成
Stream[IO,Int]
而不是
Stream[IO,Unit]

def getFromQueueFirst: Stream[IO, Int] = {
  q1.dequeue
    evalTap(n => IO.delay(println(s"Pulling from queue Second $n")))
}
然后

val程序=用于{

q1为什么当我删除最后一个错误并将其放在排水管上时,它不起作用?你能提供图片吗please@MunaAr
.drain
返回
IO[单位]
,所以
a
单元
。不幸的是,即使你回答了这个问题,这个问题仍然存在,我已经打开了一个新的问题。如果你能将它检查出相等的问题,那就太好了
val program = for {
  q1 <- Queue.bounded[IO, Int](10)
  q2 <- Queue.bounded[IO, String](10)

  b = new StreamTypeIntToDouble(q1, q2)
  _ <- b.storeInQueueFirst.compile.drain.start
  a <- b.getFromQueueFirst.compile.lastOrError
  _ <- b.storeInQueueSecond(Stream(a)).compile.drain
  _ <- b.getFromQueueSecond.compile.drain
} yield ()