The next item from the stream in Scala

Is there any way or way to get each next element from the stream?

For example, if there is a stream similar to

def natural: Stream[Long] = { def naturalHelper: Long => Stream[Long] = { n => n #:: naturalHelper(n+1) } naturalHelper(1) } val s = natural 

I am looking for something like s.next() , returning 2 on the first call, s.next () = 3 on the next call, etc .... without using var .

+4
source share
1 answer

Make Iterator

 val s = natural.iterator s.next() s.next() 
+9
source

Source: https://habr.com/ru/post/1493972/


All Articles