How to put contents of stream into a val?

眉间皱痕 提交于 2020-01-06 04:13:30

问题


I have a stream like this:

def myStream[T: AS: MAT](source: Source[T, NotUsed]): Future[Seq[T]] = {
    return source.runWith(Sink.seq)
}

def myMethod(colorStream: Source[Color, NotUsed]) {
  val allColors = myStream(colorStream).map(_.toList)

  //how can I actually extract the things from allColors
  //so that I can call my method below? myOtherMethod

  if I do println(allColors.map(println _)) I can print the elements fine
}

def myOtherMethod(colors: Seq[Color] = List.empty()) {
 ...
}

回答1:


allColors is a Future. You need to access what the future is wrapping to access the colours:Seq[Color]. Try this:

allColors.onComplete{
  case Success(list) => myOtherMethod(list)
  case Failure(err) => //handle the error
}


来源:https://stackoverflow.com/questions/53798622/how-to-put-contents-of-stream-into-a-val

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!