uglymemo-0.1.0.1/0000755000000000000000000000000011642626636011707 5ustar0000000000000000uglymemo-0.1.0.1/uglymemo.cabal0000644000000000000000000000111011642626636014522 0ustar0000000000000000Name: uglymemo Version: 0.1.0.1 Synopsis: A simple (but internally ugly) memoization function. Description: A simple (but internally ugly) memoization function. . * New in 0.1.0.1: Make it exception safe. 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 uglymemo-0.1.0.1/Setup.hs0000644000000000000000000000005611642626636013344 0ustar0000000000000000import Distribution.Simple main = defaultMain uglymemo-0.1.0.1/Data/0000755000000000000000000000000011642626636012560 5ustar0000000000000000uglymemo-0.1.0.1/Data/MemoUgly.hs0000644000000000000000000000146511642626636014660 0ustar0000000000000000module 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 }; modifyMVar_ v (return . M.insert x r); 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)