Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6476117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:47:09+00:00 2026-05-25T06:47:09+00:00

I am doing writing code for PhD research and starting to use Scala. I

  • 0

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 implementing complex iterators over large, often irregularly structured text files. Similar constructs exist in other languages (e.g. C#), for good reason.

Yes I know there have been previous threads on this. But they look like hacked-up (or at least badly explained) solutions that don’t clearly work well and often have unclear limitations. I would like to write code something like this:

import generator._

def yield_values(file:String) = {
  generate {
    for (x <- Source.fromFile(file).getLines()) {
      # Scala is already using the 'yield' keyword.
      give("something")
      for (field <- ":".r.split(x)) {
        if (field contains "/") {
          for (subfield <- "/".r.split(field)) { give(subfield) }
        } else {
          // Scala has no 'continue'.  IMO that should be considered
          // a bug in Scala.
          // Preferred: if (field.startsWith("#")) continue
          // Actual: Need to indent all following code
          if (!field.startsWith("#")) {
            val some_calculation = { ... do some more stuff here ... }
            if (some_calculation && field.startsWith("r")) {
              give("r")
              give(field.slice(1))
            } else {
              // Typically there will be a good deal more code here to handle different cases
              give(field)
            }
          }
        }
      }
    }
  }
}

I’d like to see the code that implements generate() and give(). BTW give() should be named yield() but Scala has taken that keyword already.

I gather that, for reasons I don’t understand, Scala continuations may not work inside a for statement. If so, generate() should supply an equivalent function that works as close as possible to a for statement, because iterator code with yield almost inevitably sits inside a for loop.

Please, I would prefer not to get any of the following answers:

  1. ‘yield’ sucks, continuations are better. (Yes, in general you can do more with continuations. But they are hella hard to understand, and 99% of the time an iterator is all you want or need. If Scala provides lots of powerful tools but they’re too hard to use in practice, the language won’t succeed.)
  2. This is a duplicate. (Please see my comments above.)
  3. You should rewrite your code using streams, continuations, recursion, etc. etc. (Please see #1. I will also add, technically you don’t need for loops either. For that matter, technically you can do absolutely everything you ever need using SKI combinators.)
  4. Your function is too long. Break it up into smaller pieces and you won’t need ‘yield’. You’d have to do this in production code, anyway. (First, “you won’t need ‘yield'” is doubtful in any case. Second, this isn’t production code. Third, for text processing like this, very often, breaking the function into smaller pieces — especially when the language forces you to do this because it lacks the useful constructs — only makes the code harder to understand.)
  5. Rewrite your code with a function passed in. (Technically, yes you can do this. But the result is no longer an iterator, and chaining iterators is much nicer than chaining functions. In general, a language should not force me to write in an unnatural style — certainly, the Scala creators believe this in general, since they provide shitloads of syntactic sugar.)
  6. Rewrite your code in this, that, or the other way, or some other cool, awesome way I just thought of.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T06:47:10+00:00Added an answer on May 25, 2026 at 6:47 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm doing some research on multicore processors; specifically I'm looking at writing code for
I'm writing code like this, doing a little quick and dirty timing: var sw
I'm writing a wizard for an Eclipse RCP application. After doing some processing on
I am doing some research on Unicode for a white-paper I am writing. Does
When writing code in Python, how can you write something next to it that
I have been writing some code that detects add and removal of USB devices,
Hey guys, I have a problem with a code that I've been writing. I
I'm writing a utility module for easy handling of data text files. I have
I find myself writing code like the following quite a lot: if (myEntity.Id ==
I'm not to sure if my title is right. What I'm doing is writing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.