title: How to flatten list of options using higher order functions?
body: <p>Using Scala 2.7.7:</p>
  
  <p>If I have a list of Options, I can flatten them using a for-comprehension:</p>
  
  <pre><code>val listOfOptions = List(None, Some("hi"), None)
  listOfOptions: List[Option[java.lang.String]] = List(None, Some(hi), None)
  
  scala&gt; for (opt &lt;- listOfOptions; string &lt;- opt) yield string
  res0: List[java.lang.String] = List(hi)
  </code></pre>
  
  <p>I don't like this style, and would rather use a HOF. This attempt is too verbose to be acceptable:</p>
  
  <pre><code>scala&gt; listOfOptions.flatMap(opt =&gt; if (opt.isDefined) Some(opt.get) else None)
  res1: List[java.lang.String] = List(hi)
  </code></pre>
  
  <p>Intuitively I would have expected the following to work, but it doesn't:</p>
  
  <pre><code>scala&gt; List.flatten(listOfOptions)
  &lt;console&gt;:6: error: type mismatch;
   found   : List[Option[java.lang.String]]
   required: List[List[?]]
         List.flatten(listOfOptions)
  </code></pre>
  
  <p>Even the following seems like it should work, but doesn't:</p>
  
  <pre><code>scala&gt; listOfOptions.flatMap(_: Option[String])
  &lt;console&gt;:6: error: type mismatch;
   found   : Option[String]
   required: (Option[java.lang.String]) =&gt; Iterable[?]
         listOfOptions.flatMap(_: Option[String])
                            ^
  </code></pre>
  
  <p>The best I can come up with is:</p>
  
  <pre><code>scala&gt; listOfOptions.flatMap(_.toList)         
  res2: List[java.lang.String] = List(hi)
  </code></pre>
  
  <p>... but I would much rather not have to convert the option to a list. That seems clunky.</p>
  
  <p>Any advice?</p>