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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:17:57+00:00 2026-05-31T19:17:57+00:00

I have a deeply nested for-comprehension, simplified to 3 levels below: x, y, and

  • 0

I have a deeply “nested” for-comprehension, simplified to 3 levels below: x, y, and z. I was hoping making only x a Stream would make the y and z computations lazy too:

val stream = for {
  x <- List(1, 2, 3).toStream
  y <- List("foo", "bar", "baz")
  z = {
    println("Processed " + x + y)
    x + y
  }
} yield z

stream take (2) foreach (doSomething)

But this computes all 3 elements, as evidenced by the 3 prints. I’d like to only compute the first 2, since those are all I take from the stream. I can work around this by calling toStream on the second List and so on. Is there a better way than calling that at every level of the for-comprehension?

  • 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-31T19:17:58+00:00Added an answer on May 31, 2026 at 7:17 pm

    What it prints is:

    Processed 1foo
    Processed 1bar
    Processed 1baz
    stream: scala.collection.immutable.Stream[String] = Stream(1foo, ?)
    
    scala> stream take (2) foreach (println)
    1foo
    1bar
    

    The head of a Stream is always strictly evaluated, which is why you see Processed 1foo etc and not Processed 2foo etc. This is printed when you create the Stream, or more precisely, when the head of stream is evaluated.

    You are correct that if you only wish to process each resulting element one by one then all the generators will have to be Streams. You could get around calling toStream by making them Streams to start with as in example below.

    stream is a Stream[String] and its head needs to be evaluated. If you don’t want to calculate a value eagerly, you could either prepend a dummy value, or better, make your value stream lazy:

    lazy val stream = for {
      x <- Stream(1, 2, 3)
      y <- Stream("foo", "bar", "baz")
      z = { println("Processed " + x + y); x + y }
    } yield z 
    

    This does not do any “processing” until you take each value:

    scala> stream take 2 foreach println
    Processed 1foo
    1foo
    Processed 1bar
    1bar
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a page with deeply-nested HTML elements generated from a framework. I would
I have to manipulate a deeply nested hash (5 or six levels) in Ruby.
Thanks in advance for your time. I have a deeply nested XML whose XSLT
I have some deeply randomly nested dictionary as follows. {'CompilationStatistics': {'CodeGeneration': {'EndTime': '2010-04-21T14:03:11', 'StartTime':
Suppose we have to use in some function deeply nested pointer very extensively: function
I have this deeply nested list (list of lists) and I want to replace
I have a deeply nested partial view, where I iterate over a collection of
I have a several nested methods in my program. If a deeply nested method
I have 3 Deeply nested attraibutes: Process has many categories , Categories has many
I have a particular element (input box) that's deeply nested in some table stuff

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.