Scala 的使用
Scala USING of USING
我的 csv 数据如下:
1;57840
2;57840
3;57834
...
以及将此数据读入 Tuple2[Int,Int]
序列的简单 scala 代码
代码:
case class Tick(Data : Tuple2[Int,Int]) {
val tNum : Int = Data._1
val tVal : Int = Data._2
}
case class TicksDs(Data : Seq[Tick]) {
def size : Int = Data.size
}
class scvReader(CsvPath : String){
def using[A <: { def close(): Unit }, TicksDs](param: A)(f: A => TicksDs): TicksDs =
try {
f(param)
} finally {
//param.close()
}
def readTicks : TicksDs = {
using(io.Source.fromFile(CsvPath)) { source =>
TicksDs(source.getLines.map{oneLine => (oneLine.split(";")(0).toInt-1,
oneLine.split(";")(1).toInt)}.toSeq.map(x => Tick(x._1,x._2)))
}
}
}
object CurryingFuncs extends App {
val ticks : TicksDs = new scvReader("data/simple.csv").readTicks
println("ticks.size="+ticks.size+" ticks(Class): "+ticks.getClass.getName)
for (tick <- ticks.Data) {
println(tick.tNum+" "+tick.tVal+" "+tick.getClass.getName)
}
}
一切正常,这 return 我输出:
ticks.size=50 ticks(Class): TicksDs
0 57840 Tick
1 57840 Tick
2 57834 Tick
3 57831 Tick
...
但是当我取消注释行 //param.close() 时它会引发错误
Exception in thread "main" java.io.IOException: Stream Closed
at java.io.FileInputStream.readBytes(Native Method)
...
...
错在哪里,如何改正?谢谢。
修复(感谢 jwvh):
...
def using[A <: { def close(): Unit }, B](param: A)(f: A => B): B =
try f(param)
finally param.close()
...
...
using(io.Source.fromFile(CsvPath)) { source =>
TicksDs(source.getLines.map{oneLine => (oneLine.split(";")(0).toInt-1,
oneLine.split(";")(1).toInt)}.toList.map(x => Tick(x._1,x._2))) //was .toSeq now .toList
}
错误是由于您在清空源之前关闭了它。
这 source.getLines
会产生一个 Iterator[String]
,它被延迟计算,而 toSeq
之后同一行不会改变它。如果您使用 toList
而不是,那么将强制在 close()
.
之前评估完整的文件内容
此外,您不需要 TicksDs
作为 using()
的类型参数。它没有任何作用。
def using[A <: {def close(): Unit}]( . . .
我的 csv 数据如下:
1;57840
2;57840
3;57834
...
以及将此数据读入 Tuple2[Int,Int]
序列的简单 scala 代码代码:
case class Tick(Data : Tuple2[Int,Int]) {
val tNum : Int = Data._1
val tVal : Int = Data._2
}
case class TicksDs(Data : Seq[Tick]) {
def size : Int = Data.size
}
class scvReader(CsvPath : String){
def using[A <: { def close(): Unit }, TicksDs](param: A)(f: A => TicksDs): TicksDs =
try {
f(param)
} finally {
//param.close()
}
def readTicks : TicksDs = {
using(io.Source.fromFile(CsvPath)) { source =>
TicksDs(source.getLines.map{oneLine => (oneLine.split(";")(0).toInt-1,
oneLine.split(";")(1).toInt)}.toSeq.map(x => Tick(x._1,x._2)))
}
}
}
object CurryingFuncs extends App {
val ticks : TicksDs = new scvReader("data/simple.csv").readTicks
println("ticks.size="+ticks.size+" ticks(Class): "+ticks.getClass.getName)
for (tick <- ticks.Data) {
println(tick.tNum+" "+tick.tVal+" "+tick.getClass.getName)
}
}
一切正常,这 return 我输出:
ticks.size=50 ticks(Class): TicksDs
0 57840 Tick
1 57840 Tick
2 57834 Tick
3 57831 Tick
...
但是当我取消注释行 //param.close() 时它会引发错误
Exception in thread "main" java.io.IOException: Stream Closed
at java.io.FileInputStream.readBytes(Native Method)
...
...
错在哪里,如何改正?谢谢。
修复(感谢 jwvh):
...
def using[A <: { def close(): Unit }, B](param: A)(f: A => B): B =
try f(param)
finally param.close()
...
...
using(io.Source.fromFile(CsvPath)) { source =>
TicksDs(source.getLines.map{oneLine => (oneLine.split(";")(0).toInt-1,
oneLine.split(";")(1).toInt)}.toList.map(x => Tick(x._1,x._2))) //was .toSeq now .toList
}
错误是由于您在清空源之前关闭了它。
这 source.getLines
会产生一个 Iterator[String]
,它被延迟计算,而 toSeq
之后同一行不会改变它。如果您使用 toList
而不是,那么将强制在 close()
.
此外,您不需要 TicksDs
作为 using()
的类型参数。它没有任何作用。
def using[A <: {def close(): Unit}]( . . .