title: Confused with the for-comprehension to flatMap/Map transformation
body: <p>I really don't seem to be understanding Map and FlatMap. What I am failing to understand is how a for-comprehension is a sequence of nested calls to map and flatMap. The following example is from <a href="http://www.manning.com/bjarnason/" rel="noreferrer">Functional Programming in Scala</a></p>
  
  <pre><code>def bothMatch(pat:String,pat2:String,s:String):Option[Boolean] = for {
              f &lt;- mkMatcher(pat)
              g &lt;- mkMatcher(pat2)
   } yield f(s) &amp;&amp; g(s)
  </code></pre>
  
  <p>translates to</p>
  
  <pre><code>def bothMatch(pat:String,pat2:String,s:String):Option[Boolean] = 
           mkMatcher(pat) flatMap (f =&gt; 
           mkMatcher(pat2) map (g =&gt; f(s) &amp;&amp; g(s)))
  </code></pre>
  
  <p>The mkMatcher method is defined as follows:</p>
  
  <pre><code>  def mkMatcher(pat:String):Option[String =&gt; Boolean] = 
               pattern(pat) map (p =&gt; (s:String) =&gt; p.matcher(s).matches)
  </code></pre>
  
  <p>And the pattern method is as follows:</p>
  
  <pre><code>import java.util.regex._
  
  def pattern(s:String):Option[Pattern] = 
    try {
          Some(Pattern.compile(s))
     }catch{
         case e: PatternSyntaxException =&gt; None
     }
  </code></pre>
  
  <p>It will be great if someone could shed some light on the rationale behind using map and flatMap here.</p>