Unable to use for comprehension to map over List within Future

后端 未结 4 787

I have this issue that I have to work around every time. I can\'t map over something that is contained within a Future using a for comprehension.

Example:

         


        
4条回答
  •  孤城傲影
    2020-12-13 18:25

    I find this form more readable than either the serial map or the serial yield:

    for (vs <- future(data);
         xs = for (x <- vs) yield g(x)
    ) yield xs
    

    at the expense of the tupling map:

    f.map((_, xs)).map(_._2)
    

    or more precisely:

    f.map((vs: List[Int]) => (vs, for (x <- vs) yield g(x))).map(_._2)
    

提交回复
热议问题