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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:13:37+00:00 2026-06-12T00:13:37+00:00

Why is it that, scala> List(1,2,3,4).iterator.map((x: Int) => println(x)) does not print out 1

  • 0

Why is it that,

scala> List(1,2,3,4).iterator.map((x: Int) => println(x))

does not print out

1
2
3
4

while

List(1,2,3,4).map((x: Int) => println(x))
List(1,2,3,4).foreach((x: Int) => println(x))
List(1,2,3,4).iterator.foreach((x: Int) => println(x))

all do?

In other words, why is it that a map on a iterator that maps type T to Unit and has side effects unable to show those side effects?

Edit:

Also why does the following invocation of lazyMap actually computes the new iterator (provide the complete new iterator) from beginning to end if iterator is lazy?

def lazyMap[T, U](coll: Iterable[T], f: T => U) = new Iterable[U] {
  def iterator = coll.iterator map f
}

scala> lazyMap(List(1,2,3,4), (x: Int) => x + 1)
res4: java.lang.Object with Iterable[Int] = (2, 3, 4, 5)
  • 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-12T00:13:38+00:00Added an answer on June 12, 2026 at 12:13 am

    Cause map on iterator is lazy and you need some strictness:

    scala> List(1,2,3,4).iterator.map((x: Int) => println(x))
    res0: Iterator[Unit] = non-empty iterator
    
    // nothing actually happened yet, just remember to do this printing things
    
    scala> res0.toList
    1
    2
    3
    4
    res1: List[Unit] = List((), (), (), ())
    

    When you doing foreach on iterator it is quite obvious that you’re doing side effects, so lazyness will be undesired. I wouldn’t said so about map.

    UPD

    As for your edit: the reason for such behaviour, is that there is implicit call of toString for statement result which in turn stricts the iterator — try this code on your own:

    scala> { lazyMap(List(1,2,3,4), {(x: Int) => println(x); x + 1}); 1 }
    

    and you’ll see that function f is never called

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

Sidebar

Related Questions

It's a sad fact of life on Scala that if you instantiate a List[Int],
Suppose I want to add functionality like map to a Scala List , something
I have a list in scala called l : List[AType] that I want to
In Scala, I can create a method that takes more than one argument list:
I read in Programming in Scala section 23.5 that map, flatMap and filter operations
I'm kinda new to Scala trying it out while reading Beggining Scala by David
I have a short list of questions that I've found today. I'm a scala
I found out reading the spec that scala supports binding type variables when doing
Why is the scala list implementation named :: and not a class name? Is
From the design of Scala's collections I understand that something like: scala> BitSet(1,2,3) map

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.