howManyHandles :: IO ()
howManyHandles = runResourceT @IO do
hs <- openManyHandles
liftIO $ putStrLn ("Opened " <> show (length hs) <> " handles")
openManyHandles :: ResourceT IO [Handle]
openManyHandles = do
let openManyHandles_ xs =
do
x <- fileResourceMaybe
case x of
Just x' -> openManyHandles_ (x' : xs)
Nothing -> return xs
openManyHandles_ []
fileResourceMaybe :: ResourceT IO (Maybe Handle)
fileResourceMaybe = do
dir <- liftIO getDataDir
result <- Ex.tryIO (fileResource (dir </> "b") WriteMode <&> snd <&> Just)
case result of
Right x -> return x
Left e -> do
liftIO $ print (displayException e)
return Nothing
main :: IO ()
main = howManyHandles