Skip to content

Instantly share code, notes, and snippets.

@weskerfoot
Created December 20, 2013 17:44
Show Gist options
  • Save weskerfoot/8058553 to your computer and use it in GitHub Desktop.
Save weskerfoot/8058553 to your computer and use it in GitHub Desktop.
import System.Directory
import Control.Monad
import qualified Data.List as L
import Data.List.Split
import Control.Applicative
import qualified Data.Foldable as F
import Data.Monoid
import qualified Data.Text as T
data Directory a = Dir {
subDirectories :: [IO (Directory a)],
fileName :: a} |
File {terminalFile :: a }
isTerminal dir =
let splitted = splitOn "/" dir
l = last splitted
in l == ".." || l == "."
concatPath root dir = root++"/"++dir
searchRaw (Dir [] root) = do
contents' <- getDirectoryContents root
let contents = map (concatPath root) contents'
fs <- filterM doesFileExist contents
ds' <- filterM doesDirectoryExist contents
let ds = filter (not . isTerminal) ds'
case ds of
[] -> return $ Dir (map (return . File) fs) root
_ -> do
let ds' = map (Dir []) ds
let ds'' = map searchRaw ds'
return $ Dir ds'' root
searchRaw _ = undefined
smartSearch root =
let s = splitOn "/" root
s' = filter (not.null) s
s'' = "/" ++ L.intercalate "/" s'
in (searchRaw (Dir [] s''))
dirmap :: (a -> b) -> Directory a -> Directory b
dirmap f (File fn) = File $ f fn
dirmap f (Dir fs fp) = Dir ((map (fmap (dirmap f))) fs) (f fp)
dirfold :: (IO b -> t -> IO b) -> IO b -> Directory t -> IO b
dirfold f acc (File fp) = f acc fp
dirfold f acc (Dir fs fp) = do
fs' <- sequence fs
foldl (dirfold f) (f acc fp) fs'
instance Functor Directory where
fmap = dirmap
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment