-
-
Save bitemyapp/2311fcbfa152ce0980ed to your computer and use it in GitHub Desktop.
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
module Main where | |
import System.Directory | |
import Control.Monad | |
import System.IO | |
import System.IO.Error | |
import Data.List | |
import Control.Applicative | |
import Control.Exception.Base | |
import System.Environment | |
listFilesInDir :: FilePath -> IO [FilePath] | |
listFilesInDir dir = | |
getDirectoryContents dir >>= filterM doesFileExist | |
readAndWrite :: Handle -> Handle -> IO () | |
readAndWrite inHandle outHandle = do | |
myLn <- tryIOError (hGetLine inHandle) | |
case myLn of | |
Left e -> if isEOFError e | |
then return () | |
else ioError e | |
Right myLn -> do hPutStrLn outHandle myLn | |
return () | |
firstNLines :: Int -> FilePath -> FilePath -> IO () | |
firstNLines n source sink = do | |
inHandle <- openFile source ReadMode | |
outHandle <- openFile sink WriteMode | |
mapM_ (\_ -> readAndWrite inHandle outHandle) [1..n] | |
-- join . fmap sequence_ $ mapM (\_ -> readAndWrite inHandle outHandle) [1..n] | |
-- one problem: | |
-- Prelude> :t readAndWrite <$> blah <*> woot | |
-- readAndWrite <$> blah <*> woot :: IO (IO ()) | |
-- This is because you didn't bind over them, you put them in the where clause which isn't going to | |
-- bind the operations over the IO. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment