【发布时间】:2017-03-23 21:05:02
【问题描述】:
似乎应用map 和filter 以某种方式将view 转换为Seq。 documentation 包含此示例:
> (v.view map (_ + 1) map (_ * 2)).force
res12: Seq[Int] = Vector(4, 6, 8, 10, 12, 14, 16, 18, 20, 22)
但是如果我做类似的事情,我会得到一个错误:
> val a = Array(1,2,3)
> s.view.map(_ + 1).map(_ + 1).force
<console>:67: error: value force is not a member of Seq[Int]
似乎如果我map 超过Array view 不止一次SeqView 变成Seq。
> a.view.map(_+1)
res212: scala.collection.SeqView[Int,Array[Int]] = SeqViewM(...)
> a.view.map(_+1).map(_+1)
res211: Seq[Int] = SeqViewMM(...)
我怀疑这种行为可能与Array 是一个可变集合有关,因为我无法使用List 或Vector 复制这种行为。不过我可以filterArrayview 任意多次。
【问题讨论】:
-
奇怪,看起来像 REPL 中的一个错误,因为我的 IDE 正确输入了这个。如果你能得到更多的反馈,也许你应该提交一个错误。
-
使用 scala 的 IDE 我在尝试做
a.view.map(_ + 1).map(_ + 1).force时遇到同样的错误@ -
不过,它会让我在上面做
.asInstanceOf[SeqView[Int, Array[Int]]].force而不会抱怨。
标签: scala collections scala-collections