/polysemy-pool

Primary LanguageHaskellMIT LicenseMIT

Read me

What is this?

polysemy-pool is a small library implementing a resource pool effect to be used with polysemy.

How do I use this?

Here’s a short example:

foo = do
  res <- acquire
  bar res
  release res

Since a pool has usually a limited amount of resources, ‘acquireMaybe’ can be used to avoid stalling:

foo = do
  mres <- acquireMaybe
  case mres of
    Just res -> bar res >> release res
    Nothing -> embed (putStrLn "Ooops! We ran out of resources!")

The two examples above are useful, but are prone to bugs if your code isn’t pure and may run into exceptions. In this case, the right course of action would be to use ‘withResourceIO’ or ‘withResourceFinalIO’:

foo = withResourceIO $ \res ->
  embed $ putStrLn "Bzzz bzzzz"
  bar res
  embed $ putStrLn "All done!"

Or…

foo = withResourceFinalIO $ \res ->
  embedFinal $ putStrLn "Bzzz bzzzz"
  bar res
  embedFinal $ putStrLn "All done!"

These functions are implemented in terms of ‘Resource’ effect from Polysemy.