Re: [Vala] Is there a way to use the Vala coroutines to achieve the same effect as in the following Python snippet (coroutines returning partial results in an iterator-like way) ?



In the meantime, tried the "scanner" example (my last entry in the Vala
maling list with an iterator.

Of course, since the vala iterator returns one item at a time it does the
trick.

The problem, on the second degree is that the vala iterator needs know in
advance the value of the next element t be reutuned (via get) and does not
wait.

An obvious way around this is to store the words (continuously pulled from
the input stream) temporary in a Queue, and to "feed to the Queue" on one
hand (scan method), and to "consume from the Queue" on the other hand (get()
method), hence using a sort of temporary dynamic buffer (and preserving the
order of the data).

Pb with this alternative scheme:

- Writing to a Vala Queue is O(1).
- reading (and deleting) from it is O(log(n))

So this alternative sheme, while emulating a "generator" and hence, saving a
lot of memory (compared to returning all the elements at once, as a list),
is extremely slow (many orders of magnitude).

Perhaps there are other alternatives:

- Sockets (probably too slow and overkill).
- Threads ?
All suggestions welcome !

Sincerely,
Serge Hulne.


Ref. : Hereunder : The scanner case-study (with the naive list-based
implementation)

//////////////////
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;
    }

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


On Sat, Jul 9, 2011 at 8:39 AM, Luca Bruno <lethalman88 gmail com> wrote:

On Sat, Jul 09, 2011 at 08:04:38AM +0800, Nor Jaidi Tuah wrote:

"The yield statement is only used when defining a generator function,
and is
only used in the body of the generator function. Using a yield
statement in
a function definition is sufficient to cause that definition to create
a
generator function instead of a normal function."

I use such feature before in Ruby and miss it in Vala.
So, I'm anxiously awaiting the answer to the following question
in the original post.

Vala has no generators and this behavior has nothing to do with coroutines.
So I guess it's useless to show a possible solution without syntax
support, you would say "it's not the same".

--
http://www.debian.org - The Universal Operating System



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