unliftio-0.2.25.0: The MonadUnliftIO typeclass for unlifting monads to IO (batteries included)

Safe Haskell | None
Language | Haskell2010

UnliftIO.Memoize

Description

Memoize the results of actions. In other words: actions will be run once, on demand, and their results saved.

Exceptions semantics: if a synchronous exception is thrown while performing the computation, that result will be saved and rethrown each time `runMemoized` is called subsequently.'

Since: 0.2.8.0

Synopsis

- data Memoized a
- runMemoized :: MonadIO m => Memoized a -> m a
- memoizeRef :: MonadUnliftIO m => m a -> m (Memoized a)
- memoizeMVar :: MonadUnliftIO m => m a -> m (Memoized a)

# Documentation

data Memoized a #

A "run once" value, with results saved. Extract the value with `runMemoized`. For single-threaded usage, you can use `memoizeRef` to create a value. If you need guarantees that only one thread will run the action at a time, use `memoizeMVar`.

Note that this type provides a `Show` instance for convenience, but not useful information can be provided.

Since: 0.2.8.0

#### Instances

Instances details

Applicative Memoized # |
Instance details Defined in UnliftIO.Memoize Methods pure :: a -> Memoized a # (<*>) :: Memoized (a -> b) -> Memoized a -> Memoized b # liftA2 :: (a -> b -> c) -> Memoized a -> Memoized b -> Memoized c # (*>) :: Memoized a -> Memoized b -> Memoized b # (<*) :: Memoized a -> Memoized b -> Memoized a #
Functor Memoized # |
Instance details Defined in UnliftIO.Memoize Methods fmap :: (a -> b) -> Memoized a -> Memoized b # (<$) :: a -> Memoized b -> Memoized a #
Monad Memoized # |
Instance details Defined in UnliftIO.Memoize Methods (>>=) :: Memoized a -> (a -> Memoized b) -> Memoized b # (>>) :: Memoized a -> Memoized b -> Memoized b # return :: a -> Memoized a #
Show (Memoized a) # |
Instance details Defined in UnliftIO.Memoize Methods showsPrec :: Int -> Memoized a -> ShowS # show :: Memoized a -> String # showList :: [Memoized a] -> ShowS #

runMemoized :: MonadIO m => Memoized a -> m a #

Extract a value from a `Memoized`, running an action if no cached value is available.

Since: 0.2.8.0

memoizeRef :: MonadUnliftIO m => m a -> m (Memoized a) #

Create a new `Memoized` value using an `IORef` under the surface. Note that the action may be run in multiple threads simultaneously, so this may not be thread safe (depending on the underlying action). Consider using `memoizeMVar`.

Since: 0.2.8.0

memoizeMVar :: MonadUnliftIO m => m a -> m (Memoized a) #

Same as `memoizeRef`, but uses an `MVar` to ensure that an action is only run once, even in a multithreaded application.

Since: 0.2.8.0