Skip to content

Instantly share code, notes, and snippets.

@chadaustin
Created January 1, 2015 07:24
Show Gist options
  • Save chadaustin/f98dca810d64f32ad65f to your computer and use it in GitHub Desktop.
Save chadaustin/f98dca810d64f32ad65f to your computer and use it in GitHub Desktop.
walk :: FilePath -> ((FilePath, [FilePath], [FilePath]) -> IO ()) -> IO ()
walk root action = do
entries <- Directory.getDirectoryContents root
directories <- newIORef []
files <- newIORef []
forM_ entries $ \entry -> do
let fullPath = combine root entry
isFile <- Directory.doesFileExist fullPath
if isFile then
modifyIORef files (entry:)
else do
when (entry /= "." && entry /= "..") $ do
isDirectory <- Directory.doesDirectoryExist fullPath
when isDirectory $ modifyIORef directories (entry:)
ds <- readIORef directories
fs <- readIORef files
action (root, sort ds, sort fs)
forM_ ds $ \dir ->
walk (combine root dir) action
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment