From 9b58030110a8771fcd966a6482e39568b9bf4dc7 Mon Sep 17 00:00:00 2001 From: Joshua Clayton Date: Sat, 11 Jun 2016 06:10:57 -0400 Subject: [PATCH] Replace custom lift with liftIO --- src/Unused/Cache.hs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/Unused/Cache.hs b/src/Unused/Cache.hs index bfdcc4e..4a2a091 100644 --- a/src/Unused/Cache.hs +++ b/src/Unused/Cache.hs @@ -2,6 +2,7 @@ module Unused.Cache ( cached ) where +import Control.Monad.IO.Class (liftIO) import Control.Monad.Trans.Reader import System.Directory import Data.Csv (FromRecord, ToRecord, HasHeader(..), encode, decode) @@ -13,23 +14,23 @@ cached :: (FromRecord a, ToRecord a) => String -> IO [a] -> IO [a] cached context f = runReaderT fromCache =<< cacheFileName context where - fromCache = maybe (writeCache =<< liftReaderT f) return =<< readCache + fromCache = maybe (writeCache =<< liftIO f) return =<< readCache writeCache :: ToRecord a => [a] -> ReaderT String IO [a] writeCache [] = return [] writeCache contents = do - liftReaderT $ createDirectoryIfMissing True cacheDirectory + liftIO $ createDirectoryIfMissing True cacheDirectory fileName <- ask - liftReaderT $ BS.writeFile fileName $ encode contents + liftIO $ BS.writeFile fileName $ encode contents return contents readCache :: FromRecord a => ReaderT String IO (Maybe [a]) readCache = do fileName <- ask - exists <- liftReaderT $ doesFileExist fileName + exists <- liftIO $ doesFileExist fileName if exists - then fmap processCsv (decode NoHeader <$> liftReaderT (BS.readFile fileName)) + then fmap processCsv (decode NoHeader <$> liftIO (BS.readFile fileName)) else return Nothing where processCsv = either (const Nothing) (Just . toList) @@ -42,6 +43,3 @@ cacheFileName context = do cacheDirectory :: String cacheDirectory = "tmp/unused" - -liftReaderT :: m a -> ReaderT r m a -liftReaderT m = ReaderT $ const m