Showing posts with label coroutine. Show all posts
Showing posts with label coroutine. Show all posts

2012-08-01

Python's generators and coroutines

2.16: adda/cstr/1st-class functions/
generators and coroutines:

. a generator in python
is a function identified by it's body:
it's calling yield instead of return .
. the easier way to view this
in a typed lang, like adda,
is to set the return type as a stream ?
. streams are conceptually like files
so they should work like files;
eg, to restart sequence do an
f.open(name of generator instead of file) .
. streams are like sequences, in that
seek(n) can be done by restarting the generator
and asking for an nth item .
. the coroutine in python is like a generator;
but instead of streaming output,
it has a streaming input:
f`open mycoroutine(init);
f`put (some data to send to it) .
. it could do put's to output, and get's to input;
being both a generator and a coroutine .

2010-03-30

act'rec as implicit parameter to coroutine

3.11: adda/translation/coroutines/act'rec as implicit parameter:
. after seeing coroutines for c
I saw a way that uses
another implicit parameter:
the param is a record of
all the routine's needed locals,
along with a yield pointer .
. it returns that record,
and then is called with the same record .
. when called again with the same record,
it uses the yield pointer to jump to where it left off .
. this gets a bit messy when
the yield is inside loops:
this is not really a problem with c,
where loops are very primitive:
a for-loop is just a while-loop is just a
combination goto and conditional
-- you can goto anywhere .