Why does scala `GenTraversableOnce` not declare a` map`?

GenTraversableOncedoes not declare mapas an (abstract) element, but also its direct children GenTraversableand TraversableOncedo (although the latter does this using an implicit conversion). Why?

If I need a collection that implements map, what type should I declare?

This question is completely different, as it asks for a way to implement a functor in Scala, while I ask a question about the details of the Scala collection structure.

EDIT This has nothing to do with how many times you can go through GenTraversableOnce, because it TraversableOncemakes the same promises regarding the number of possible workarounds (that is, both promise at least one workaround).

+4
source share
1 answer

I don’t remember this, but here is my intuition about it (I have no links / quotes, so take this with salt).

As the name implies for GenTraversableOnce, this feature guarantees only one iteration over the contents of the passing.

mapSeveral operations imply the potential of chain transformations (for example, foo.map(f).map(g)), which means multiple workarounds.

+1
source

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


All Articles