module System.IO.Read
( readFrom
, readIn
, readDirectory
, listAbsDirectory
, listFromIndexedMap
, indexedMap
, indexed
) where
import System.Directory (canonicalizePath, listDirectory)
import System.FilePath ((</>))
import System.IO (openFile, IOMode(ReadMode), hSetEncoding, hGetContents, utf8)
import Data.List (sort)
import Data.Map (Map, fromList, toList)
readFileUtf8 :: FilePath -> IO String
readFileUtf8 path = do
handle <- openFile path ReadMode
hSetEncoding handle utf8
hGetContents handle
readFrom :: (Read a) => FilePath -> IO a
readFrom path =
readIO =<< readFileUtf8 =<< canonicalizePath path
readIn :: (Read a) => FilePath -> FilePath -> IO [a]
readIn basedir subpath =
readDirectory (basedir </> subpath)
readDirectory :: (Read a) => FilePath -> IO [a]
readDirectory dir =
mapM readFrom =<< listAbsDirectory dir
listAbsDirectory :: FilePath -> IO [FilePath]
listAbsDirectory dir =
sort <$> listDirectory dir >>= mapM (canonicalizePath . (dir </>))
listFromIndexedMap :: Read c => (b1 -> b2 -> IO FilePath) -> Map (a1, a2) (b1, b2) -> IO [((a1, a2), c)]
listFromIndexedMap f index =
sequence [ uncurry2 ((a1, b1), readIO =<< readFileUtf8 =<< f a2 b2) | ((a1, b1), (a2, b2)) <- toList index ]
uncurry2 :: Functor f => (a1, f a) -> f (a1, a)
uncurry2 =
uncurry $ fmap . (,)
indexedMap :: (Ord t1, Ord t, Num t1, Num t, Enum t1, Enum t) => [t2] -> [t3] -> Map (t, t1) (t2, t3)
indexedMap a b =
pairedMap (indexed a) (indexed b)
indexed :: (Num a, Enum a) => [b] -> [(a, b)]
indexed =
zip [0..]
pairedMap :: (Ord t1, Ord t) => [(t, t2)] -> [(t1, t3)] -> Map (t, t1) (t2, t3)
pairedMap a b =
fromList $ pairedCartProd a b
pairedCartProd :: [(t, t2)] -> [(t1, t3)] -> [((t, t1), (t2, t3))]
pairedCartProd a b =
[((a1, b1), (a2, b2)) | (a1, a2) <- a, (b1, b2) <- b]