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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:58:36+00:00 2026-05-15T01:58:36+00:00

In Scala, is there a way to get the currently evaluated items in a

  • 0

In Scala, is there a way to get the currently evaluated items in a Stream? For example in the Stream

val s: Stream[Int] = Stream.cons(1, Stream.cons(2, Stream.cons(3, s.map(_+1))))

the method should return only List(1,2,3).

  • 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-15T01:58:37+00:00Added an answer on May 15, 2026 at 1:58 am

    In 2.8, there is a protected method called tailDefined that will return false when you get to the point in the stream that has not yet been evaluated.

    This isn’t too useful (unless you want to write your own Stream class) except that Cons itself makes the method public. I’m not sure why it’s protected in Stream and not in Cons–I would think one or the other might be a bug. But for now, at least, you can write a method like so (writing a functional equivalent is left as an exercise to the reader):

    def streamEvalLen[T](s: Stream[T]) = {
      if (s.isEmpty) 0
      else {
        var i = 1
        var t = s
        while (t match {
          case c: Stream.Cons[_] => c.tailDefined
          case _ => false
        }) {
          i += 1
          t = t.tail
        }
        i
      }
    }
    

    Here you can see it in action:

    scala> val s = Stream.iterate(0)(_+1)
    s: scala.collection.immutable.Stream[Int] = Stream(0, ?)
    
    scala> streamEvalLen(s)
    res0: Int = 1
    
    scala> s.take(3).toList
    res1: List[Int] = List(0, 1, 2)
    
    scala> s
    res2: scala.collection.immutable.Stream[Int] = Stream(0, 1, 2, ?)
    
    scala> streamEvalLen(s)
    res3: Int = 3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

is there a way in Scala to sort an array of tuples using and
Is there a standard way of invoking the scala REPL from a gradle task?
Is there a good scala-esque (I guess I mean functional) way of recursively listing
Hey guys I was wondering if there is a way to get a certain
I'm currently developing a Scala application for Android. I probably could get by, with
Is there a way in java or scala, that allows to override methods of
I'm learning Scala and there is a thing that I can't find out about
As the title says, is there any Scala library that exports functions to convert,
Are there any tools for performing static analysis of Scala code, similar to FindBugs
I wonder if there are any game engine written in Scala or easily accesible

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.