For a complete answer, you should look at my rather long question and answer about the builders (note: as of September 2012, the alternative version of Miles does not work either in version 2.9 or in the latest version 2.10).
Here you can get started (pay attention to the strange formatting of explicit and implicit parameter blocks to avoid excessively long lines for displaying on SO):
import collection.generic.CanBuildFrom case class UsefulData(data: Int) {} def lookerUpper[C[String]]( ids: C[String], someOtherInfo: Int )( implicit cbf: CanBuildFrom[C[String],UsefulData,C[UsefulData]], ev: C[String] => Iterable[String] ): C[UsefulData] = { val b = cbf() val i = ev(ids) i.foreach{ s => b += UsefulData(s.length + someOtherInfo) } b.result }
And note that it works:
scala> lookerUpper(Vector("salmon","cod"),2) res0: scala.collection.immutable.Vector[UsefulData] = Vector(UsefulData(8), UsefulData(5)) scala> lookerUpper(Array("salmon","cod"),2) res1: Array[UsefulData] = Array(UsefulData(8), UsefulData(5))
Edit: if you only care about subclasses of TraversableLike (which Array not), and you are going to use standard collection operations to do all the work, then you can use answer 8609398 , as Luigi points out. (Perhaps I should move this answer there as a different perspective.)
source share