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

怎甘沉沦 提交于 2019-12-08 03:59:48

问题


I have a method that is takes in a future as parameter and also has a future inside it. I want to create a list from this method such that it takes values from the future that is passed in.

Case Classes

case class Color (colorName: String)
case class Shade (shadeName: String)
case class ColoShade (color: Color, shades: List[Shade])

Methods

val shadesFuture: Future[Seq[Shade]] = {
    val larSource: Future[Seq[LoanApplicationRegister]] =
      getMySource(ctx, id)
        .map(_.utf8String)
        .map(_.trim)
        .map(s => ShadeParser(s))
        .collect {
          case Right(shade) => shade
        }
        .runWith(Sink.seq)
}

//call the method
myMethod(shadesFuture)

//method definition
def myMethod(shadesFuture: Future[Seq][Shade]])
  :Future[ColorShade] {
    colorFuture
      .map(_.utf8String)
      .map(_.trim)
      .map { s =>
        ColorParser(s)
      }
      .collect {
        case Right(c) => c
      }
      .map { c =>  
         //How can I make an instance of ColorSade here? 
         //I tried 
         val colorShade = ColorShade(c, shadesFuture) 
         //but this doesn't work  

         //I would like to change this to instead be someOtherMethod(colorShade.c)
         someOtherMethod(c) 
      }
}

Question

How can I correctly return ColorShade from myMethod such that the shades property is the output from the passed in parameter shadesFuture?


回答1:


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)
    }  


来源:https://stackoverflow.com/questions/53796511/how-to-process-future-stream-to-create-an-instance-of-class-with-list-property

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