As I understand, method filterKeys
of MapLike
creates a wrapper over the original map. So, if I execute the code below m
will be a chain of 10K wrappers and the original map.
var m = Map(1 -> "one", 2 -> "two") for(1 <- 0 until 10000) {m = m.filterKeys(_%2 == 0)}
Now I thought that an invocation of m.contains
caused a stack overflow but it does not happen. Could you explain what is going on in this case?
Your loop only executes 1 time, if I copy verbatim. Because of this you are only creating a single wrapper, so what was intended to be a chain of 10000 wrappers is just a chain of 1. It might be a typo, but the loop,
for(1 <- 0 until 10000) {m = m.filterKeys(_%2 == 0)}
should probably be
for(i <- 0 until 10000) {m = m.filterKeys(_%2 == 0)}
Aside from that, Daniel is right; fitlerKeys does produce what is essentially a wrapper. It took me more than 10k iterations, but I did manage to create a StackOverflowError.