]> gitweb.factorcode.org Git - factor.git/commitdiff
Adding coreset to extra/coroutines, plus docs and tests
authorJames Cash <james.nvc@gmail.com>
Fri, 7 Nov 2008 01:01:31 +0000 (20:01 -0500)
committerJames Cash <james.nvc@gmail.com>
Tue, 11 Nov 2008 07:33:18 +0000 (02:33 -0500)
extra/coroutines/coroutines-docs.factor
extra/coroutines/coroutines-tests.factor
extra/coroutines/coroutines.factor

index 327c60e01785c34e48488afccf452c95ba21563f..3b2c5033e0b70d7ea9ff16e1e437864bc40a26b7 100644 (file)
@@ -46,7 +46,13 @@ HELP: coyield*
 HELP: coterminate
 { $values { "v" "an object" } }
 { $description "Terminate the current coroutine, leaving the value v on the stack when control is passed to the " { $link coresume } " caller. Resuming a terminated coroutine is a no-op." }
-{ $see-also coyield }
+{ $see-also coyield coreset }
+;
+
+HELP: coreset
+{ $values { "v" "an object" } }
+{ $description "Reset the current coroutine, leaving the value v on the stack when control is passed to the " { $link coresume } " caller. When the coroutine is resumed, it will continue at the beginning of the coroutine." }
+{ $see-also coyield coterminate }
 ;
 
 HELP: current-coro
index 6710452b228e3533838c951973bcce8775d77262..5c443891cb07e8464e46d81afb6dcf57528f02a0 100644 (file)
@@ -17,3 +17,5 @@ test1 dup *coresume . dup *coresume . dup *coresume . dup *coresume 2drop
   [ [ coyield* ] each ] cocreate ;
 
 { "c" "b" "a" } [ test3 { "a" "b" "c" } over coresume >r dup *coresume >r *coresume r> r> ] unit-test
+
+{ 4+2/3 } [ [ 1+ coyield 2 * coyield 3 / coreset ] cocreate 1 5 [ over coresume ] times nip ] unit-test
\ No newline at end of file
index dc594abd2d5330858f52f918c40dda3758eba5e3..59b703600cb0c0c92c742cb8787cd53d35345a60 100644 (file)
@@ -6,7 +6,7 @@ IN: coroutines
 
 SYMBOL: current-coro
 
-TUPLE: coroutine resumecc exitcc ;
+TUPLE: coroutine resumecc exitcc originalcc ;
 
 : cocreate ( quot -- co )
   coroutine new
@@ -14,7 +14,7 @@ TUPLE: coroutine resumecc exitcc ;
   [ swapd , , \ bind , 
     "Coroutine has terminated illegally." , \ throw ,
   ] [ ] make
-  >>resumecc ;
+  [ >>resumecc ] [ >>originalcc ] bi ;
 
 : coresume ( v co -- result )
   [ 
@@ -43,3 +43,8 @@ TUPLE: coroutine resumecc exitcc ;
   current-coro get
   [ ] >>resumecc
   exitcc>> continue-with ;
+
+: coreset ( v --  )
+  current-coro get dup
+  originalcc>> >>resumecc
+  exitcc>> continue-with ;
\ No newline at end of file