Another new io library

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Fri Feb 19 06:03:54 PST 2016


On 2/19/16 5:22 AM, Kagamin wrote:
> On Thursday, 18 February 2016 at 18:27:28 UTC, Steven Schveighoffer wrote:
>> The philosophy that I settled on is to create an iopipe that extends
>> one "item" at a time, even if more are available. Then, apply the
>> range interface on that.
>>
>> When I first started to write byLine, I made it a range. Then I
>> thought, "what if you wanted to iterate by 2 lines at a time, or
>> iterate by one line at a time, but see the last 2 for context?", well,
>> then that would be another type, and I'd have to abstract out the
>> functionality of line searching.
>
> You mean window has current element and context - lookahead and
> lookbehind? I stumbled across this article
> http://blog.jooq.org/2016/01/06/2016-will-be-the-year-remembered-as-when-java-finally-had-window-functions/
> it suggests that such window abstraction is generally useful for data
> analysis.

window doesn't have any "current" pointer. The window itself is the 
current data. But with byLine, you could potentially remember where the 
last N lines were delineated. Hm...

auto byLineWithContext(size_t extraLines = 1, Chain)(Chain c)
{
    auto input = byLine(c);
    static struct ByLineWithContext
    {
       typeof(input) chain;
       size_t[extraLines] prevLines;
       auto front() { return chain.window[prevLines[$-1] .. $]; }
       void popFront()
       {
           auto offset = prevLines[0];
           foreach(i; 0 .. prevLines.length-1)
           {
               prevLines[i] = prevLines[i+1] - offset;
           }
           prevLines[$-1] = chain.window.length - offset;
           chain.release(offset);
           chain.extend(0); // extend in the next line
       }
       void empty()
       {
           return chain.window.length != prevLines[$-1];
       }
       // previous line of context (i = 0 is the oldest context line)
       auto contextLine(size_t i)
       {
           assert(i < prevLines.length);
           return chain.window[i == 0 ? 0 : prevLines[i-1] .. prevLines[i])
       }
    }
    return ByLineWithContext(input);
}

It's an interesting transition to think about looking at an entire 
buffer of data instead of some pointer to a single point in a stream as 
the primitive that you have.

-Steve


More information about the Digitalmars-d mailing list