Search code examples
scalafor-comprehensionflatmap

Filter two Future[Seq] with for comprehension scala


I have some functions which returns list of tuple elements(Int, Int) with future. For simplicity I will define two futures

  val f1 = Future {
    List((8, 3), (2, 1), (4, 2), (3, 4))
  }
  val f2 = Future {
    List((2, 3), (5, 1), (7, 9))
  }

I want to filter and get the elements form these two future lists with below criteria.

Tuples which contains same element in second position

In this scenario output should be

List(((2,1),(5,1)), ((8,3),(2,3))) 

I can do this with normal lists(without futures) with for comprehension like below

val l1 = List((4, 2), (3, 4), (2, 1), (8, 3))
val l2 = List((2, 3), (5, 1), (7, 9))

val o = for {
  a <- l1
  b <- l2 if a._2 == b._2
} yield (a, b)

How to do it with futures?


Solution

  • Additional solution if you want to use full for comprehension:

    val o2 = for {
      l1 <- f1
      l2 <- f2
    } yield for {
      a <- l1
      b <- l2 if a._2 == b._2
    } yield (a,b)
    

    Of course you will have Future here, so you need to await for result or pass it further to something which will process it