> 2020/03/04 22:02、KUSUMOTO Norio <kusum...@na.rim.or.jp>のメール:
>>    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.

It seems that another person had the same question as me.
    [Factor-talk] Working with Coroutines?
    
<https://www.mail-archive.com/factor-talk@lists.sourceforge.net/msg03332.html>

Today, I came up with the following code:

    : gen ( -- co )
        [
            drop 100000 <iota> [ coyield* ] each
            f coterminate
        ] cocreate ;

    gen [ dup *coresume dup ] [ drop ] while 2drop


I think this idea is working well. (But to tell the truth, I still don't know 
much about coroutine.)

    IN: scratchpad USE: coroutines
    : gen10 ( -- co )
        [
            drop 10 <iota> [ coyield* ] each
            f coterminate
        ] cocreate ;

    gen10 [ dup *coresume dup ] [ . ] while 2drop

    Loading resource:extra/coroutines/coroutines.factor
    Loading resource:extra/coroutines/coroutines-docs.factor
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    IN: scratchpad 

This code purposely creates a double virtual sequence that doesn't make much 
sense, but it gives me a foothold to understand coroutine.

Sometimes I've come across the explanation that the slow speed of Factor's 
"Continuation" makes it expensive to execution of "Exception Handling" "Escape" 
and "Coroutine".
Is it impossible to make Factor's continuation faster?
For example, do you think Factor's continuation will be faster if continuation 
tuples do not hold `data`, `call`, `retain`, `name`, and `catch` stacks, but VM 
manages multiple areas of a pair and switches between them?

--
KUSUMOTO Norio



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

Reply via email to