【发布时间】:2023-04-01 01:45:01
【问题描述】:
我正在尝试映射文件流,但只评估第一个元素:
var fList = Stream(new java.io.File("test1") , new java.io.File("test2"))
//> fList : scala.collection.immutable.Stream[java.io.File] = Stream(test1, ?)
//|
fList.map(file => {
println(file.getAbsolutePath)
}) //> C:\Eclipse\scala-SDK-3.0.2-vfinal-2.10-win32.win32.x86\eclipse\test1
//| res0: scala.collection.immutable.Stream[Unit] = Stream((), ?)
Reading http://daily-scala.blogspot.com/2010/01/introducing-streams.html Streams are a special type of Iterable/Traversable whose elements are not evaluated until they are requested. Streams are normally constructed as functions. 由于我在 Stream 上使用 map,是否应该评估流中包含的每个文件并因此输出其名称?
更新:我发现这很有用,是使用 Stream 的替代方法:Reading files from a directory in Scala
【问题讨论】:
标签: scala