Skip to content

Instantly share code, notes, and snippets.

@kseo
Last active December 31, 2015 19:49
Show Gist options
  • Save kseo/8035718 to your computer and use it in GitHub Desktop.
Save kseo/8035718 to your computer and use it in GitHub Desktop.
find written in Haskell
-- 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