5

从 Scala 集合的设计中,我了解到类似:

scala> BitSet(1,2,3) map (_ + "a")
res7: scala.collection.immutable.Set[String] = Set(1a, 2a, 3a)

不构建中间数据结构:新 Set 是在使用 Builder 迭代 BitSet 时构建的。事实上,在这种情况下,这是显而易见的,因为一组字符串没有意义。

列表中的地图呢?我很确定以下内容会构建一个中间列表:

scala> List(1,2,3) map (_ -> "foo") toMap
res8: scala.collection.immutable.Map[Int,java.lang.String] =
    Map(1 -> foo, 2 -> foo, 3 -> foo)

即列表List((1,foo), (2,foo), (3,foo))。如果没有,那怎么办?现在,下面的呢?

scala> Map.empty ++ (List(1,2,3) map (_ -> "foo"))
res10: scala.collection.immutable.Map[Int,java.lang.String] =
    Map(1 -> foo, 2 -> foo, 3 -> foo)

这一次,从我似乎理解的类型来看++

def ++ [B >: (A, B), That]
       (that: TraversableOnce[B])
       (implicit bf: CanBuildFrom[Map[A, B], B, That]): That

认为可能是地图是动态构建的,并且没有构建中间列表。

是这样吗?如果是,这是确保森林砍伐的规范方法还是有更直接的语法?

4

2 回答 2

14

您可以使用breakOut来确保不创建任何中间集合。例如:

// creates intermediate list.
scala> List((3, 4), (9, 11)).map(_.swap).toMap 
res542: scala.collection.immutable.Map[Int,Int] = Map(4 -> 3, 11 -> 9)

scala> import collection.breakOut
import collection.breakOut

// doesn't create an intermediate list.
scala> List((3, 4), (9, 11)).map(_.swap)(breakOut) : Map[Int, Int]
res543: Map[Int,Int] = Map(4 -> 3, 11 -> 9)

你可以在这里阅读更多关于它的信息。

更新:

如果您阅读 的定义breakOut,您会注意到它基本上是一种创建CanBuildFrom预期类型的​​对象并将其显式传递给方法的方法。breakOut只是使您免于键入以下样板。

// Observe the error message. This will tell you the type of argument expected.
scala> List((3, 4), (9, 11)).map(_.swap)('dummy)
<console>:16: error: type mismatch;
 found   : Symbol
 required: scala.collection.generic.CanBuildFrom[List[(Int, Int)],(Int, Int),?]
              List((3, 4), (9, 11)).map(_.swap)('dummy)
                                                ^

// Let's try passing the implicit with required type.
// (implicitly[T] simply picks up an implicit object of type T from scope.)
scala> List((3, 4), (9, 11)).map(_.swap)(implicitly[CanBuildFrom[List[(Int, Int)], (Int, Int), Map[Int, Int]]])
// Oops! It seems the implicit with required type doesn't exist.
<console>:16: error: Cannot construct a collection of type Map[Int,Int] with elements of type (Int, Int) based on a coll
ection of type List[(Int, Int)].
              List((3, 4), (9, 11)).map(_.swap)(implicitly[CanBuildFrom[List[(Int, Int)], (Int, Int), Map[Int, Int]]])

// Let's create an object of the required type ...
scala> object Bob extends CanBuildFrom[List[(Int, Int)], (Int, Int), Map[Int, Int]] {
     |   def apply(from: List[(Int, Int)]) = foo.apply
     |   def apply() = foo.apply
     |   private def foo = implicitly[CanBuildFrom[Nothing, (Int, Int), Map[Int, Int]]]
     | }
defined module Bob

// ... and pass it explicitly.
scala> List((3, 4), (9, 11)).map(_.swap)(Bob)
res12: Map[Int,Int] = Map(4 -> 3, 11 -> 9)

// Or let's just have breakOut do all the hard work for us.
scala> List((3, 4), (9, 11)).map(_.swap)(breakOut) : Map[Int, Int]
res13: Map[Int,Int] = Map(4 -> 3, 11 -> 9)
于 2011-11-21T04:53:02.453 回答
3

示例 1) 正确,没有中间列表

2)是的,你得到一个中间列表。

3)再次是的,您从括号中的内容中获得了一个中间列表。没有“魔法”在发生。如果括号中有内容,则首先对其进行评估。

我不确定您在这里所说的“砍伐森林”是什么意思:根据维基百科,这意味着消除树结构。如果您的意思是消除中间列表,您应该使用view。例如,请参见此处:summing a transformation of a list of a numbers in scala

因此,如果没有中间结果,您的示例将是

BitSet(1,2,3).view.map(_ + "a").toSet

(toSet是必需的,否则你有一个IterableView[String,Iterable[_]])

List(1,2,3).view.map(_ -> "foo").toMap

Map.empty ++ (List(1,2,3).view.map(_ -> "foo"))

还有force一种执行转换操作的方法,但这似乎有一个讨厌的习惯,即给你一个更通用的类型(也许有人可以评论一下):

scala> Set(1,2,3).view.map(_ + 1).force
res23: Iterable[Int] = Set(2, 3, 4)
于 2011-11-21T04:13:40.627 回答