- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
对 Scala 2.8 的收集行为感到非常沮丧。问题是:我正在创建一个数独板。我正在标记从 A1 到 I9 的单元格(字母是行,数字是列)。我想获得棋盘上的单位列表,即第 9 行、夜间列和夜间象限。
这是我的 Scala 类:
class Square(val row:Char, val column:Int) extends Pair[Char, Int](row, column) {
override def toString() = "" + row + column
}
object Board {
private val rows = "ABCDEFGHI"
private val cols = 1 to 9
private lazy val units = unitList(rows, cols)
private def cross(rows:Iterable[Char], columns:Iterable[Int]):Iterable[Square] = {
for (row <- rows; col <- columns)
yield new Square(row, col)
}
private def unitList(rows:Iterable[Char], cols:Iterable[Int]) = {
val u1 = (for (col <- cols) yield cross(rows, List(col)))
val u2 = (for (row <- rows) yield cross(List(row), cols))
val u3 = (for (cols <- List("ABC", "DEF", "GHI"); rows <- List(1 to 3, 4 to 6, 7 to 9)) yield cross(cols, rows))
u1 :+ u2 :+ u3 // won't compile, reason: :+ is not a member of Iterable[Iterable[sudoku.Square]]
}
def run() {
val u1 = (for (col <- cols) yield cross(rows, List(col)))
val u2 = (for (row <- rows) yield cross(List(row), cols))
val u3 = (for (cols <- List("ABC", "DEF", "GHI"); rows <- List(1 to 3, 4 to 6, 7 to 9)) yield cross(cols, rows))
println(u1)
println(u2)
println(u3)
val u4 = u1 :+ u2 :+ u3 // compiles
println(u1 :+ u2 :+ u3) // compiles and output correctly
}
}
查看代码中的注释。具体来说,为什么相同的代码不能在 unitList 中编译,但在 run() 中编译并运行良好?
此外,当我观察 run 方法的输出时,似乎 yield 关键字返回的集合在 Vector 和 List 之间随机切换:
Vector(Vector(A1, B1, C1, D1, E1, F1, G1, H1, I1), Vector(A2, B2, C2, D2, E2, F2, G2, H2, I2), Vector(A3, B3, C3, D3, E3, F3, G3, H3, I3), Vector(A4, B4, C4, D4, E4, F4, G4, H4, I4), Vector(A5, B5, C5, D5, E5, F5, G5, H5, I5), Vector(A6, B6, C6, D6, E6, F6, G6, H6, I6), Vector(A7, B7, C7, D7, E7, F7, G7, H7, I7), Vector(A8, B8, C8, D8, E8, F8, G8, H8, I8), Vector(A9, B9, C9, D9, E9, F9, G9, H9, I9))
Vector(List(A1, A2, A3, A4, A5, A6, A7, A8, A9), List(B1, B2, B3, B4, B5, B6, B7, B8, B9), List(C1, C2, C3, C4, C5, C6, C7, C8, C9), List(D1, D2, D3, D4, D5, D6, D7, D8, D9), List(E1, E2, E3, E4, E5, E6, E7, E8, E9), List(F1, F2, F3, F4, F5, F6, F7, F8, F9), List(G1, G2, G3, G4, G5, G6, G7, G8, G9), List(H1, H2, H3, H4, H5, H6, H7, H8, H9), List(I1, I2, I3, I4, I5, I6, I7, I8, I9))
List(Vector(A1, A2, A3, B1, B2, B3, C1, C2, C3), Vector(A4, A5, A6, B4, B5, B6, C4, C5, C6), Vector(A7, A8, A9, B7, B8, B9, C7, C8, C9), Vector(D1, D2, D3, E1, E2, E3, F1, F2, F3), Vector(D4, D5, D6, E4, E5, E6, F4, F5, F6), Vector(D7, D8, D9, E7, E8, E9, F7, F8, F9), Vector(G1, G2, G3, H1, H2, H3, I1, I2, I3), Vector(G4, G5, G6, H4, H5, H6, I4, I5, I6), Vector(G7, G8, G9, H7, H8, H9, I7, I8, I9))
Vector(Vector(A1, B1, C1, D1, E1, F1, G1, H1, I1), Vector(A2, B2, C2, D2, E2, F2, G2, H2, I2), Vector(A3, B3, C3, D3, E3, F3, G3, H3, I3), Vector(A4, B4, C4, D4, E4, F4, G4, H4, I4), Vector(A5, B5, C5, D5, E5, F5, G5, H5, I5), Vector(A6, B6, C6, D6, E6, F6, G6, H6, I6), Vector(A7, B7, C7, D7, E7, F7, G7, H7, I7), Vector(A8, B8, C8, D8, E8, F8, G8, H8, I8), Vector(A9, B9, C9, D9, E9, F9, G9, H9, I9), Vector(List(A1, A2, A3, A4, A5, A6, A7, A8, A9), List(B1, B2, B3, B4, B5, B6, B7, B8, B9), List(C1, C2, C3, C4, C5, C6, C7, C8, C9), List(D1, D2, D3, D4, D5, D6, D7, D8, D9), List(E1, E2, E3, E4, E5, E6, E7, E8, E9), List(F1, F2, F3, F4, F5, F6, F7, F8, F9), List(G1, G2, G3, G4, G5, G6, G7, G8, G9), List(H1, H2, H3, H4, H5, H6, H7, H8, H9), List(I1, I2, I3, I4, I5, I6, I7, I8, I9)), List(Vector(A1, A2, A3, B1, B2, B3, C1, C2, C3), Vector(A4, A5, A6, B4, B5, B6, C4, C5, C6), Vector(A7, A8, A9, B7, B8, B9, C7, C8, C9), Vector(D1, D2, D3, E1, E2, E3, F1, F2, F3), Vector(D4, D5, D6, E4, E5, E6, F4, F5, F6), Vector(D7, D8, D9, E7, E8, E9, F7, F8, F9), Vector(G1, G2, G3, H1, H2, H3, I1, I2, I3), Vector(G4, G5, G6, H4, H5, H6, I4, I5, I6), Vector(G7, G8, G9, H7, H8, H9, I7, I8, I9)))
我完全迷失在这里。
最佳答案
for 理解中 yield 的结果是从第一个生成器的类型派生的,在你的方法中,你限制了参数的类型,到目前为止你失去了你的 :+ 方法。
//The type of 1 to 9 is show below
scala> 1 to 9
res0: scala.collection.immutable.Range.Inclusive with scala.collection.immutable.Range.ByOne = Range(1, 2, 3, 4, 5, 6, 7, 8, 9)
//If you cast it to Iterable[Int] it doens't have the :+ method
scala> (res0:Iterable[Int]) :+ 1
<console>:7: error: value :+ is not a member of Iterable[Int]
(res0:Iterable[Int]) :+ 1
^
//But if you don't, you have it
scala> res0 :+ 1
res6: scala.collection.immutable.IndexedSeq[Int] = Vector(1, 2, 3, 4, 5, 6, 7, 8, 9, 1)
//And to prove that for comprehensions yield derives the type of the first generator:
scala> for(a <- res0) yield a
res7: scala.collection.immutable.IndexedSeq[Int] = Vector(1, 2, 3, 4, 5, 6, 7, 8, 9)
scala> for(a <- (res0:Iterable[Int])) yield a
res8: Iterable[Int] = Vector(1, 2, 3, 4, 5, 6, 7, 8, 9)
关于Scala 集合 : totally unpredictable behaviours,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3292486/
这个问题在这里已经有了答案: Relationship between Threads and println() statements (2 个回答) 6年前关闭。 我正在浏览 volatile 的
对 Scala 2.8 的收集行为感到非常沮丧。问题是:我正在创建一个数独板。我正在标记从 A1 到 I9 的单元格(字母是行,数字是列)。我想获得棋盘上的单位列表,即第 9 行、夜间列和夜间象限。
问题的一点概述。 假设我有一个名为 TableA 的表,它具有固定属性 PropertyA、PropertyB、PropertyC。这已经足以满足您自己的网站需求,但是您突然有客户想要在您的网站上自定
我有一些类型的实例。我们称它们为 A、B 和 C。它们都是类型类 X 的实例。现在我想创建一个单独的函数 create给定一些输入(比如说一个字符串),它会创建一个 A、B 或 C 的实例。类型系统无
如有必要,我会发布一个 JSFIddle,因为我使用的是标记语言,这会花费一些精力,但请转到此处:http://mixtape.meteor.com 添加足够的元素,以便溢出并出现滚动条 现在尝试检查
请引用下面的 LinqPad 脚本。 在实现工作流时,我从集合 (IEnumerable) 中获取下一组 HasRun 任务。在迭代 Linq 查询的结果集时,我将任务更改为 HasRun = tru
我正在创建一个 PDF,其中包含几个简单的表格。每个表格包含 1-2 个单元格:一个图像单元格,大多数表格还包含一个文本单元格。 它适用于所有 10 个图像,除了其中一张图像:图像未渲染。 现在,我获
我正在帮助处理仪表板中的交互式表格。单击表中的行会更改显示在仪表板其余部分的数据。表行可以有一个 'selected' 类,其中选定的行有一个 4px 的粗体边框。这些行还有一个 :hover 选择器
当使用 myDelegate -= eventHandler ReSharper(版本 6)问题时: Delegate subtraction has unpredictable result 这背后
我在 xcode 中打开了一个新标志,并收到警告“弱接收器在 ARC 模式下可能不可预测地为空”。这让我很困惑,因为它当然可能为零。 最佳答案 我一周前问过这个问题,但没有收到任何答复,但格雷格·帕克
我编写了一个 Java 套接字服务器,它将保持连接处于 Activity 状态,直到客户端断开连接。我的客户端代码将继续将消息推送到该服务器应用程序。 但是当我运行这些程序一段时间时,我也发现了一种不
我是一名优秀的程序员,十分优秀!