Bug 3868 – It would be nice to have a function which read a file lazily using a range
Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
Other
OS
Linux
Creation time
2010-03-01T23:13:00Z
Last change time
2015-06-09T01:27:40Z
Assigned to
andrei
Creator
issues.dlang
Comments
Comment #0 by issues.dlang — 2010-03-01T23:13:24Z
It would be great if there were a function which returned a range to a file, reading it in as you moved through the range. Presumably, it would do some buffering, but it wouldn't be a case of reading in the whole file at a time and hopefully would be more friendly and powerful than reading in a single byte, or int, or whatever like you'd do with read() with streams. It also avoids having to explicitly read in a portion of the file at a time like you'd do with readBlock(). In addition, it would make it possible to use various of the standard algorithms directly on files.
It might also be useful to write to a file with a range, but I'm not acquainted well enough with output ranges to know how well it would work, or if it would be as useful as reading with ranges. It would probably be useful though.
In any case, I really like the idea of being to read a file with a range. Maybe there's a reason that I'm not aware of which would make it a really bad idea, but I think that it would be a good addition to phobos.
Comment #1 by dsimcha — 2010-08-19T19:14:53Z
Shouldn't std.stdio.File.byChunk() do this?
Comment #2 by andrei — 2010-08-19T21:08:24Z
byChunk uses opApply. We need to transform it into a range; using opApply alone severely limits the applicability of byChunk.
Here's a nice potential application of byChunk:
import std.stdio;
void main(string[] a) {
enforce(a.length == 3);
auto f1 = File(a[1]), f2 = File(a[2]);
immutable bufsize = 1024 * 1024;
return equal(f1.byChunk(bufsize), f2.byChunk(bufsize));
}
One other idea suggested by the above is to implement the algorithm found in diff in std.algorithm. Then we can write a diff program in a dozen line of code, using general components.
Comment #3 by peter.alexander.au — 2014-02-01T12:26:56Z
byChunk is now a proper range. Is everyone happy that this is resolved?