Created
August 11, 2021 08:26
-
-
Save kuribas/6d63a4b9be2ac210162b4c9ee2ab10ca to your computer and use it in GitHub Desktop.
memoize
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
memoize2 :: (Eq a, Hashable a) => (a -> IO b) -> IO (a -> b) | |
memoize2 f = do | |
ref <- newIORef HashMap.empty | |
pure $ \x -> | |
do mp <- readIORef ref | |
case HashMap.lookup x mp of | |
Nothing -> unsafePerformIO $ do | |
res <- f x | |
writeIORef ref $ HashMap.insert x res mp | |
pure res | |
Just res -> pure res | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment