" + x.head); x} s.take(5).toList ...">

Scala Flow Confusion

Duration:

lazy val s: Stream[Int] = 1 #:: 2 #:: {val x = s.tail.map(_+1); println("> " + x.head); x}
s.take(5).toList

I would expect:

> List(2, 3)
> List(2, 3, 4)
List(1, 2, 3, 4, 5)

And I get:

> 3
List(1, 2, 3, 4, 5)

Could you explain this to me?

+5
source share
1 answer

The reason you get Intinstead Listis because it sis a stream of integers, so it contains integers, not lists.

The reason you get 3 is because the tail (1,2,3,4,5, ...) (i.e. s) is (2,3,4,5, ...) and if you put +1 over it, you will get (3,4,5,6,7, ...) and the head is 3.

, , , , . , s.tail.map(_+1) ( ).

+5