Last active
December 31, 2015 19:49
-
-
Save kseo/8035718 to your computer and use it in GitHub Desktop.
find written in Haskell
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
-- From A tutorial on the enumerator library | |
-- http://www.mew.org/~kazu/proj/enumerator/ | |
import Control.Applicative | |
import Control.Monad | |
import Control.Monad.IO.Class | |
import Data.Enumerator hiding (map, filter, filterM) | |
import qualified Data.Enumerator.List as EL | |
import Data.List | |
import System.Directory | |
import System.FilePath | |
getValidContents :: FilePath -> IO [FilePath] | |
getValidContents path = | |
filter (`notElem` [".", "..", ".git", ".svn"]) | |
<$> getDirectoryContents path | |
isSearchableDir :: FilePath -> IO Bool | |
isSearchableDir dir = | |
(&&) <$> doesDirectoryExist dir | |
<*> (searchable <$> getPermissions dir) | |
grepE :: String -> Enumeratee String String IO b | |
grepE pattern = EL.filter (pattern `isInfixOf`) | |
printI :: Iteratee String IO () | |
printI = do | |
mx <- EL.head | |
case mx of | |
Nothing -> return () | |
Just file -> do | |
liftIO . putStrLn $ file | |
printI | |
enumDir :: FilePath -> Enumerator String IO b | |
enumDir dir = list | |
where | |
list (Continue k) = do | |
(files,dirs) <- liftIO getFilesDirs | |
if null dirs | |
then k (Chunks files) | |
else k (Chunks files) >>== walk dirs | |
list step = returnI step | |
walk dirs = foldr1 (<==<) $ map enumDir dirs | |
getFilesDirs = do | |
cnts <- map (dir </>) <$> getValidContents dir | |
(,) <$> filterM doesFileExist cnts | |
<*> filterM isSearchableDir cnts | |
findEnum :: FilePath -> String -> IO () | |
findEnum dir pattern = run_ $ enumDir dir | |
$$ grepE pattern | |
=$ printI |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment