2

Let there is a function foo(): Int. For example, each call foo() returns different values. Just to illustrate:

var i: Int = 0 def foo(): Int = { i += 1 i } 

I would like to iterate through these values. Actually, I would like to construct an Iterator it so that each call it.next() computed by foo() called repeatedly. How it can be done?

2 Answers 2

4

As I always say, the Scaladoc is your friend.

Iterator.continually(foo()) 
Sign up to request clarification or add additional context in comments.

Comments

3

Luis's answer is better, but here's another way to do it:

new Iterator[Int] { override def hasNext: Boolean = true override def next: Int = foo() } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.