fcache-0.1.0.0: Cache a function (a -> b)

Maintainerylilarry@gmail.com
StabilityExperimental
PortabilityGHC
Safe HaskellSafe
LanguageHaskell2010

Cache.State

Description

This module has everything you need to cache a function (a -> b)

   let cache = newCache Nothing :: Cache Int Int
   let f = do
      withCache doSomethingSlow 3 -- Slow; the result is cached in a map using 3 as the key.
      withCache doSomethingSlow 3 -- Read from the cache.
      withCache doSomethingSlow 3 :: SCache Int Int
   evalState f cache 

Documentation

data Cache k v Source

Instances

(Hashable k, Eq k) => IsCache Cache k Source 
(Eq k, Eq v) => Eq (Cache k v) Source 
(Show k, Show v) => Show (Cache k v) Source 

type SCacheT k v n = StateT (Cache k v) n v Source

type SCache k v = State (Cache k v) v Source

class (IsCache m k, Monad n) => IsSCacheT m k n where Source

Minimal complete definition

Nothing

Methods

withCache :: (k -> n v) -> k -> StateT (m k v) n v Source

withCache action x, "x" is an argument for the action.

If "x" is a key in the cache, ignore the "action" and return the cached value;

Otherwise perform the action and cache the result using "x" as a key.

Instances

(IsCache m k, Monad n) => IsSCacheT m k n Source