title: Scala: How to deal with sequences in a Future for comprehension
body: <p>Given the following methods that return a <code>Future</code>...</p>
  
  <pre><code>def getProducts: Future[List[Product]] = { ... }
  def sendOrder(p: Product, n: Int): Future[Order] = { ... }
  </code></pre>
  
  <p>... I need to invoke <code>sendOrder</code> for each product returned by <code>getProducts</code> and in the end <code>yield</code> the number of products processed:</p>
  
  <pre><code>for {
    products &lt;- getProducts
    // how do I iterate thru products and invoke sendOrder for each element?
    ...
  } yield products.length
  </code></pre>
  
  <p>How do I deal with the list of products in the for comprehension above?</p>
  
  <p><strong><em>EDIT</em></strong></p>
  
  <p>Things get even more complex since I need to invoke a third method before <code>sendOrder</code> if and only if <code>getProducts</code> actually returns a nonEmpty list:</p>
  
  <pre><code>def doSomethingBeforeSendingOrder: Future[String] = { ... }
  
  for {
    p &lt;- getProducts
    // how do I invoke doSomethingBeforeSendingOrder before sendOrder and
    // only if getProducts actually returns a nonEmptylist?
    o &lt;- Future.sequence(p.map(senderOrder(_,IDontKnowWhatThisIntMeans)))
  } yield o.length
  </code></pre>