groupBy within akka stream

Hi everyone. I was testing my stream and found the following phenomenon.

  Source((0 until 100).zipWithIndex)
      .groupedWithin(10, 10 millisecond)
      .map { seq =>
        seq.groupBy(x => x._1 % 2)
          .mapValues { v =>
            println(s"doing stuff ${v.map(_._2)}") // <-- this won't print
            v.size
          }
      }
      .toMat(Sink.ignore)(Keep.both)
      .run()

However, if I change mapValues to map. It prints alright. Any idea why this might happen?

Thanks for the puzzle. The answer was that .mapValues creates a view of the original map, meaning that the values will only be computed when accessed for the first time. This is why calling .mapValues directly on a map is deprecated in Scala 2.13 and you should make this explicit by calling .view.mapValues on the map instead (which is not deprecated).

If you want to compute the values immediately, call .toMap on the view created by .mapValues. This forces the creation of a concrete map.

The question has nothing to do with Akka but it was interesting nonetheless. Gave me a better understanding about views.

aha. that makes sense. it inspired me to learn more about views and differentiate these concepts.

thanks a bunch. cheers