problem with byLine

Ali Çehreli acehreli at yahoo.com
Tue May 15 11:24:23 PDT 2012


On 05/15/2012 09:03 AM, Matt Soucy wrote:

 >>> I believe byLine reuses the internal buffer. Try duping the lines:
 >>> auto i = f.byLine().map!"a.idup"().array();
 >> Can someone please explain to me the last line?
 >>
 >> I'm trying to learn D, by playing with code and reading this forum. I'm
 >> a slow learner. :)
 >>
 >> Anyways, I looked at std.stdio code and noticed that byLine resturns a
 >> struct ByLine, but where does the .map come from? Thanks!
 >>
 >
 > It comes from std.algorithm. What that line does is:
 > f.byLine() // Get by lines, exactly as you know already

Just to clarify: what byLine() returns is a lazy range. Nothing is read 
from the file just by that line.

 > .map!"a.idup"() // Iterate over the byLine, and make a Range of
 > immutable strings with the same contents as each line.

And even map() is lazy: It returns a lazy range that will apply .idup() 
to the lines that it takes from ByLine.

 > .array() // Convert it from a range to an array of strings

And that's the eager one that consumes the lazy range that map() returns.

Ali

-- 
D Programming Language Tutorial: http://ddili.org/ders/d.en/index.html



More information about the Digitalmars-d-learn mailing list