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 582537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:41:50+00:00 2026-05-13T14:41:50+00:00

Is it possible to implement in Scala something equivalent to the Python yield statement

  • 0

Is it possible to implement in Scala something equivalent to the Python yield statement where it remembers the local state of the function where it is used and “yields” the next value each time it is called?

I wanted to have something like this to convert a recursive function into an iterator. Sort of like this:

# this is python
def foo(i):
  yield i
  if i > 0:
    for j in foo(i - 1):
      yield j

for i in foo(5):
  print i

Except, foo may be more complex and recurs through some acyclic object graph.

Additional Edit:
Let me add a more complex example (but still simple):
I can write a simple recursive function printing things as it goes along:

// this is Scala
def printClass(clazz:Class[_], indent:String=""): Unit = {
  clazz match {
    case null =>
    case _ =>
      println(indent + clazz)
      printClass(clazz.getSuperclass, indent + "  ")
      for (c <- clazz.getInterfaces) {
        printClass(c, indent + "  ")
      }
  }
}

Ideally I would like to have a library that allows me to easily change a few statements and have it work as an Iterator:

// this is not Scala
def yieldClass(clazz:Class[_]): Iterator[Class[_]] = {
  clazz match {
    case null =>
    case _ =>
      sudoYield clazz
      for (c <- yieldClass(clazz.getSuperclass)) sudoYield c
      for (c <- clazz.getInterfaces; d <- yieldClasss(c)) sudoYield d
  }
}

It does seem continuations allow to do that, but I just don’t understand the shift/reset concept. Will continuation eventually make it into the main compiler and would it be possible to extract out the complexity in a library?

Edit 2:
check Rich’s answer in that other thread.

  • 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-13T14:41:51+00:00Added an answer on May 13, 2026 at 2:41 pm

    While Python generators are cool, trying to duplicate them really isn’t the best way to go about in Scala. For instance, the following code does the equivalent job to what you want:

    def classStream(clazz: Class[_]): Stream[Class[_]] = clazz match {
      case null => Stream.empty
      case _ => (
        clazz 
        #:: classStream(clazz.getSuperclass) 
        #::: clazz.getInterfaces.toStream.flatMap(classStream) 
        #::: Stream.empty
      )
    }
    

    In it the stream is generated lazily, so it won’t process any of the elements until asked for, which you can verify by running this:

    def classStream(clazz: Class[_]): Stream[Class[_]] = clazz match {
      case null => Stream.empty
      case _ => (
        clazz 
        #:: { println(clazz.toString+": super"); classStream(clazz.getSuperclass) } 
        #::: { println(clazz.toString+": interfaces"); clazz.getInterfaces.toStream.flatMap(classStream) } 
        #::: Stream.empty
      )
    }
    

    The result can be converted into an Iterator simply by calling .iterator on the resulting Stream:

    def classIterator(clazz: Class[_]): Iterator[Class[_]] = classStream(clazz).iterator
    

    The foo definition, using Stream, would be rendered thus:

    scala> def foo(i: Int): Stream[Int] = i #:: (if (i > 0) foo(i - 1) else Stream.empty)
    foo: (i: Int)Stream[Int]
    
    scala> foo(5) foreach println
    5
    4
    3
    2
    1
    0
    

    Another alternative would be concatenating the various iterators, taking care to not pre-compute them. Here’s an example, also with debugging messages to help trace the execution:

    def yieldClass(clazz: Class[_]): Iterator[Class[_]] = clazz match {
      case null => println("empty"); Iterator.empty
      case _ =>
        def thisIterator = { println("self of "+clazz); Iterator(clazz) }
        def superIterator = { println("super of "+clazz); yieldClass(clazz.getSuperclass) }
        def interfacesIterator = { println("interfaces of "+clazz); clazz.getInterfaces.iterator flatMap yieldClass }
        thisIterator ++ superIterator ++ interfacesIterator
    }
    

    This is pretty close to your code. Instead of sudoYield, I have definitions, and then I just concatenate them as I wish.

    So, while this is a non-answer, I just think you are barking up the wrong tree here. Trying to write Python in Scala is bound to be unproductive. Work harder at the Scala idioms that accomplish the same goals.

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

Sidebar

Ask A Question

Stats

  • Questions 297k
  • Answers 297k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There is no efficient way to do that. The idea… May 13, 2026 at 7:15 pm
  • Editorial Team
    Editorial Team added an answer Documented behaviour seems to imply your method should work: http://msdn.microsoft.com/en-us/library/ms146084.aspx… May 13, 2026 at 7:15 pm
  • Editorial Team
    Editorial Team added an answer I've been using PortAudio successfully. I took some excerpts from… May 13, 2026 at 7:15 pm

Related Questions

I've recently started learning scala, and I've come across the :: (cons) function, which
I want to implement bulk availability checking of .co.za domain names as accurately as
DNS Round Robin (DRR) permits to do cheap load balancing (distribution is a better
As far as I know, what I ask here isn't possible, but I thought
Over the years, I think I have seen and tried every conceivable way of

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.