System.Directory.listDir

This function is what users want 99.9% of time.
This commit is contained in:
Stiopa Koltsov 2021-07-16 20:13:59 +01:00
parent c7629e20fe
commit 4eff6ac916
2 changed files with 19 additions and 10 deletions

View File

@ -118,3 +118,20 @@ dirEntry d = do r <- nextDirEntry d
Left e => Left e
Right (Just n) => Right n
Right Nothing => Left FileNotFound
collectDir : HasIO io => Directory -> io (Either FileError (List String))
collectDir d
= liftIO $ do let (>>=) : (IO . Either e) a -> (a -> (IO . Either e) b) -> (IO . Either e) b
(>>=) = Prelude.(>>=) @{Monad.Compose {m = IO} {t = Either e}}
Just n <- nextDirEntry d
| Nothing => pure $ Right []
ns <- assert_total $ collectDir d
pure $ Right (n :: ns)
export
listDir : HasIO io => String -> io (Either FileError (List String))
listDir name = do Right d <- openDir name
| Left e => pure $ Left e
ns <- collectDir d
ignore <- closeDir d
pure $ ns

View File

@ -5,16 +5,8 @@ panic : String -> IO a
panic s = do putStrLn s
exitFailure
collectEntries : Directory -> IO (List String)
collectEntries d = do Right (Just n) <- nextDirEntry d
| Right Nothing => pure []
| Left e => panic (show e)
ns <- collectEntries d
pure (n :: ns)
main : IO ()
main = do Right d <- openDir "dir"
main = do Right ["a"] <- listDir "dir"
| Left e => panic (show e)
["a"] <- collectEntries d
| x => panic ("wrong entries: " ++ (show x))
| Right x => panic ("wrong entries: " ++ (show x))
pure ()