readLines 流

发布于 2024-09-11 09:12:57 字数 324 浏览 6 评论 0原文

我正在尝试从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鹿港小镇 2024-09-18 09:12:58
import java.io.{BufferedReader, InputStreamReader}
val in = new BufferedReader(new InputStreamReader(System in))
val input = Stream.continually(in readLine)
import java.io.{BufferedReader, InputStreamReader}
val in = new BufferedReader(new InputStreamReader(System in))
val input = Stream.continually(in readLine)
述情 2024-09-18 09:12:58

请参阅 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文