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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:09:19+00:00 2026-06-17T16:09:19+00:00

I have an infinite Stream and need to pull from it until some element,

  • 0

I have an infinite Stream and need to “pull” from it until some element, naturally.
This is the first step.
But only part of the “pulled” elements will be used in the second step, e.g. only the even
elements.
Is it possible to avoid processing the odd elements by means of lazyness?

The better way to explain what I am asking is showing the code:

  Welcome to Scala version 2.9.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_26).
  Type in expressions to have them evaluated.
  Type :help for more information.

  scala> var n=0; def numbers:Stream[Int] = {n += 1; println("n= " + n); n #:: numbers}
  n: Int = 0
  numbers: Stream[Int]

  scala> numbers.map{z => println("z^2= " + z*z) ; z*z}.take(10)(2)
  n= 1
  z^2= 1
  n= 2
  z^2= 4
  n= 3
  z^2= 9
  res0: Int = 9

  scala> var n=0; def numbers:Stream[Int] = {n += 1; println("n= " + n); n #:: numbers}
  n: Int = 0
  numbers: Stream[Int]

  scala> numbers.map{lazy z => println("z^2= " + z*z) ; z}.take(10)(2)
  <console>:1: error: lazy not allowed here. Only vals can be lazy
  numbers.map{lazy z => println("z^2= " + z*z) ; z*z}.take(10)(2)
  ^

  scala> 

Since the result of take(10)(2) is res0: Int = 9, only the z^2= 9 calculation is really needed.

  • 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-06-17T16:09:20+00:00Added an answer on June 17, 2026 at 4:09 pm

    If you want to defer the calculation of z^2 (the map operation), a view should suffice:

    object Worksheet {
      var n = 0                                   //> n  : Int = 0
      def numbers: Stream[Int] = { n += 1; println("n= " + n); n #:: numbers }
                                                  //> numbers: => Stream[Int]
    
      numbers.view.map { z => println("z^2= " + z * z); z }.take(10)(2)
                                                  //> n= 1
                                                  //| n= 2
                                                  //| n= 3
                                                  //| z^2= 9
                                                  //| res0: Int = 3
    }
    

    If you also want to defer the generation of the numbers in the stream until they are needed, you can do so manually:

    object sheet {
      var n = 0                                   //> n  : Int = 0
      def numbers: Stream[() => Int] = (() => { n += 1; println("n= " + n); n }) #:: numbers
                                                  //> numbers: => Stream[() => Int]
    
      numbers.view.map { z => val x = z(); println("z^2= " + x * x); x }.take(10)(2)
                                                  //> n= 1
                                                  //| z^2= 1
                                                  //| res0: Int = 1
    }
    

    The issue here seems to be that the streams in the Scala library are not lazy in their head. I wouldn’t be surprised if the Scalaz library had something like that already.

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

Sidebar

Related Questions

I have this code: class LFSeq: # lazy infinite sequence with new elements from
I have a PHP script with infinite loop. I need this script running forever.
I have an infinite structure like the following (using the Stream type from the
I'm attempting to create an infinite stream of strings from readLine calls: import java.io.{BufferedReader,
I have been developing web applications for some time but have very recently been
I have infinite gallery based on this example : http://blog.blundell-apps.com/infinite-scrolling-gallery/ , It runs nicely,
I have an infinite stream of data coming out of a logger, which I
I need to find the best way to record an audio stream. I have
I have an infinite Stream of primes primeStream (starting at 2 and increasing). I
I have an iterator (actually a Source.getLines ) that's reading an infinite stream of

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.