This page is no longer maintained — Please continue to the home page at

Suggestion for building non-strict collections. Or: Can and Stream.flatMap return a custom non-strict collection?

No replies
Stefan Wachter
Joined: 2009-09-18,
User offline. Last seen 42 years 45 weeks ago.

Hi all,

I studied the new collection classes and wondered if it is possible to
have the and Stream.flatMethod methods to return custom
implemented non-strict collections.

The method does not use the implicit BuilderFactory.
If something other than a Stream is required as the result then the
TraversableLike.flatMap method is used. This method uses the implicit
BuilderFactory but processes the whole collection. Yet, this is suitable
for strict collections only!

Could the concept of the BuilderFactory be extended to usable for
non-strict collections too? Would it be possible to have the
BuilderFactory not only to create a builder but to build the result
collection completely. Then the builder factory would have a method:

buildCollection[t: Traversable, f: A => Traversable[B]

The default implementation could be taken from TraversableLike:

val b = bf(repr)
for (x <- t) b ++= f(x)

Now BuilderFactories of non-strict collections would have a chance to
return their non-strict collections. Now the former "BuilderFactory"
should better be named "CollectionBuilder".

Thanks for your attention,


Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland