Last active
February 25, 2022 17:05
-
-
Save srghma/7e78adb6dc6c9b409f71d04067d97e86 to your computer and use it in GitHub Desktop.
purescript-coroutines
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- | 'guy state (start): { state: 0 }' | |
-- | 'appleTree state (start): { state: 0 }' | |
-- | 'appleTree state (1 put): { state: 1 }' | |
-- | 'guy awaitInput (1 await): { output: 1 }' | |
-- | 'guy state (1 get): { state: 1 }' | |
-- | 'appleTree state (1 emit): { state: 1 }' | |
-- | 'appleTree state (2 put): { state: 2 }' | |
-- | 'guy awaitInput (2 await): { output: 2 }' | |
-- | 'guy state (2 get): { state: 2 }' | |
-- | '(Tuple unit { state: 2 })' | |
appleTree :: Co.Producer { output :: Int } (State { state :: Int }) Unit | |
-- | appleTree = Co.producer $ pure $ Right unit | |
appleTree = do | |
get >>= \state -> traceM $ "appleTree state (start): " <> show state | |
put { state: 1 } | |
get >>= \state -> traceM $ "appleTree state (1 put): " <> show state | |
Co.emit { output: 1 } | |
get >>= \state -> traceM $ "appleTree state (1 emit): " <> show state | |
put { state: 2 } | |
get >>= \state -> traceM $ "appleTree state (2 put): " <> show state | |
Co.emit { output: 2 } | |
get >>= \state -> traceM $ "appleTree state (2 emit): " <> show state | |
guy :: Co.Consumer { output :: Int } (State { state :: Int }) Unit | |
-- | guy = Co.consumer \i -> do | |
-- | traceM i | |
-- | pure $ Just unit | |
guy = do | |
get >>= \state -> traceM $ "guy state (start): " <> show state | |
Co.await >>= \input -> traceM $ "guy awaitInput (1 await): " <> show input | |
get >>= \state -> traceM $ "guy state (1 get): " <> show state | |
Co.await >>= \input -> traceM $ "guy awaitInput (2 await): " <> show input | |
get >>= \state -> traceM $ "guy state (2 get): " <> show state | |
main :: Effect Unit | |
main = void $ launchAff do | |
let | |
x :: Co.Process (State { state :: Int }) Unit | |
x = pullFrom guy appleTree | |
x' :: State { state :: Int } Unit | |
x' = Co.runProcess x | |
x'' :: Tuple Unit { state :: Int } | |
x'' = runState x' { state: 0 } | |
traceM $ show x'' | |
pure unit | |
-- | Co.runProcess (showing `Co.fuseCoTransform` coshowing) | |
-- | Co.runProcess ((nats $~ showing) $$ printer) | |
-- | Co.runProcess (nats /\ nats $$ showing ~$ printer) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- | 'appleTree state (start)' | |
-- | 'guy state (start)' | |
-- | 'appleTree state (after 1 emit)' | |
-- | 'guy awaitInput (1 await): { output: 1 }' | |
-- | 'guy state (after 1 await)' | |
-- | 'appleTree state (after 2 emit)' | |
-- | 'guy awaitInput (2 await): { output: 2 }' | |
-- | 'guy state (after 2 await)' | |
-- | 'unit' | |
main :: Effect Unit | |
main = void $ launchAff do | |
-- StateT cannot be Parallel | |
-- | state <- AVar.new 0 | |
let | |
appleTree :: Co.Producer { output :: Int } Aff Unit | |
-- | appleTree = Co.producer $ pure $ Right unit | |
appleTree = do | |
traceM "appleTree state (start)" | |
Co.emit { output: 1 } | |
traceM "appleTree state (after 1 emit)" | |
Co.emit { output: 2 } | |
traceM "appleTree state (after 2 emit)" | |
guy :: Co.Consumer { output :: Int } Aff Unit | |
-- | guy = Co.consumer \i -> do | |
-- | traceM i | |
-- | pure $ Just unit | |
guy = do | |
traceM "guy state (start)" | |
Co.await >>= \input -> traceM $ "guy awaitInput (1 await): " <> show input | |
traceM "guy state (after 1 await)" | |
Co.await >>= \input -> traceM $ "guy awaitInput (2 await): " <> show input | |
traceM "guy state (after 2 await)" | |
x :: Co.Process Aff Unit | |
x = connect appleTree guy | |
x' :: Aff Unit | |
x' = Co.runProcess x | |
x'' <- x' | |
traceM $ show x'' | |
pure unit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- | 'guy state (start)' | |
-- | 'appleTree state (start)' | |
-- | 'guy awaitInput (1 await): { output: 1 }' | |
-- | 'guy state (after 1 await)' | |
-- | 'appleTree state (after 1 emit)' | |
-- | 'guy awaitInput (2 await): { output: 2 }' | |
-- | 'guy state (after 2 await)' | |
-- | 'unit' | |
main :: Effect Unit | |
main = void $ launchAff do | |
-- StateT cannot be Parallel | |
-- | state <- AVar.new 0 | |
let | |
appleTree :: Co.Producer { output :: Int } Aff Unit | |
-- | appleTree = Co.producer $ pure $ Right unit | |
appleTree = do | |
traceM "appleTree state (start)" | |
Co.emit { output: 1 } | |
traceM "appleTree state (after 1 emit)" | |
Co.emit { output: 2 } | |
traceM "appleTree state (after 2 emit)" | |
guy :: Co.Consumer { output :: Int } Aff Unit | |
-- | guy = Co.consumer \i -> do | |
-- | traceM i | |
-- | pure $ Just unit | |
guy = do | |
traceM "guy state (start)" | |
Co.await >>= \input -> traceM $ "guy awaitInput (1 await): " <> show input | |
traceM "guy state (after 1 await)" | |
Co.await >>= \input -> traceM $ "guy awaitInput (2 await): " <> show input | |
traceM "guy state (after 2 await)" | |
x :: Co.Process Aff Unit | |
x = pullFrom guy appleTree | |
x' :: Aff Unit | |
x' = Co.runProcess x | |
x'' <- x' | |
traceM $ show x'' | |
pure unit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment