Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The premise of your question seems to be that you want exactly Python's yield, and you don't want any other reasonable suggestions to do the same thing in a different way in Scala. If this is true, and it is that important to you, why not use Python? It's quite a nice language. Unless your Ph.D. is in computer science and using Scala is an important part of your dissertation, if you're already familiar with Python and really like some of its features and design choices, why not use it instead?</p> <p>Anyway, if you actually want to learn how to solve your problem in Scala, it turns out that for the code you have, delimited continuations are overkill. All you need are flatMapped iterators.</p> <p>Here's how you do it.</p> <pre><code>// You want to write for (x &lt;- xs) { /* complex yield in here */ } // Instead you write xs.iterator.flatMap { /* Produce iterators in here */ } // You want to write yield(a) yield(b) // Instead you write Iterator(a,b) // You want to write yield(a) /* complex set of yields in here */ // Instead you write Iterator(a) ++ /* produce complex iterator here */ </code></pre> <p>That's it! All your cases can be reduced to one of these three.</p> <p>In your case, your example would look something like</p> <pre><code>Source.fromFile(file).getLines().flatMap(x =&gt; Iterator("something") ++ ":".r.split(x).iterator.flatMap(field =&gt; if (field contains "/") "/".r.split(field).iterator else { if (!field.startsWith("#")) { /* vals, whatever */ if (some_calculation &amp;&amp; field.startsWith("r")) Iterator("r",field.slice(1)) else Iterator(field) } else Iterator.empty } ) ) </code></pre> <p>P.S. Scala <em>does</em> have continue; it's done like so (implemented by throwing stackless (light-weight) exceptions):</p> <pre><code>import scala.util.control.Breaks._ for (blah) { breakable { ... break ... } } </code></pre> <p>but that won't get you what you want because Scala doesn't have the yield you want.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload