| Copyright | © 2021 Evan Relf |
|---|---|
| License | BSD-3-Clause |
| Maintainer | evan@evanrelf.com |
| Stability | experimental |
| Safe Haskell | None |
| Language | Haskell2010 |
Drama
Description
Simple actor library for Haskell
Synopsis
- data Actor msg a
- spawn :: Message childMsg => Actor childMsg () -> Actor msg (Address childMsg)
- spawn_ :: Actor Void () -> Actor msg ()
- wait :: Actor msg ()
- data Address msg
- here :: Message msg => Actor msg (Address msg)
- send :: Message recipientMsg => Address recipientMsg -> recipientMsg -> Actor msg ()
- receive :: Message msg => Actor msg msg
- tryReceive :: Message msg => Actor msg (Maybe msg)
- 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)
- run :: (Message msg, MonadIO m) => Actor msg a -> m a
- run_ :: MonadIO m => Actor Void a -> m a
- module Drama.Reexports
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 # | |
Spawning actors
spawn :: Message childMsg => 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
Messages
Addresses
here :: Message msg => 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
Sending messages
send :: Message recipientMsg => 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
Receiving messages
receive :: Message msg => 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 :: Message msg => 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
Managing state
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 countSince: 0.1.0.0
continue :: s -> Actor msg (Either s a) Source #
Continue looping with state.
continue s = pure (Left s)
Since: 0.1.0.0
exit :: a -> Actor msg (Either s a) Source #
Exit loop with value.
exit x = pure (Right x)
Since: 0.1.0.0
Running your program
run :: (Message msg, 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
Re-exports
module Drama.Reexports