Combining Scala Futures and collections in for comprehensions
scala
Solution
`flatMap` requires that the type constructors of `numberList` and `squareInTheFuture(number)` are the same (modulo whatever implicit conversions the collection library does). That isn't the case here. Instead, this is a traversal:
val allSquaresInTheFuture: Future[List[Int]] =
Future.traverse(numberList)(squareInTheFuture)
Problem
I'm trying to use a for expression to iterate over a list, then do a transformation on each element using a utility that returns a Future. Long story short, it doesn't compile, and I'd like to understand why. I read this question, which is similar, and was a great help, but what I'm trying to do is even simpler, which is all the more confusing as to why it doesn't work. I'm trying to do something like: ``` import scala.concurrent.Future import scala.concurrent.ExecutionContext.Implicits.global val numberList = List(1, 2, 3) def squareInTheFuture(number: Int): Future[Int] = Future { number * number} val allTheSquares = for { number <- numberList square <- squareInTheFuture(number) } yield { square } ``` And what I get is: error: type mismatch; found : scala.concurrent.Future[Int] required: scala.collection.GenTraversableOnce[?] square <- squareInTheFuture(number) ^ Can someone help me understand why this doesn't work and what the best alternative is?