This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Suggestion for building non-strict collections. Or: Can Stream.map 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 Stream.map and Stream.flatMethod methods to return custom
implemented non-strict collections.

The Stream.map/flatMap 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)
b.result

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,

--Stefan

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