title: println in scala for-comprehension
body: <p>In a for-comprehension, I can't just put a print statement:</p>
  <pre><code>def prod (m: Int) = {
    for (a &lt;- 2 to m/(2*3);
      print (a + &quot;  &quot;);
      b &lt;- (a+1) to m/a;
      c = (a*b) 
      if (c &lt; m)) yield c
  }
  </code></pre>
  <p>but I can circumvent it easily with a dummy assignment:</p>
  <pre><code>def prod (m: Int) = {
    for (a &lt;- 2 to m/(2*3);
      dummy = print (a + &quot;  &quot;);
      b &lt;- (a+1) to m/a;
      c = (a*b) 
      if (c &lt; m)) yield c
  }
  </code></pre>
  <p>Being a side effect, and only used (so far) in code under development, is there a better ad hoc solution?</p>
  <p>Is there a serious problem why I shouldn't use it, beside being a side effect?</p>
  <h3>update showing the real code, where adapting one solution is harder than expected:</h3>
  <p>From the discussion with Rex Kerr, the necessity has risen to show the original code, which is a bit more complicated, but did not seem to be relevant for the question (2x .filter, calling a method in the end), but when I tried to apply Rex' pattern to it I failed, so I post it here:</p>
  <pre><code>  def prod (p: Array[Boolean], max: Int) = {
      for (a &lt;- (2 to max/(2*3)).
          filter (p);
        dummy = print (a + &quot;  &quot;);
        b &lt;- (((a+1) to max/a).
           filter (p));
        if (a*b &lt;= max)) 
          yield (em (a, b, max)) }
  </code></pre>
  <p>Here is my attempt -- (b * a).filter is wrong, because the result is an int, not a filterable collection of ints:</p>
  <pre><code>  // wrong: 
    def prod (p: Array[Boolean], max: Int) = {
      (2 to max/(2*3)).filter (p).flatMap { a =&gt;
        print (a + &quot; &quot;)
        ((a+1) to max/a).filter (p). map { b =&gt; 
          (b * a).filter (_ &lt;= max).map (em (a, b, max))
        }
      }
    }
  </code></pre>
  <p>Part II belongs to the comments, but can't be read, if written there - maybe I delete it in the end. Please excuse.</p>
  <p>Ok - here is Rex last answer in code layout:</p>
  <pre><code>  def prod (p: Array[Boolean], max: Int) = {
      (2 to max/(2*3)).filter (p).flatMap { a =&gt;
        print (a + &quot; &quot;)
        ((a+1) to max/a).filter (b =&gt; p (b) 
          &amp;&amp; b * a &lt; max).map { b =&gt; (m (a, b, max))
        }
      }
    }
   
  </code></pre>