on a4c2ae4 help sign in
streamly/examples/ListDir.hs
31 lines 1.2 kB history raw
1import System.IO (stdout, hSetBuffering, BufferMode(LineBuffering))
2import Streamly (aheadly, ahead, AheadT)
3import Data.Function ((&))
5import qualified Streamly.Prelude as S
6import qualified Streamly.Internal.FileSystem.Dir as Dir
8-- | List the current directory recursively using concurrent processing
9--
10main :: IO ()
11main = do
12 hSetBuffering stdout LineBuffering
13 S.mapM_ print $ aheadly $ recursePath (Left ".")
15 where
17 -- XXX Fix bug in enqueueAhead when mixing serial with ahead:
18 -- (\x -> S.yield dir <> S.concatMapWith ahead recursePath x)
19 -- :: SerialT IO String
20 -- or S.cons dir . S.concatMapWith ahead recursePath
21 recursePath :: Either String String -> AheadT IO String
22 recursePath (Left dir) =
23 Dir.toEither dir -- SerialT IO (Either String String)
24 & S.map (prefixDir dir) -- SerialT IO (Either String String)
25 & S.consM (return dir)
26 . S.concatMapWith ahead recursePath -- SerialT IO String
27 recursePath (Right file) = S.yield file -- SerialT IO String
29 prefixDir :: String -> Either String String -> Either String String
30 prefixDir dir (Right x) = Right $ dir ++ "/" ++ x
31 prefixDir dir (Left x) = Left $ dir ++ "/" ++ x