Odd word problem/SimpleCoroutineSupportForJ: Difference between revisions

bug 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 corutinecoroutine context
coroutine=: 1 :0
stack=. ''
verb=. u
noun=. y
while. do. context=. verb noun
context=. verb noun
select. (0 {:: context) * 1+*#stack
case. 0 do. NB. yield
stack=. stack, 21 { context
verb=. (32 { context)`:0
noun=. 13 {:: context
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; y; u ` v,< y
)
 
NB. return y return. NB. 1 -- immediate result: y
return=: 3 :0
1; y
6,962

edits