Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stream of readLines

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?

like image 321
Ukonu Avatar asked Jul 22 '10 06:07

Ukonu


People also ask

What is stream and stream reader?

StreamReader is designed for character input in a particular encoding, whereas the Stream class is designed for byte input and output. Use StreamReader for reading lines of information from a standard text file. Important. This type implements the IDisposable interface.

How to read file into MemoryStream in c#?

You can simply call File. OpenRead to get a FileStream containing the file. If you really want the file to be in a MemoryStream, you can call CopyTo to copy the FileStream to a MemoryStream.


1 Answers

import java.io.{BufferedReader, InputStreamReader}
val in = new BufferedReader(new InputStreamReader(System in))
val input = Stream.continually(in readLine)
like image 104
Eastsun Avatar answered Sep 21 '22 23:09

Eastsun