input range from stdin

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Sep 17 11:05:36 PDT 2014


On 09/17/2014 08:30 AM, krzaq wrote:
> On Wednesday, 17 September 2014 at 14:37:21 UTC, Marc Schütz wrote:
>> On Wednesday, 17 September 2014 at 12:44:00 UTC, krzaq wrote:
>>> I'd like to have something similar to C++'s
>>> std::istream_iterator<int>(std::cin)
>>>
>>> Is it possible? I'm relatively indifferent to efficiency of the
>>> solution.
>>
>> import std.stdio;
>> import std.algorithm;
>> import std.conv;
>> writeln(stdin.byLine.map!(to!int));
>
> What happens if I later have some strings that I want to read from the
> same line?
>
> How can I use the resultant range with std.fill?
>
> My idea doesn't seem to work: http://dpaste.dzfl.pl/130e14c927f3

The following worked:

import std.stdio;
import std.format;
import std.exception;
import std.string;
import std.algorithm;

struct Data
{
     int i;
     string s;
}

Data toData(char[] line)
{
     int i;
     string s;
     auto slice = line;

     const items = formattedRead(line, " %s %s", &i, &s);
     enforce (items == 2, format("Incomplete line: %s", slice));

     return Data(i, s);
}

void main()
{
     auto data = stdin.byLine.map!toData;
     writeln(data);
}

I could not get it work with fill because fill requires specific types 
of ranges, which neither the destination nor the source were. For 
example, I wanted to use std.array.Appender but fill wants an 
InputRange. Also, the source is not a ForwardRange because it is 
consuming from stdin.

However, it is easy to make an array with std.array.array:

     import std.array;
     writeln(data.array);

Ali



More information about the Digitalmars-d-learn mailing list