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

  • Home
  • SEARCH
  • 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 6227865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:17:34+00:00 2026-05-24T09:17:34+00:00

In Python, I can do something like this: lazy = ((i,j) for i in

  • 0

In Python, I can do something like this:

lazy = ((i,j) for i in range(0,10000) for j in range(0,10000))
sum((1 for i in lazy))

It will take a while, but the memory use is constant.

The same construct in scala:

(for(i<-0 to 10000; j<-i+1 to 10000) yield (i,j)).count((a:(Int,Int)) => true)

After a while, I get a java.lang.OutOfMemoryError, even though it should be evaluated lazily.

  • 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-24T09:17:35+00:00Added an answer on May 24, 2026 at 9:17 am

    Nothing’s inherently lazy about Scala’s for-comprehension; it’s syntactic sugar* which won’t change the fact that the combination of your two ranges will be eager.

    If you work with lazy views of your ranges, the result of the comprehension will be lazy too:

    scala> for(i<-(0 to 10000).view; j<-(i+1 to 10000).view) yield (i,j)
    res0: scala.collection.SeqView[(Int, Int),Seq[_]] = SeqViewN(...)
    
    scala> res0.count((a: (Int, Int)) => true)
    res1: Int = 50005000
    

    The laziness here is nothing to do with the for-comprehension, but because when flatMap or map (see below) are called on some type of container, you get back a result in the same type of container. So, the for-comprehension will just preserve the laziness (or lack of) of whatever you put in.


    *for something like:

    (0 to 10000).flatMap(i => (i+1 to 10000).map(j => (i, j)))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can I do something like this in Python? for (i = 0; i <
In python you can do something like this to import a module using a
In Python, you can something like this: >>> list(map(str.upper, ['foo','bar'])) ['FOO', 'BAR'] I would
In python you can do something like this: arr = list(set(randint(-50, 50) for _
In python, I can do something like this: List=[3, 4] def Add(x, y): return
In PHP, how can I achieve something like this from Python? class CrowdProcess(): def
I love how in python I can do something like: points = [] for
I can do something like this in Haskell: #!/usr/bin/runghc main=putStrLn Hello World Then I
Python optparse works very good when script usage is something like this %prog [options]
In PHP, I can do something like this: class MyClass { function __call($name, $args)

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.