Interesting implementation of coroutine. It is based
on the object-oriented pattern, where object
contains the state.
However, coroutine is a "routine", i.e. a single function
with local variables as it's state. This function
then "yields" some results to a parallel owning
context, and then resumes execution. Such as,
cr=: 4 : 0
while. x < y do.
yield x
x=. x+1
end.
)
Coroutines besides business state maintain
execution state, thus closely knit with
continuations. Closest thing in J of an
execution state snapshot is Debug facilities.
In this regard, recall simulation of coroutines,
http://www.jsoftware.com/pipermail/programming/2006-March/001513.html
Also coroutine is more a syntactic sugar, not an
implementation. For example, in C# 2.0, coroutines
are implemented with objects similar to proposed here,
but in source code they look like coroutines.
--- Pascal Jasmin <[EMAIL PROTECTED]> wrote:
> that was it thanks :)
> here is updated class to work with gerund parameters too.
>
> coclass'coroutine'
> NB. Coroutines are functions with bonded state, that when called return a
> value
> NB. and update their bonded state, ready to be called a next time. Other
> languages implement
> this similarly to putting the function into debug pause state.
> NB.This is a much simpler and more versatile approach: just save the state
> somewhere easy to
> find.
>
> NB.CREATE:
> NB. savedobject =. verb Coroutine [noun Initialdata] or,
> NB. savedobject =. resultverb`transfromverb Coroutine [noun Initialdata]
> NB. first version is equivalen to verb`}. Coroutine [noun Initialdata]
> NB.USE:
> NB. resume__savedobject ''
> NB. after each call, the state is beheaded, or transform function passed as
> 2nd gerund is used.
> Note 'Example'
> a1=: {. Coroutine 1 2 3 4 5
>
> resume__a1 ''
> 1
> (resume__a1 + ]) 10 20
> 12 22
> (resume__a1 + ])("0) 10 20
> 13 24
> resume__a1 ''
> 5
> (i.0 0) -: resume__a1 ''
> 1
> )
>
> function=: i.0
> transform=: }.
>
> create=: 3 : 0
> STATE=: y
> )
> destroy=: codestroy
>
>
> resume=: 3 : 0
> if. 0=#STATE do. return. end.
> (STATE=: transform STATE) ] function STATE
> )
> NB. possible alternative: call y function STATE to use param to resume.
>
>
> lazy=: 3 : 0
> NB. lazy data structures
> NB. not meant to be mixed and matched with resume_coroutine_ but it is the
> same code.
> NB. this version is meant to be slighly faster and simpler.
> NB. if verb, then new STATE=: f STATE
> if. 0=#STATE do. return. end.
> ] STATE=: function STATE
>
> )
>
> cocurrent 'base'
>
> Coroutine=: 1 : 0
> lastobj =: y conew 'coroutine'
> function__lastobj =: u
> if. 4!:0 <'function__lastobj' do. else. NB. if gerund... if not, just behead
> list
> '`function__lastobj transform__lastobj' =: function__lastobj
> end.
> lastobj
> )
>
>
>
> ----- Original Message ----
> From: Henry Rich <[EMAIL PROTECTED]>
> To: Programming forum <[email protected]>
> Sent: Friday, October 27, 2006 11:08:58 PM
> Subject: RE: [Jprogramming] Neat coroutine implementation
>
> I still don't know what you are looking for. If you could
> give a more detailed spec it would be nice.
>
> '`p m' =: +`-
>
> (note the leading `) assigns verbs (more precisely, it assigns
> from the ARs of the right argument, so it can assign all types).
>
> Henry Rich
>
> > -----Original Message-----
> > From: [EMAIL PROTECTED]
> > [mailto:[EMAIL PROTECTED] On Behalf Of Pascal Jasmin
> > Sent: Friday, October 27, 2006 10:45 PM
> > To: Programming forum
> > Subject: Re: [Jprogramming] Neat coroutine implementation
> >
> > I don't think it will work. I'd like to make a parameter to
> > an adverb be either a verb, or a gerund of verbs... and if it
> > is a gerund overide default class verbs with the extra parameters.
> >
> > this just gives literals:
> > 'p m' =: +`-
> >
> > this works if you know you want monad or dyad
> > p=: 4 : ('x' , p , 'y')
> >
> > ----- Original Message ----
> > From: Henry Rich <[EMAIL PROTECTED]>
> > To: Programming forum <[email protected]>
> > Sent: Friday, October 27, 2006 10:14:24 PM
> > Subject: RE: [Jprogramming] Neat coroutine implementation
> >
> > is
> > > there a way to convert a gerund into verbs instead of strings?
> >
> > Don't know just what you need, but have you looked at
> >
> > gerund `: 6
> >
> > ?
> >
> > Henry Rich
> >
> > ----------------------------------------------------------------------
> > For information about J forums see http://www.jsoftware.com/forums.htm
> >
> >
> >
> >
> >
> > ----------------------------------------------------------------------
> > For information about J forums see
> > http://www.jsoftware.com/forums.htm
>
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>
>
>
>
>
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>
__________________________________________________________________________________________
Check out the New Yahoo! Mail - Fire up a more powerful email and get things
done faster.
(http://advision.webevents.yahoo.com/mailbeta)
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm