persistent byLine

Brad Anderson eco at gnuk.net
Mon Jul 22 13:57:34 PDT 2013


On Monday, 22 July 2013 at 12:08:06 UTC, Nick Treleaven wrote:
> 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?

I remember seeing Walter's component programming example and 
feeling that the idup stuck out like a sore thumb.

I like the idea. Makes simple programs even simpler without 
sacrificing performance by changing byLines.  Ideally readText 
would take generic streams rather than Files but that will have 
to wait until std.io gets finished.


More information about the Digitalmars-d mailing list