scala - How to choose the output collection type in Seq.map? -
i'd implement like:
def f(s: seq[int]): vector[string] = s.map(_.tostring).tovector
but i'd create directly output vector without executing map
first, making whatever seq, before copying vector.
seq.map
takes implicit canbuilfrom
parameters induces collection output type. tried s.map(...)(vector.canbuildfrom[string])
gives error:
found : scala.collection.generic.canbuildfrom[vector.coll,string,scala.collection.immutable.vector[string]] (which expands to) scala.collection.generic.canbuildfrom[scala.collection.immutable.vector[_],string,scala.collection.immutable.vector[string]] required: scala.collection.generic.canbuildfrom[seq[int],string,vector[string]] def f(s: seq[int]): vector[string] = s.map(_.tostring)(vector.canbuildfrom[string])
basically doesn't infer correctly first type argument of canbuildfrom
how can done ?
breakout
you're looking for
def f(s: seq[int]): vector[string] = s.map(_.tostring)(collection.breakout)
for in-depth discussion of breakout
does, check out stackoverflow question: scala 2.8 breakout
Comments
Post a Comment