Skip to content

Instantly share code, notes, and snippets.

@bitemyapp
Created October 26, 2015 05:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bitemyapp/2311fcbfa152ce0980ed to your computer and use it in GitHub Desktop.
Save bitemyapp/2311fcbfa152ce0980ed to your computer and use it in GitHub Desktop.
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