phobos: What type to use instead of File when doing I/O on streams?
Alex Parrill via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Nov 9 10:43:58 PST 2015
On Monday, 9 November 2015 at 18:18:19 UTC, J.Frank wrote:
> On Monday, 9 November 2015 at 14:48:35 UTC, Ali Çehreli wrote:
>> import std.stdio;
>> import std.range;
>>
>> void foo(I, O)(I in_stream, O out_stream)
>> if (isInputRange!I &&
>> isOutputRange!(O, ElementType!I)) {
>>
>> // in_stream.seek(3); // compile error - good :)
>>
>> foreach (element; in_stream) {
>> out_stream.put(element);
>> }
>> }
>>
>> void main(string[] args)
>> {
>> // Also consider .byLine, which is faster and risky
>> foo(stdin.byLineCopy,
>> stdout.lockingTextWriter);
>> }
>>
>> Ali
>
> Uhm... no. That's not a solution to my problem. Ranges are not
> (I/O-)streams.
Ranges are streams. file.byLine(Copy) and byChunk are effectively
streams that are ranges.
The only problem with implementing your code in ranges is that
`splitter` requires a forward range, which file-backed ranges
aren't. Otherwise you could do
`file.byChunk(4096).joiner.drop(3).splitter('\n')` to get a
"stream" of lines.
More information about the Digitalmars-d-learn
mailing list