On 1/04/2015 7:19 p.m., Suliman wrote:
Rikki, could you explain? I did not understand where it can help me

Here is some example code. While I've only implemented one InputRange!string instance. You would probably have one, for just zip's and another raw text files. Keep in mind it returns only a single line from standard input.

In other words, let the range handle reading a line from a file.

import std.range;
import std.stdio;

class Range : InputRange!string {
        private {
                string buffer;
        }
        
        this() {
                popFront;
        }
        
        @property {
                string front() {
                        return buffer;
                }
                
                bool empty() {
                        return false;
                }
        }
        
        void popFront() {
                buffer = readln();
        }

        string moveFront() {
                string ret = front();
                popFront();
                return ret;
        }
        
        int opApply(int delegate(string) del) {
                int result;
                
                while(!empty()) {
                        result = del(moveFront());
                        
                        if (result)
                                break;
                }
                
                return result;
        }
        
        int opApply(int delegate(size_t, string) del) {
                int result;
                size_t offset;
                
                while(!empty()) {
                        result = del(offset, moveFront());
                        
                        if (result)
                                break;
                        offset++;
                }
                
                return result;
        }
}

void myfunc(InputRange!string input) {
        foreach(line; input) {
                writeln("GOT [line]: ", line);
        }
}

void main() {
        InputRange!string theRange = new Range();
        myfunc(theRange);
}

Reply via email to