Scala how to get the last calculated stream value?

According to scala, the docs stream implements lazy lists where items are evaluated only when they are needed. Example

val fibs: Stream[BigInt] = BigInt(0) #:: BigInt(1) #:: fibs.zip(fibs.tail).map(n => { 
    n._1 + n._2
})  

After that in scala repl;

fibs(4)
fibs

Will be printed;

res1: stream [BigInt] = stream (0, 1, 1, 2, 3 ,?)

Since calling .length or .last causes an infinite loop, how can I get the value "3" (the last calculated value) in the most efficient way?

+4
source share
1 answer

. API Stream. , , Stream () Stream.

+3

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


All Articles