Scala method to convert generic Collection

Viewed 305

How can I create a method that takes a generic Collection M[A] and a function from A to B and returns a Collection M[B], using the map method?

Something like: def convert[A, M[X] <: Traversable[X], B](in: M[A], f: A => B): M[B] = in.map(f)

The method above fails to compile with: type mismatch; found : Traversable[B] required: M[B]. Since the static type of Traversable[A].map(f: A => B) is Traversable[B] as pointed by Oleg Pyzhcov

Note: The purpose of this method is not just mapping the collection, this is just a simplification.

1 Answers
Related