title: How to yield a single element from for loop in scala?
body: <p>Much like this question:</p>
  
  <p><a href="https://stackoverflow.com/questions/6729785/functional-code-for-looping-with-early-exit">Functional code for looping with early exit</a></p>
  
  <p>Say the code is</p>
  
  <pre><code>def findFirst[T](objects: List[T]):T = {
    for (obj &lt;- objects) {
      if (expensiveFunc(obj) != null) return /*???*/ Some(obj)
    }
    None
  }
  </code></pre>
  
  <p>How to yield a single element from a for loop like this in scala?</p>
  
  <p>I do not want to use find, as proposed in the original question, i am curious about if and how it could be implemented using the for loop.</p>
  
  <p><strong>* UPDATE *</strong></p>
  
  <p>First, thanks for all the comments, but i guess i was not clear in the question. I am shooting for something like this:</p>
  
  <pre><code>val seven = for {
      x &lt;- 1 to 10
      if x == 7
  } return x
  </code></pre>
  
  <p>And that does not compile. The two errors are:
  - return outside method definition
  - method main has return statement; needs result type</p>
  
  <p>I know find() would be better in this case, i am just learning and exploring the language. And in a more complex case with several iterators, i think finding with for can actually be usefull.</p>
  
  <p>Thanks commenters, i'll start a bounty to make up for the bad posing of the question :)</p>