-
Notifications
You must be signed in to change notification settings - Fork 103
/
findpar.hs
43 lines (38 loc) · 1.07 KB
/
findpar.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import System.Directory
import Control.Concurrent
import System.FilePath
import Control.Concurrent.Async
import System.Environment
import Data.List hiding (find)
main = do
[s,d] <- getArgs
find s d >>= print
-- <<find
find :: String -> FilePath -> IO (Maybe FilePath)
find s d = do
fs <- getDirectoryContents d
let fs' = sort $ filter (`notElem` [".",".."]) fs
if any (== s) fs'
then return (Just (d </> s))
else do
let ps = map (d </>) fs' -- <1>
foldr (subfind s) dowait ps [] -- <2>
where
dowait as = loop (reverse as) -- <3>
loop [] = return Nothing
loop (a:as) = do -- <4>
r <- wait a
case r of
Nothing -> loop as
Just a -> return (Just a)
-- >>
-- <<subfind
subfind :: String -> FilePath
-> ([Async (Maybe FilePath)] -> IO (Maybe FilePath))
-> [Async (Maybe FilePath)] -> IO (Maybe FilePath)
subfind s p inner asyncs = do
isdir <- doesDirectoryExist p
if not isdir
then inner asyncs
else withAsync (find s p) $ \a -> inner (a:asyncs)
-- >>