uglymemo (empty) → 0.1.0.0
raw patch · 3 files changed
+41/−0 lines, 3 filesdep +basedep +containerssetup-changed
Dependencies added: base, containers
Files
- Data/MemoUgly.hs +24/−0
- Setup.hs +2/−0
- uglymemo.cabal +15/−0
+ Data/MemoUgly.hs view
@@ -0,0 +1,24 @@+module Data.MemoUgly(memoIO, memo) where+import Control.Concurrent.MVar+import qualified Data.Map as M+import System.IO.Unsafe(unsafePerformIO)++-- | Memoize the given function by allocating a memo table,+-- and then updating the memo table on each function call.+memoIO :: (Ord a)+ => (a -> b) -- ^Function to memoize+ -> IO (a -> IO b)+memoIO f = do+ v <- newMVar M.empty+ let f' x = do+ m <- readMVar v+ case M.lookup x m of+ Nothing -> do let { r = f x }; m <- takeMVar v; putMVar v (M.insert x r m); return r+ Just r -> return r+ return f'++-- | The pure version of 'memoIO'.+memo :: (Ord a)+ => (a -> b) -- ^Function to memoize+ -> (a -> b)+memo f = let f' = unsafePerformIO (memoIO f) in \ x -> unsafePerformIO (f' x)
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ uglymemo.cabal view
@@ -0,0 +1,15 @@+Name: uglymemo+Version: 0.1.0.0+Synopsis: A simple (but internally ugly) memoization function.+Description: A simple (but internally ugly) memoization function.+License: PublicDomain+Author: Lennart Augustsson+Maintainer: lennart@augustsson.net+Category: Data+Build-type: Simple+Cabal-version: >=1.2+++Library+ Exposed-modules: Data.MemoUgly+ Build-depends: base < 10, containers