How to process future stream to create an instance of class with list property

爷,独闯天下 提交于 2019-12-09 02:19:29

I am not sure I understand what you mean ... But I think you are looking for something like this:

 def myMethod(shadesFuture: Future[Seq[Shade]]) : Future[ColorShade] = for {
   shades <- shadesFuture
   color <- colorFuture.map(...) // whatever the transformations you wanted
   transformedColor = ColorParser(color.utf8String.trim) // or you can do them like this

   colorShade = ColorShade(color, shades)
   // whatever you wanted to do with it here
   _ = someOtherMethod(colorShade.c, colorShade.shades)
 } yield colorShade // or whatever you need to return. The result of this is `Future[ColorShade]`

This is called "for-comprehension" and is syntactic sure for a bunch of nested .flatMap calls. You could also writer it explicitly (this is not exactly what for-comprehension is desugared into, but functionally equivalent):

 shadesFuture
    .flatMap { shades => 
       colorFuture
         .map(...)  // transform, whatever
         .map(ColorShade(_, shades)
    }  
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!