Slide 53
Slide 53 text
Consider a large Vector, v1:
@ val v1 = Vector(1, 2, 0, 9, 7, 2, 9, 6, …, 3, 2, 5, 5, 4, 8, 4, 6)
This is represented in-memory as a tree structure, whose breadth and depth depend on the size of the Vector:
This example is somewhat simplified – a Vector in Scala has 32 elements per tree node rather than the 4 shown above – but it
will serve us well enough to illustrate how the Vector data structure works.
Let us consider what happens if we want to perform an update, e.g. replacing the fifth value 7 in the above Vector with the
value 8:
@ val v2 = v1.updated(4, 8)
@ v2
res50: Vector[Int] = Vector(1, 2, 0, 9, 8, 2, 9, 6, …, 3, 2, 5, 5, 4, 8, 4, 6)
Li Haoyi
@lihaoyi
1 2 0 9 3 2 5 5 4 8 4 6
7 2 9 6 … … … … … … … … … … … …
v1