Created
October 20, 2015 23:14
-
-
Save FranklinChen/133cb61af931a08bbe20 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
-- | http://stackoverflow.com/questions/14259229/streaming-recursive-descent-of-a-directory-in-haskell/14261710#14261710 | |
-- | |
-- Updated to latest Pipes 4. | |
module Main where | |
import Pipes | |
import qualified Pipes.Prelude as P | |
import Control.Monad (forM_) | |
import System.Directory (doesDirectoryExist, getDirectoryContents) | |
import System.Environment (getArgs) | |
import System.FilePath ((</>)) | |
getRecursiveContents :: FilePath -> Producer FilePath IO () | |
getRecursiveContents topPath = do | |
names <- lift $ getDirectoryContents topPath | |
let properNames = filter (`notElem` [".", ".."]) names | |
forM_ properNames $ \name -> do | |
let path = topPath </> name | |
isDirectory <- lift $ doesDirectoryExist path | |
if isDirectory | |
then getRecursiveContents path | |
else yield path | |
main :: IO () | |
main = do | |
[path] <- getArgs | |
runEffect $ | |
getRecursiveContents path | |
>-> P.map ("Found file " ++) | |
>-> P.stdoutLn |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment