Update demos and adds <|
This commit is contained in:
33
demos/interactionTrees/yield.tri
Normal file
33
demos/interactionTrees/yield.tri
Normal file
@@ -0,0 +1,33 @@
|
||||
!import "../../lib/base.tri" !Local
|
||||
!import "../../lib/list.tri" !Local
|
||||
!import "../../lib/io.tri" !Local
|
||||
|
||||
-- Cooperative scheduling with yield.
|
||||
-- yield returns control to the scheduler so other tasks can run.
|
||||
--
|
||||
-- Two tasks print alternately because each yields after every line.
|
||||
|
||||
--chatter = (name n :
|
||||
-- bind (putStrLn (append name " says 1")) (_ :
|
||||
-- bind yield (_ :
|
||||
-- bind (putStrLn (append name " says 2")) (_ :
|
||||
-- bind yield (_ :
|
||||
-- bind (putStrLn (append name " says 3")) (_ :
|
||||
-- pure n))))))
|
||||
|
||||
chatter = name n : bind <|
|
||||
putStrLn (append name " says 1") (_ :
|
||||
bind yield (_ :
|
||||
bind (putStrLn (append name " says 2")) (_ :
|
||||
bind yield (_ :
|
||||
bind (putStrLn (append name " says 3")) (_ :
|
||||
pure n)))))
|
||||
|
||||
|
||||
main = io <|
|
||||
bind (fork (chatter "A" "doneA")) (ha :
|
||||
bind (fork (chatter "B" "doneB")) (hb :
|
||||
bind yield (_ :
|
||||
bind (await ha) (a :
|
||||
bind (await hb) (b :
|
||||
putStrLn (append "Finished: " (append a (append " " b))))))))
|
||||
Reference in New Issue
Block a user