Monday, September 9, 2013

Idiomatic Scala to build collection B from collection A

Idiomatic Scala to build collection B from collection A

I have one collection A and I need to create a collection B from
collection A. In the simplest case, one could hope to use the mapmethod,
but I am looking for a solution where at each step in the creation of
elements of B, it is needed to have access to the elements already in the
collection.
I have in mind multiple possibilities to create B:
Using a mutable collection for B and then turn it into an immutable
Using the builder underlying the collection B
Using a foldLeft with an empty B and grow it inside the closure
Is there an idiomatic approach, or at least a way to pick up one according
to different use cases?

No comments:

Post a Comment