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) ?



On Fri, Jul 08, 2011 at 02:43:46PM +0200, Serge Hulne wrote:
Is there a way to use the Vala coroutines to achieve the same effect
as in the following Python snippet:

/////////////////////////////

################
# Python coroutine
###############*


def countdown(n):
   print "Counting down from", n
   while n > 0:
       yield n
       n -= 1
   print "Done counting down"


# Example use
if __name__ == '__main__':
   for i in countdown(10):
       print i

///////////////////////////////

More precisely, is there a way to use a coroutine in order to achieve
an iterator-like effect, namely:

A regular routine would store the results in a data structure
(array,list ...) and return the whole bunch of results, all at once,
when the routine completes.

In the python example, above, partial results can be "returned" (or
rather transmitted using the yield keyword) as soon as they show up in
the coroutine.

- Can a similar effect be achieved in Vala using coroutines (or
something else) ?

That's not a coroutine, it's a generator.

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



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