title: scala: how to rewrite this function using for comprehension
body: <p>I have this piece of code with a couple of nasty nested checks...</p>
  
  <p>I'm pretty sure it can be rewritten with a nice for comprehension, but I'm a bit confused about how to mix the pattern matching stuff</p>
  
  <pre><code>// first tries to find the token in a header: "authorization: ideas_token=xxxxx"
  // then tries to find the token in the querystring: "ideas_token=xxxxx"
  private def applicationTokenFromRequest(request: Request[AnyContent]): Option[String] = {
  
    val fromHeaders: Option[String] = request.headers.get("authorization")
    val tokenRegExp = """^\s*ideas_token\s*=\s*(\w+)\s*$""".r
  
    val tokenFromHeader: Option[String] = {
      if (fromHeaders.isDefined) {
        val header = fromHeaders.get
        if (tokenRegExp.pattern.matcher(header).matches) {
          val tokenRegExp(extracted) = header
          Some(extracted)
        } else {
          None
        }
      } else {
        None
      }
    }
  
    // try to find it in the queryString
    tokenFromHeader.orElse {
      request.queryString.get("ideas_token")
    }
  
  }
  </code></pre>
  
  <p>any hint you can give me?</p>