Reputation: 5681
I am trying to apply a partial function to a generic collection while returning a specific collection instead of Traversable of the original type.
Here's what I've tried (among other things):
def filter[A, B, C <: Traversable[A],Y<:B, D <: Traversable[Y]](xs: Option[D] with TraversableLike[A,Option[D]])(pf:PartialFunction[A,B]): D = {
xs.collect(pf)
}
def filter[A, B, C <: Traversable[A], D <: Traversable[B]](xs: D with TraversableLike[A,D])(pf:PartialFunction[A,B])(implicit ev: B =:= Option[Double]): D = {
xs.collect(pf)
}
Any ideas why implicit ev: B =:= Option[Double]
doesn't find the correct types?
I got inspiration from these posts:
Returning original collection type in generic method
How do I apply the enrich-my-library pattern to Scala collections?
Writing my own generic map functioni
Upvotes: 0
Views: 72
Reputation: 22895
You can do this:
import scala.collection.Iterable
import scala.collection.generic.CanBuildFrom
def collect[C[_], A, B](col: C[A])
(pf: PartialFunction[A, B])
(implicit ev: C[A] <:< Iterable[A],
cbf: CanBuildFrom[Nothing, B, C[B]]): C[B] =
col.iterator.collect(pf).to[C]
Upvotes: 1