Last active
January 27, 2016 18:10
-
-
Save drewr/bff3921da5b99bc84558 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
module Main where | |
import Prelude | |
import Data.Array | |
import qualified Data.Array.Unsafe as U | |
import Data.Maybe | |
import Data.Foldable | |
import Control.Monad.Eff.Console (log) | |
data Path = Directory String (Array Path) | |
| File String Int | |
instance showPath :: Show Path where | |
show = filename | |
root :: Path | |
root = | |
Directory "/" | |
[ Directory "/bin/" | |
[ File "/bin/cp" 24800 | |
, File "/bin/ls" 34700 | |
, File "/bin/mv" 20200 | |
] | |
, Directory "/etc/" | |
[ File "/etc/hosts" 300 | |
] | |
, Directory "/home/" | |
[ Directory "/home/user/" | |
[ File "/home/user/todo.txt" 1020 | |
, Directory "/home/user/code/" | |
[ Directory "/home/user/code/js/" | |
[ File "/home/user/code/js/test.js" 40000 | |
] | |
, Directory "/home/user/code/haskell/" | |
[ File "/home/user/code/haskell/test.hs" 5000 | |
] | |
] | |
] | |
] | |
] | |
filename :: Path -> String | |
filename (File name _) = name | |
filename (Directory name _) = name | |
isDirectory :: Path -> Boolean | |
isDirectory (Directory _ _) = true | |
isDirectory _ = false | |
ls :: Path -> Array Path | |
ls (Directory _ xs) = xs | |
ls _ = [] | |
size :: Path -> Maybe Int | |
size (File _ bytes) = Just bytes | |
size _ = Nothing | |
isFile :: Path -> Boolean | |
isFile = not isDirectory | |
testFile = U.head $ onlyFiles root | |
testDir = U.head $ drop 2 $ onlyDirs root | |
allPaths :: Path -> Array Path | |
allPaths f = f : concatMap allPaths (ls f) | |
onlyFiles :: Path -> Array Path | |
onlyFiles = filter (not isDirectory) <<< allPaths | |
onlyDirs :: Path -> Array Path | |
onlyDirs = filter isDirectory <<< allPaths | |
match :: String -> Path -> Boolean | |
match s p = filename p == s | |
childFiles :: Path -> Array Path | |
childFiles path = filter isFile (ls path) | |
childFilesWithDir :: Path -> Array (Array Path) | |
childFilesWithDir path = map (\f -> [path, f]) $ childFiles path | |
matchDirFromFilename :: String -> Array Path -> Maybe Path | |
matchDirFromFilename s [d, f] = if match s f then Just d else Nothing | |
whereIs :: String -> Path -> Maybe Path | |
whereIs s path = head <<< | |
catMaybes <<< | |
(map (matchDirFromFilename s)) <<< | |
(map childFilesWithDir) $ onlyDirs path | |
main = log $ whereIs "/etc/hosts" root | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment