Odd word problem/SimpleCoroutineSupportForJ: Difference between revisions
Odd word problem/SimpleCoroutineSupportForJ (view source)
Revision as of 15:45, 9 August 2012
, 11 years agobug fix (Aai emailed me both the problem and solution here)
mNo edit summary |
(bug fix (Aai emailed me both the problem and solution here)) |
||
(6 intermediate revisions by the same user not shown) | |||
Line 1:
Here is a simplistic coroutine implementation for [[../#J|J]].
Note that this version does not support switching between coroutine contexts -- that would have been an unnecessary complication for this task. (So, technically, it might be better to call this "coroutine inspired".)
<lang j>NB. u coroutine y NB. execute u in new
coroutine=: 1 :0
stack=. ''
verb=. u
noun=. y
while. do. context=. verb noun
select. (0 {:: context) * 1+*#stack
case. 0 do. NB. yield
stack=. stack,
verb=. (
noun=.
case. 1 do. NB. return (with empty stack)
1 {:: context return.
Line 21 ⟶ 20:
noun=. 1 {:: context
stack=. }: stack
case. do. NB. (default)
invalid coroutine 1 :'error.'
end.
Line 27 ⟶ 26:
)
NB. u yield v y return. NB. 0 -- deferred result, will be executing: u v y
yield=: 2 :0
0
)
NB. return y return. NB. 1 -- immediate result: y
return=: 3 :0
1; y
|