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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:16:47+00:00 2026-05-15T04:16:47+00:00

I am defining a stream in terms of itself (a recursive definition). When trying

  • 0

I am defining a stream in terms of itself (a recursive definition). When trying to access the second element of the stream, StackOverflowError is thrown. The code from scala console:

scala> val s1 = Stream.iterate(1)(identity _)
s1: scala.collection.immutable.Stream[Int] = Stream(1, ?)

scala> lazy val s2 : Stream[Int]= Stream.cons(1, (s2, s1).zipped.map { _ + _ })
s2: Stream[Int] = <lazy>

scala> s1 take 5 print
1, 1, 1, 1, 1, empty
scala> s2(0)
res4: Int = 1

scala> s2(1)
java.lang.StackOverflowError
        at $anonfun$s2$1$$anonfun$apply$1.apply(<console>:9)
        at $anonfun$s2$1$$anonfun$apply$1.apply(<console>:9)
        at scala.Tuple2$Zipped$$anonfun$map$1.apply(Tuple2.scala:62)
        at scala.collection.immutable.Stream.foreach(Stream.scala:255)
        at scala.Tuple2$Zipped.map(Tuple2.scala:60)
        at $anonfun$s2$1.apply(<console>:9)
        at $anonfun$s2$1.apply(<console>:9)
        at scala.collection.immutable.Stream$Cons.tail(Stream.scala:556)
        at scala.collection.immutable.Stream$Cons.tail(Stream.scala:550)
        at scala.collection.immutable.Stream.foreach(Stream.scala:256)
        at scala.Tuple2$Zipped.map(Tuple2.scala:60)
        at $anonfun$s2$1.apply(<console>:9)
        at $anonfun$s2$1.apply(<console>:9)
        at scala.collection.immutable.Stream$Cons.tail(Stream.scala:556)
        at scala.collection.immutable.Str...

I am unable to understand the reason of the stack overflow. Since streams are inherently lazy, recursive mapping should work.

What is wrong with this scenario?

I am using Scala version 2.8.0.RC2.

  • 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-15T04:16:48+00:00Added an answer on May 15, 2026 at 4:16 am

    The problem is that zipped is not lazy–it actually tries to evaluate that map right there. You can do what you want with zip.

    scala> val s1 = Stream.iterate(1)(identity _)
    s1: scala.collection.immutable.Stream[Int] = Stream(1, ?)
    
    scala> lazy val s2: Stream[Int] = Stream.cons(1, (s2 zip s1).map {s=>s._1+s._2})
    s2: Stream[Int] = <lazy>
    
    scala> s2 take 5 print
    1, 2, 3, 4, 5, empty
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When defining a custom operator from the limited set of single-character operators that can
When defining a string in a class using Code First, the default for the
Does defining functions inside a loop affect performance? Like var doSomething = function(element){ $(element).whatever();
; defining stream-for-each (define (stream-for-each proc s) (if (stream-null? s) 'done (begin (proc (stream-car
In defining my Element class I get the following error: no matching function for
When defining a database access object for use in cl-postmodern, what should the :col-type
Defining page arguments is useful because you can call the same callback from different
When defining sequential build steps I use the depends attribute of the target element.
When defining a local inner class, is it safe to access local variables of
Defining custom components in Facelets is easy and quick but there's one thing I

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.