August 02, 2017 Re: Using lazy code to process large files | ||||
---|---|---|---|---|
| ||||
Attachments:
| 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@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 > |
August 02, 2017 Re: Using lazy code to process large files | ||||
---|---|---|---|---|
| ||||
Dne 2.8.2017 v 14:11 Daniel Kozak via Digitalmars-d-learn napsal(a):
> 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);
> }
Thanks a lot. That did the trick.
Martin
|
Copyright © 1999-2021 by the D Language Foundation