Copyright | © 2021 Evan Relf |
---|---|
License | BSD-3-Clause |
Maintainer | evan@evanrelf.com |
Stability | experimental |
Safe Haskell | None |
Language | Haskell2010 |
Drama.Internal
Description
Synopsis
- newtype Actor msg a = Actor (ReaderT (ActorEnv msg) IO a)
- runActor :: MonadIO m => ActorEnv msg -> Actor msg a -> m a
- data ActorEnv msg = ActorEnv {}
- newtype Address msg = Address (InChan msg)
- newtype Mailbox msg = Mailbox (OutChan msg)
- newtype Scope = Scope Scope
- spawn :: Actor childMsg () -> Actor msg (Address childMsg)
- wait :: Actor msg ()
- here :: Actor msg (Address msg)
- send :: Address recipientMsg -> recipientMsg -> Actor msg ()
- receive :: Actor msg msg
- tryReceive :: Actor msg (Maybe msg)
- run :: MonadIO m => Actor msg a -> m a
- loop :: s -> (s -> Actor msg (Either s a)) -> Actor msg a
- continue :: s -> Actor msg (Either s a)
- exit :: a -> Actor msg (Either s a)
Documentation
Instances
Monad (Actor msg) Source # | |
Functor (Actor msg) Source # | |
MonadFix (Actor msg) Source # | |
Defined in Drama.Internal | |
MonadFail (Actor msg) Source # | |
Defined in Drama.Internal | |
Applicative (Actor msg) Source # | |
MonadIO (Actor msg) Source # | |
Defined in Drama.Internal | |
Alternative (Actor msg) Source # | |
MonadPlus (Actor msg) Source # | |
Environment for the Actor
monad.
Since: 0.1.0.0
Where messages are delivered. Implicitly provided to receive
and
tryReceive
by the Actor
monad.
Since: 0.1.0.0
spawn :: Actor childMsg () -> Actor msg (Address childMsg) Source #
Spawn a new actor. Returns the spawned actor's address.
Example:
printerAddress <- spawn printer
Since: 0.1.0.0
Wait for all actors spawned by the current actor to terminate.
Example:
fooAddress <- spawn foo barAddress <- spawn bar wait
Since: 0.1.0.0
here :: Actor msg (Address msg) Source #
Return the current actor's own address. Useful for sending your address to other actors, or for sending yourself a message.
Since: 0.1.0.0
send :: Address recipientMsg -> recipientMsg -> Actor msg () Source #
Given an actor's address, send it a message.
Example:
send printerAddress "Hello, world!"
Since: 0.1.0.0
receive :: Actor msg msg Source #
Receive a message sent to the actor's mailbox. This function blocks until a message is received.
Example:
printer :: Actor String () printer = forever do string <- receive liftIO $ putStrLn string
Since: 0.1.0.0
tryReceive :: Actor msg (Maybe msg) Source #
Receive a message sent to the actor's mailbox. This function blocks until a message is received.
Example:
printer :: Actor String () printer = forever do tryReceive >>= \case Just string -> liftIO $ putStrLn string Nothing -> ...
Since: 0.1.0.0
run :: MonadIO m => Actor msg a -> m a Source #
Run a top-level actor. Intended to be used at the entry point of your program.
Since: 0.1.0.0
Arguments
:: s | Initial state |
-> (s -> Actor msg (Either s a)) | Action to perform, either returning a new state to continue looping, or a final value to stop looping. |
-> Actor msg a |
Loop indefinitely with state. Use forever
for stateless
infinite loops.
Example:
counter :: Actor () Int counter = loop 10 \count -> do liftIO $ print count if count > 0 then continue (count - 1) else exit count
Since: 0.1.0.0