Creating InputRanges from strings, files etc.
rikki cattermole
rikki at cattermole.co.nz
Thu Nov 8 14:04:38 UTC 2018
On 09/11/2018 2:58 AM, Vinay Sajip wrote:
> Excuse my ignorance, but from looking at the documentation on std.range
> and a quick skim of the guides mentioned there near the top, I can't see
> what the simple way is of creating an InputRange!(ubyte) from strings,
> files etc. I would have expected to find something in the DLang Tour
> about this, but couldn't find anything. Please can someone tell me how
> to do it? Just to be clear, I want to create an object which is an
> InputRange!(ubyte) and pass that around, rather than e.g. iterate over a
> string or a file.
TLDR of how to write an input range:
struct MyInputRange {
ubyte[] input;
@property {
ubyte front() {
return this.input[0];
}
bool empty() {
return this.input.length == 0;
}
}
void popFront() {
this.input = this.input[1 .. $];
}
}
import std.stdio;
void main() {
foreach(b; MyInputRange([1, 2, 3])) {
writeln(b);
}
}
More information about the Digitalmars-d-learn
mailing list