Last active
December 17, 2019 17:33
-
-
Save yasar11732/35caeaad2ce4b2bd2cc396cec30ae698 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 RecursiveContents (getRecursiveContents) | |
where | |
import Control.Monad (forM) | |
import Control.Applicative (liftA2) | |
import System.Directory (doesDirectoryExist, listDirectory) | |
import System.FilePath ((</>)) | |
myFilterM :: (Applicative m) => (a -> m Bool) -> [a] -> m ([a],[a]) | |
myFilterM pred = foldr myFilterM' (pure ([],[])) | |
where myFilterM' x = liftA2 ( | |
\flg -> if flg then | |
\(taken,dropped) -> (x:taken,dropped) | |
else | |
\(taken,dropped) -> (taken,x:dropped) | |
) (pred x) | |
getRecursiveContents :: FilePath -> -- path to root directory | |
IO [FilePath] -- list of found paths | |
getRecursiveContents root = do | |
isDir <- doesDirectoryExist root | |
if isDir then do | |
contents <- listDirectory root | |
seperated <- myFilterM doesDirectoryExist contents | |
let dirs = fst seperated | |
others = snd seperated | |
paths <- forM dirs $ \name -> do | |
let path = root </> name | |
getRecursiveContents path | |
return $ [root </> x | x <- others] ++ concat paths | |
else | |
return [] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment