Is there a method or way to get each next element from a stream?
For example if there is a stream looking like
def natural: Stream[Long] = {
def naturalHelper: Long => Stream[Long] = {
n => n #:: naturalHelper(n+1)
}
naturalHelper(1)
}
val s = natural
I'm looking for something like s.next()
, returning 2 on the first call, s.next() = 3 on the next call, and so on... without using var
.
Make it an iterator
val s = natural.iterator
s.next()
s.next()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With