Skip to content

Instantly share code, notes, and snippets.

@lexi-lambda
Created July 21, 2017 23:58
Show Gist options
  • Save lexi-lambda/587eaef572c990a4b59fc3ba6e094c1b to your computer and use it in GitHub Desktop.
Save lexi-lambda/587eaef572c990a4b59fc3ba6e094c1b to your computer and use it in GitHub Desktop.
class Monad m => FileSystemMonad m where
listDirectory' :: String -> m [String]
readFile' :: String -> m String
writeFile' :: String -> String -> m ()
instance FileSystemMonad IO where
listDirectory' = listDirectory
readFile' = readFile
writeFile' = writeFile
instance FileSystemMonad (State [(String, String)]) where
listDirectory' _ = fmap (map fst) get
readFile' path = do
fs <- get
case lookup path fs of
Just contents -> return contents
Nothing -> error "no such file"
writeFile' path contents = do
fs <- get
put ((path, contents) : fs)
copyDirectory :: FileSystemMonad m => String -> String -> m ()
copyDirectory src dest = do
files <- listDirectory' src
forM_ files $ \file ->
copyFile file (dest ++ "/" ++ file)
copyFile :: FileSystemMonad m => String -> String -> m ()
copyFile fileSource fileDestination = do
contents <- readFile' fileSource
writeFile' fileDestination contents
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment