-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest2.hs
28 lines (21 loc) · 911 Bytes
/
test2.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
-- testing file handles and typeclasses
import Data.Char
import System.IO
class HandleWrapper f where
showFile :: f -> IO String
matchesFile :: f -> IO Bool
data TextFileWrapper = WrappedTHandle (IO Handle)
data BinaryFileWrapper = WrappedBHandle (IO Handle)
instance HandleWrapper TextFileWrapper where
showFile (WrappedTHandle h) = h >>= hGetContents
matchesFile (WrappedTHandle h) = h >>= hGetChar >>= (return . isPrint)
instance HandleWrapper BinaryFileWrapper where
showFile (WrappedBHandle _) = (return "Not printable")
matchesFile (WrappedBHandle h) = h >>= hGetChar >>= (return . not . isPrint)
wrapHandleFromList :: HandleWrapper w => [w] -> IO Handle -> [w]
wrapHandleFromList wrapperList h = filter matchesFile (map (\w -> w h) wrapperList)
testTFH = WrappedBHandle $ openFile "b" ReadMode
main :: IO ()
main = do
print =<< (matchesFile testTFH)
putStrLn =<< (showFile testTFH)