persistent byLine

Nick Treleaven ntrel-public at yahoo.co.uk
Mon Jul 22 05:08:05 PDT 2013


Hi,
I remember an example in some slides by Walter which had this snippet 
(slightly simplified):

stdin.byLine.map!(l => l.idup).array

Someone commented in a reddit post that the idup part was not intuitive 
(can't find the link now, sorry).

I made a pull request to re-enable using byLine!(char, immutable char). 
(Note this compiled in the current release, but didn't work properly 
AFAICT. It did work by commit 97cec33^).

https://github.com/D-Programming-Language/phobos/pull/1418

Using that allows us to drop the map!(l => l.idup) part from the above 
snippet. The new syntax isn't much better, but it can also be more 
efficient (as it caches front). I have an idea how to improve the 
syntax, but I'll omit it for this post.

I've since thought that if most or all lines in a file need to be 
persistent, it may be more efficient to use 
readText(filename).splitLines, because that doesn't need to allocate for 
each line.

There are two enhancements for that approach:
1. readText should accept a File, not just a filename, so we can use stdin.
2. splitLines makes an array. It would be more flexible to have an input 
range created from a function e.g. lineSplitter.

With these enhancements, we could change the byLine docs to recommend 
using File.readText.lineSplitter if most lines need to be persistent.

Thoughts?


More information about the Digitalmars-d mailing list