Using lazy code to process large files

Daniel Kozak via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Aug 2 05:11:02 PDT 2017


import std.stdio;
import std.algorithm;

void main()
{


auto input  = ["... some text, another text", "some,another","...so,an"];

auto result = input.filter!(a => a.startsWith("..."))
          .map!(a=>a.splitter(",").map!(a=>a.stripLeft(' ')))
  .map!(a=>a.joiner(","));
writeln(result);
}


On Wed, Aug 2, 2017 at 1:44 PM, Martin DraĊĦar via Digitalmars-d-learn <
digitalmars-d-learn at puremagic.com> wrote:

> Hi,
>
> I am struggling to use a lazy range-based code to process large text
> files. My task is simple, i.e., I can write a non-range-based code in a
> really short time, but I wanted to try different approach and I am
> hitting a wall after wall.
>
> Task: read a csv-like input, take only lines starting with some string,
> split by a comma, remove leading and trailing whitespaces from splitted
> elements, join by comma again and write to an output.
>
> My attempt so far:
>
> alias stringStripLeft = std.string.stripLeft;
>
> auto input  = File("input.csv");
> auto output = File("output.csv");
>
> auto result = input.byLine()
>                    .filter!(a => a.startsWith("..."))
>                    .map!(a => a.splitter(","))
>                    .stringStripleft // <-- errors start here
>                    .join(",");
>
> output.write(result);
>
> Needless to say, this does not compile. Basically, I don't know how to
> feed MapResults to splitter and then sensibly join it.
>
> Thank you for any hint.
> Martin
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20170802/9178ab83/attachment.html>


More information about the Digitalmars-d-learn mailing list