File: LazyExamples.hs

package info (click to toggle)
haskell-directory-tree 0.12.1-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 104 kB
  • sloc: haskell: 398; makefile: 2
file content (36 lines) | stat: -rw-r--r-- 953 bytes parent folder | download | duplicates (7)
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
module Main
    where

import System.Directory.Tree
import qualified Data.Foldable as F
import System.IO
import Control.Monad 



main = du "/etc"



-- Here are a few examples of using the directory-tree package to recreate
-- the basic functionality of some linux command-line tools. This module
-- uses the lazy directory building IO provided by `readDirectoryWithL`:



-- the command `ls <dir>`. Try: 
--     ghci> ls "/"
-- ...IO is done lazily.
ls :: FileName -> IO ()
ls d = do (_ :/ Dir _ c) <- readDirectoryWithL readFile d
          mapM_ (putStrLn . name) c



-- the command `du -bs <dir> 2> /dev/null` gets the total size of all files 
-- under the supplied directory. We use a more compositional style here, where 
-- (<=<) is equivalent to (.) but for monadic functions (a -> m b):
du :: FileName -> IO ()
du = print . F.foldl' (+) 0 . free <=< readDirectoryWithL (hFileSize <=< readHs)
    where readHs = flip openFile ReadMode