readLines 流
我正在尝试从 readLine 调用创建无限的字符串流:
import java.io.{BufferedReader, InputStreamReader}
val in = new BufferedReader(new InputStreamReader(System in))
val input: Stream[String] = Stream.cons(in readLine, input)
但看起来 readLine 调用并未被延迟调用。输入该代码后,readLine 立即期望输入,然后 Stream 成为同一输入的无限列表。是否有可能实现我的想法?
I'm attempting to create an infinite stream of strings from readLine calls:
import java.io.{BufferedReader, InputStreamReader}
val in = new BufferedReader(new InputStreamReader(System in))
val input: Stream[String] = Stream.cons(in readLine, input)
But it appears that the readLine call isn't being called lazily. Immediately after entering that code, the readLine expects input then the Stream becomes an infinite list of that same input. Is it possible to accomplish what I have in mind?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请参阅 Stream 中的示例。请注意,惰性重击位于尾部,而不是头部。每次调用 thunk 时,它都应该返回下一个 cons(包括下一个 thunk,而下一个 thunk 又应该提供下一个 cons,包括......)
这是 Stream.cons 的签名: <http://www.scala-lang.org/docu/files/api/scala/集合/不可变/流$$cons$.html>。请注意 thunk (=> Stream) 作为
apply
的第二个参数。See the example at Stream. Note that the lazy thunk is in the tail, not the head. Each time the thunk is invoked it should return the next cons (including the next thunk which in turn should supply the next cons incl....)
Here is the signature for Stream.cons: <http://www.scala-lang.org/docu/files/api/scala/collection/immutable/Stream$$cons$.html>. Note the thunk (=> Stream) as the 2nd argument to
apply
.