3 回答
TA贡献1934条经验 获得超2个赞
map:
def map[B, That](f : (A) => B)(implicit bf : CanBuildFrom[Repr, B, That]) : That
breakOut
breakOut(Int, String)MapList[(Int, String)]
mapBuilderListMapCanBuildFrommapbreakOut
breakOut:
def breakOut[From, T, To](implicit b : CanBuildFrom[Nothing, T, To]) =
new CanBuildFrom[From, T, To] {
def apply(from: From) = b.apply() ; def apply() = b.apply()
}breakOutCanBuildFromFrom, TTomapCanBuildFrom[List[String], (Int, String), Map[Int, String]]
From = List[String]T = (Int, String)To = Map[Int, String]
breakOutCanBuildFrom[Nothing,T,To]CanBuildFrom[Nothing,(Int,String),Map[Int,String]]
CanBuildFrom
trait CanBuildFrom[-From, -Elem, +To] extends AnyRef
CanBuildFromNothingNothing.
关于建筑商
map
scala.collection.mutable.BuilderListListMapMapmap
mapMap('a' -> 1).map(_.swap)Map(1 -> 'a')Map('a' -> 1).map(_._1)MapIterable).
BuilderCanBuildFrom
CanBuildFrom
MapListList
Map(1 -> "one", 2 -> "two") map Function.tupled(_ -> _.length)Map(1 -> "one", 2 -> "two") map (_._2)
MapIterableCanBuildFrommap
mapTraversableLikeBThatARepr
TraversableLike
trait TraversableLike[+A, +Repr] extends HasNewBuilder[A, Repr] with AnyRefdef map[B, That](f : (A) => B)(implicit bf : CanBuildFrom [Repr, B, That]) : That
AReprMap
trait Map[A, +B] extends Iterable[(A, B)] with Map[A, B] with MapLike[A, B, Map[A, B]]
TraversableLikeMap, AReprMapTraversableLike
trait Map[A, +B] extends Iterable[(A, B)] with Map[A, B] with MapLike[A, B, Map[A, B]]trait MapLike[A, +B, +This <: MapLike[A, B, This] with Map[A, B]] extends MapLike[A, B, This]trait MapLike[A, +B, +This <: MapLike[A, B, This] with Map[A, B]] extends PartialFunction[A, B] w ith IterableLike[(A, B), This] with Subtractable[A, This]trait IterableLike[+A, +Repr] extends Equals with TraversableLike[A, Repr]trait T raversableLike[+A, +Repr] extends HasNewBuilder[A, Repr] with AnyRef
Map[Int, String]TraversableLikemap
A = (Int,String)Repr = Map[Int, String]
((Int, String)) => (Int, Int)((Int, String)) => StringA
map Function.tupled(_ -> _.length):B = (Int, Int)map (_._2):B = String
mapMap[Int,Int]Iterable[String]mapThat
Map:
implicit def canBuildFrom [A, B] : CanBuildFrom[Map, (A, B), Map[A, B]]
IterableMap:
implicit def canBuildFrom [A] : CanBuildFrom[Iterable, A, Iterable[A]]
CanBuildFrom.
CanBuildFromCanBuildFrom.
回到问题上
Listmap
val map : Map[Int,String] = List("London", "Paris").map(x => (x.length, x))(breakOut)sealed abstract class List[+A] extends LinearSeq[A]
with Product with GenericTraversableTemplate[A, List] with LinearSeqLike[A, List[A]]trait LinearSeqLike[+A, +Repr <: LinearSeqLike[A, Repr]]
extends SeqLike[A, Repr]trait SeqLike[+A, +Repr] extends IterableLike[A, Repr]trait IterableLike[+A, +Repr] extends Equals with TraversableL
ike[A, Repr]trait TraversableLike[+A, +Repr] extends HasNewBuilder[A, Repr] with AnyRefdef map[B, That](f : (A) => B)(implicit bf :
CanBuildFrom[Repr, B, That]) : ThatList("London", "Paris")List[String]AReprTraversableLike
A = StringRepr = List[String]
(x => (x.length, x))(String) => (Int, String)B
B = (Int, String)
Thatmap
val map : Map[Int,String] =
That = Map[Int, String]
breakOutCanBuildFrom[List[String], (Int, String), Map[Int, String]].
TA贡献1911条经验 获得超7个赞
scala> import scala.collection.generic._import scala.collection.generic._
scala> import scala.collection._import scala.collection._
scala> import scala.collection.mutable._import scala.collection.mutable._
scala>scala> def breakOut[From, T, To](implicit b : CanBuildFrom[Nothing, T, To]) =
| new CanBuildFrom[From, T, To] {
| def apply(from: From) = b.apply() ; def apply() = b.apply()
| }breakOut: [From, T, To]
| (implicit b: scala.collection.generic.CanBuildFrom[Nothing,T,To])
| java.lang.Object with
| scala.collection.generic.CanBuildFrom[From,T,To]scala> val l = List(1, 2, 3)l: List[Int] = List(1, 2, 3)scala>
val imp = l.map(_ + 1)(breakOut)imp: scala.collection.immutable.IndexedSeq[Int] = Vector(2, 3, 4)scala>
val arr: Array[Int] = l.map(_ + 1)(breakOut)imp: Array[Int] = Array(2, 3, 4)scala> val stream: Stream[Int] = l.
map(_ + 1)(breakOut)stream: Stream[Int] = Stream(2, ?)scala> val seq: Seq[Int] = l.map(_ + 1)(breakOut)seq: scala.col
lection.mutable.Seq[Int] = ArrayBuffer(2, 3, 4)scala> val set: Set[Int] = l.map(_ + 1)(breakOut)seq: scala.collection.mutabl
e.Set[Int] = Set(2, 4, 3)scala> val hashSet: HashSet[Int] = l.map(_ + 1)(breakOut)seq: scala.collection.mutable.HashSet[Int]
= Set(2, 4, 3)scala> def buildWith[From, T, To](b : Builder[T, To]) =
| new CanBuildFrom[From, T, To] {
| def apply(from: From) = b ; def apply() = b | }buildWith: [From, T, To]
| (b: scala.collection.mutable.Builder[T,To])
| java.lang.Object with
| scala.collection.generic.CanBuildFrom[From,T,To]scala> val a = l.map(_ + 1)(buildWith(Array.newBuilder[Int]))a:
Array[Int] = Array(2, 3, 4)添加回答
举报
