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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:29:22+00:00 2026-05-17T00:29:22+00:00

I want a convenient way to generate an Iterable , given a initial object

  • 0

I want a convenient way to generate an Iterable, given a initial object and a function to produce the next object from the current one, that consumes O(1) memory (i.e., it doesn’t cache old results; if you want to iterate a second time, the function has to be applied again).

It doesn’t seem like there’s library support for this. In Scala 2.8, the method scala.collection.Iterable.iterate has signature

def iterate [A] (start: A, len: Int)(f: (A) ⇒ A) : Iterable[A]

so it requires that you specify how many iterated function applications you’re interested in ahead of time, and my understanding of the documentation is that Iterable.iterate actually computes all these values immediately. On the other hand, the method scala.collection.Iterator.iterate has signature

def iterate [T] (start: T)(f: (T) ⇒ T) : Iterator[T]

which looks great, but we only get an Iterator which doesn’t offer all the convenience of map, filter and friends.

Is there a convenient library method to produce what I want?

and if not,

Can someone suggest the ‘colloquial’ Scala code for doing this?

To summarise, given an initial object a: A, and a function f: A => A, I’d like a TraversableLike (e.g., probably an Iterable) that generates a, f(a), f(f(a)), ..., and uses O(1) memory, with map, filter etc. functions that also return something that is O(1) in memory.

  • 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-17T00:29:22+00:00Added an answer on May 17, 2026 at 12:29 am

    Iterator.iterate demo with filter:

    object I {
      def main(args:Array[String]) {
        val mb = 1024 * 1024
        val gen = Iterator.iterate(new Array[Int](10 * mb)){arr => 
          val res = new Array[Int](10 * mb)
          arr.copyToArray(res)
          println("allocated 10mb")
          res(0) = arr(0) + 1 // store iteration count in first elem of new array
          res
        }
        // take 1 out of 100
        val gen2 = gen filter (arr => arr(0) % 100 == 0) 
        // print first 10 filtered
        gen2.take(10).foreach { arr => println("filtered " + arr(0)) } 
      }
    }
    

    (this may not work in the REPL as the PRINT step may mess up with memory management)

    JAVA_OPTS="-Xmx128m" scala -cp classes I will show that the filtering works and is lazy. If it wasn’t done in constant memory that would cause a heap error (since it’s allocating something like 900*10mb).

    Use JAVA_OPTS="-Xmx128m -verbose:gc" scala -cp classes I to see the garbage collection events.

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

Sidebar

Ask A Question

Stats

  • Questions 533k
  • Answers 533k
  • 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 The final solution was to go with a friend function… May 17, 2026 at 12:33 am
  • Editorial Team
    Editorial Team added an answer I didn't find a better solution than this but I… May 17, 2026 at 12:33 am
  • Editorial Team
    Editorial Team added an answer In PHP you can increment strings (but you cannot "increase"… May 17, 2026 at 12:33 am

Trending Tags

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

Top Members

Related Questions

So, I've been writing a bunch of ruby gems recently, and one thing I
I have a table that potentially will have high number of inserts per second,
We want to localize our website to several languages. The website is a collection
I'm writing a method that should extract the fully qualified class name out of
Please bear with me as I explain the problem, how I tried to solve
This is a more a philosophical question about Apple's design decisions than a question
In C#, you could do something like this: public IEnumerable<T> GetItems<T>() { for (int
The following program terminates correctly: import System.Random randomList = mapM (\_->getStdRandom (randomR (0, 50000::Int)))
I'm working on an app engine project (java). I'm using the jdo interface. I
I have a project with a huge amount of auto-generated code, which we build

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.