Hello all,

I'm looking at how features written in Python's generator can be implemented 
with Factor.
I found such an example in the mailing list archive.

<https://www.mail-archive.com/factor-talk@lists.sourceforge.net/msg00392.html>
<https://www.mail-archive.com/factor-talk@lists.sourceforge.net/msg00394.html>

>     def gen():
>         for i in xrange(10**5):
>             yield i
>     
>     for i in gen():
>         pass


> "libs/coroutines" require
> USE: coroutines
> : gen ( -- co ) [ 100000 [ over coyield 2drop ] each f swap coyield ] 
> cocreate ;
> : run ( co -- ) f swap coresume [ run ] [ drop ] if ;
> 
> [ gen run ] time


But, this code doesn't seem to work in modern Factor.
So I tried to avoid errors, but it doesn't work as I expected and seems wrong.


USE: coroutines
: gen ( -- co ) [ 100000 <iota> [ over coyield 2drop ] each f swap coyield ] 
cocreate ;
: run ( co -- ) f swap coresume dup [ run ] [ drop ] if ;

IN: scratchpad [ gen run ] time
Running time: 0.000414741 seconds


How should I change that?



--
KUSUMOTO Norio





_______________________________________________
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk

Reply via email to