Skip to content

Instantly share code, notes, and snippets.

@yasar11732
Last active December 17, 2019 17:33
Show Gist options
  • Save yasar11732/35caeaad2ce4b2bd2cc396cec30ae698 to your computer and use it in GitHub Desktop.
Save yasar11732/35caeaad2ce4b2bd2cc396cec30ae698 to your computer and use it in GitHub Desktop.
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