What is the preferred way to implement 'yield' in Scala?

前端 未结 3 1287
轻奢々
轻奢々 2021-01-31 19:23

I am doing writing code for PhD research and starting to use Scala. I often have to do text processing. I am used to Python, whose \'yield\' statement is extremely useful for i

3条回答
  •  -上瘾入骨i
    2021-01-31 20:02

    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?

    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.

    Here's how you do it.

    // You want to write
    for (x <- 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 */
    

    That's it! All your cases can be reduced to one of these three.

    In your case, your example would look something like

    Source.fromFile(file).getLines().flatMap(x =>
      Iterator("something") ++
      ":".r.split(x).iterator.flatMap(field =>
        if (field contains "/") "/".r.split(field).iterator
        else {
          if (!field.startsWith("#")) {
            /* vals, whatever */
            if (some_calculation && field.startsWith("r")) Iterator("r",field.slice(1))
            else Iterator(field)
          }
          else Iterator.empty
        }
      )
    )
    

    P.S. Scala does have continue; it's done like so (implemented by throwing stackless (light-weight) exceptions):

    import scala.util.control.Breaks._
    for (blah) { breakable { ... break ... } }
    

    but that won't get you what you want because Scala doesn't have the yield you want.

提交回复
热议问题