Your approach is very interesting fibonacci =: +/`([,}:@:]) Coroutine 1 1
in one construct you bind the functionality with the initial state, thus creating a stateful execution unit. But I am still weary of calling it a "coroutine", because a coroutine captures execution state in terms of stack frame, not only business state bound to execution piece. I believe, your construct is more closely related to lexical closure in this regard. http://www.jsoftware.com/jwiki/FrequentlyAskedQuestions/LexicalClosure Lexical closure is defined within and captures some state from the containing context. But it does not have the feature of suspending execution, as coroutine does. So it is more matching with your implementation. --- Pascal Jasmin <[EMAIL PROTECTED]> wrote: > > Thanks for your comments, Oleg. I was expecting you to have invented > something already, and > sure enough that is a very faithful version of the yield version. > > There is some increased elegance in the object approach. Especially for J. > It can be thought > as making the same "NoNad" (like saying that word :) that bind creates, > except granting the > flexibility of keeping separate access to the bound state so that it can be > easily transformed > on each call. Modifying execution state can also be achieved by maintaining > a gerund as part of > the state, and so self modifying verbs are also possible. Another variation > (not implemented > yet, but ver easy) is to memoize the results allowing a background process > to "prefetch" lazy > results. > > To summarize the difference from the yield approach, simpler verbs return > what yield would have. > Optional simple verb maintains state between calls. The coroutine dies when > the state turns > null. Importantly, It allows reusing many verbs in a way that the verb can > ignore the existance > or absense of a coroutine. > > Here's some more simple examples, > > first, another variation on the execution pattern for speed and descriptive > brevity. > > lazyd=: 3 : 0 > NB. variation that uses dyad transform where x argument is (function STATE) > if. 0=#STATE do. return. end. > (STATE=: r transform STATE) ] r=.function STATE > ) > >:@+: (^:10) 0 > 1023 > series2yp1 =: >:@+: Coroutine 0 > lazy__series2yp1 ^: 10 '' > 1023 > lazy__series2yp1"0 i.2 > 2047 4095 > > fibonacci =: +/`([,}:@:]) Coroutine 1 1 > lazyd__fibonacci"0 i.10 > 2 3 5 8 13 21 34 55 89 144 > lazyd__fibonacci each 5$0 > +---+---+---+---+----+ > |233|377|610|987|1597| > +---+---+---+---+----+ > > > ----- Original Message ---- > From: Oleg Kobchenko <[EMAIL PROTECTED]> > To: Programming forum <[email protected]> > Sent: Saturday, October 28, 2006 3:13:18 AM > Subject: Re: [Jprogramming] Neat coroutine implementation > > 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 > > > > > > ---------------------------------------------------------------------- > For information about J forums see http://www.jsoftware.com/forums.htm > ____________________________________________________________________________________ Everyone is raving about the all-new Yahoo! Mail (http://advision.webevents.yahoo.com/mailbeta/) ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
