monad-batcher-0.0.0.0: An applicative monad that batches commands for later more efficient execution

Safe HaskellSafe
LanguageHaskell2010

Control.Monad.Batcher

Synopsis

Documentation

data Batcher command m a Source #

An applicative monad that schedules commands for later more efficient execution.

Instances

Monad (Batcher command m) Source # 

Methods

(>>=) :: Batcher command m a -> (a -> Batcher command m b) -> Batcher command m b #

(>>) :: Batcher command m a -> Batcher command m b -> Batcher command m b #

return :: a -> Batcher command m a #

fail :: String -> Batcher command m a #

Functor (Batcher command m) Source # 

Methods

fmap :: (a -> b) -> Batcher command m a -> Batcher command m b #

(<$) :: a -> Batcher command m b -> Batcher command m a #

Applicative (Batcher command m) Source #

Composing two Batcher computations using the applicative combinators ensures that commands scheduled in both computations are given to the Worker in one batch.

Methods

pure :: a -> Batcher command m a #

(<*>) :: Batcher command m (a -> b) -> Batcher command m a -> Batcher command m b #

(*>) :: Batcher command m a -> Batcher command m b -> Batcher command m b #

(<*) :: Batcher command m a -> Batcher command m b -> Batcher command m a #

runBatcher Source #

Arguments

:: MonadIO m 
=> Worker command m

Specfies how to batch and execute commands.

-> Batcher command m a

The computation to run.

-> m a 

Execute a Batcher computation using the given Worker.

type Worker command m = [Scheduled command m] -> m () Source #

A Worker is responsible for executing the given batch of scheduled commands. Instead of executing each command individually it might group commands and execute each group in one go. It might also execute each command concurrently.

The Worker should ensure that the result of each command is written using writeResult.

data Scheduled command m Source #

A scheduled command paired with a function to communicate its result.

Note that the result of the command is existentially quantified. This ensures that multiple commands with different result types can be batched in a list which can then be given to a Worker for execution.

Constructors

Scheduled 

Fields

simpleWorker Source #

Arguments

:: MonadCatch m 
=> (forall r. command r -> m r)

Specifies how to execute a command.

-> Worker command m 

A convenience Worker that simply executes commands using the given function without any batching.

schedule Source #

Arguments

:: (MonadIO m, MonadThrow m) 
=> command a

The command to schedule.

-> Batcher command m a 

Schedule a command for later execution.

catchBatcher Source #

Arguments

:: (MonadCatch m, Exception e) 
=> Batcher command m a

The computation to run

-> (e -> Batcher command m a)

Handler to invoke if an exception is raised

-> Batcher command m a 

Catch and handle exceptions.