【发布时间】:2016-11-15 21:46:15
【问题描述】:
考虑以下几点:
有一个包含一定行数的文本文件,例如:
测试.txt: 一种 b C d e F G h
(每个都有自己的一行)
那么就有下面这个类用于解析:
class MyAwesomeParser
{
def parse(fileName: String, readLines: Int): IndexedSeq[String] =
{
val randomAccessFile = new RandomAccessFile(fileName, "r")
val x: IndexedSeq[String] = for
{
x <- 0 until readLines
r = randomAccessFile.readLine()
} yield r
x
}
}
测试来了:
class MyAwesomeParserTest extends WordSpec
{
"MyAwesomeParser" when {
"read" should {
"parse only specified number of lines" in {
val parser = new EdgeParser("")
val x = parser.parse("test.txt", 5)
assert(x.size == 5)
}
}
"MyAwesomeParser" when {
"read" should {
"parse only until end of file" in {
val parser = new EdgeParser("")
val x = parser.parse("test.txt", 10)
assert(x.size == 8)
}
}
}
}
第二个测试是有问题的。现在你当然会说,你这里少了一个守卫......好吧,好吧,如果我添加
x <- 0 until readLines if randomAccessFile.readLine != null
然后它会跳过几行,因为 readLine 已经使用了该行。
r = randomAccessFile.readLine
x <- 0 until readLines if r != null
很遗憾,这行不通,因为第一行必须是为了理解而分配的。
现在我想知道,基于 readLine != null 条件,for 理解是否甚至可以循环直到给定次数或停止?
我的语法有问题吗?
【问题讨论】:
标签: scala for-loop randomaccessfile for-comprehension