[Vala] Looking for an alternative to returning a large list of data in one chunk in Vala : Example in Vala



Hi Vala people,

I am trying to find a way to code the example hereunder  in a better
way in Vala.

I assume that instead of storing the resulting tokens in a list and
then returning the list, there must be a way to kind of iterate over
the partial results (the individual tokens : the "words in this
case").

- In Go, I would use a goroutine and send the tokens down a channel in
the scanner() method on one hand and recuperate them from said channel
in the main().
- In Python, I would use the generator I mentioned in my previous post
(which is as simple as "yield word").

Given my limited experience of Vala I don't know if there is a better
solution that the one hereunder :

Any help would be really appreciated,
Thanks,
Serge.






///////////////
using Posix;
using Gee;


LinkedList<string> scanner(string fname) {

    var f = FileStream.open(fname, "r");

    if (f==null) {
        Posix.stdout.printf("File missing !\n");
        exit(1);
    }

    StringBuilder wordBuffer = new StringBuilder("");
    char c = ' ';
    int wordBuffer_length = 0;
    var erased = true;
    var wordList = new LinkedList<string>();

    while (!f.eof()) {
        c = (char) f.getc();
        //if (!isalnum(c))
        if (isspace(c)) {
            if (wordBuffer.len>0) {
                //Posix.stdout.printf("word = %s\n", wordBuffer.str);
                wordList.add(wordBuffer.str);
                if (erased == false) {
                    wordBuffer.erase(0,-1);
                    wordBuffer_length = 0;
                    erased = true;
                }
            }
        } else {
            wordBuffer_length++;
            //Posix.stdout.printf("%d\n", wordBuffer_length);
            wordBuffer.append_c(c);
            erased = false;
        }
    }

    return wordList;
}



void main(string[] args) {
    var fname = "../corpora/shakes.txt";

    // -- testing scanner --

    var list = scanner(fname);
    var i = 0;
    foreach (var item in list) {
        stdout.printf("%s\n", item);
        i++;
        if (i > 10) return;
    }

}
///////////////



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]