How to properly use Stream's iterate and takeWhile constructs?

SkyWalker

In Scala 2.12.x I have the following example but it doesn't produce the expected result:

val result = Stream.iterate(0)(_ + 10).takeWhile(_ < 100)
println(result)

// outputs:
// Stream(0, ?)

// while I expected:
// Stream(0, 10, 20, 30, 40, 50, 60, 70, 80, 90, ?)

What am I doing wrong here?

Stu Redman

Your stream won't be materialized until you convert it to non-lazy colection:

Stream.iterate(0)(_ + 10).takeWhile(_ < 100).toList

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can this Stream iterate-takeWhile code be rewritten without side effects?

Unclear how to use takewhile python

How to use rxjs buffer with takeWhile

How to iterate structures properly

How to properly use the range() function to print/iterate a grid

Java 8 lambda: iterate over stream objects and use previous/next object(s) in stream

How to implement a parallel supporting takeWhile for the Stream API in Java 8?

How to call both takeWhile and dropWhile on the same Java Stream

How to properly use Haskell's `parseTimeM` function?

How to properly use Django's extra(select={})?

How to properly use Ninject's NamedScope extension?

Javascript: How to iterate array properly

How to iterate JSON object properly?

How to properly vectorize instead of iterate?

Idiomatic haskell using takewhile and iterate

How to use node's transform stream in rxjs?

Unable to iterate over stream inside use{} block

What's the use of Pull Request? And how to use Bitbucket properly?

How to properly use apoc.periodic.iterate to reduce heap usage for large transactions?

How do I use Rust's std::iter::iterate method

How to use higher-order constructs in a particular case

How can I use conditional constructs in a bash case statement?

Stream takeWhile for a sorted stream in the same pipeline

How to iterate through list in a function properly

How should I iterate a priority queue properly?

How to properly iterate over intervals in Python?

How to properly iterate on array of objects in Vue/Nuxt

How to properly iterate over container while modifying it?

How to properly iterate over Array[String]?