forked from composewell/streamly
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileIOExamples.hs
More file actions
65 lines (53 loc) · 2.07 KB
/
FileIOExamples.hs
File metadata and controls
65 lines (53 loc) · 2.07 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import qualified Streamly.Prelude as S
import qualified Streamly.Data.Fold as FL
import qualified Streamly.Memory.Array as A
import qualified Streamly.Internal.Data.Fold as FL
import qualified Streamly.Internal.Prelude as IP
import qualified Streamly.Internal.FileSystem.File as File
import Data.Char (ord)
import System.Environment (getArgs)
cat :: FilePath -> IO ()
cat src =
File.fromChunks "/dev/stdout"
$ File.toChunksWithBufferOf (256*1024) src
cp :: FilePath -> FilePath -> IO ()
cp src dst =
File.fromChunks dst
$ File.toChunksWithBufferOf (256*1024) src
append :: FilePath -> FilePath -> IO ()
append src dst =
File.appendChunks dst
$ File.toChunksWithBufferOf (256*1024) src
ord' :: Num a => Char -> a
ord' = (fromIntegral . ord)
wcl :: FilePath -> IO ()
wcl src = print =<< (S.length
$ S.splitOnSuffix (== ord' '\n') FL.drain
$ File.toBytes src)
grepc :: String -> FilePath -> IO ()
grepc pat src = print . (subtract 1) =<< (S.length
$ IP.splitOnSeq (A.fromList (map ord' pat)) FL.drain
$ File.toBytes src)
avgll :: FilePath -> IO ()
avgll src = print =<< (S.fold avg
$ S.splitOnSuffix (== ord' '\n') FL.length
$ File.toBytes src)
where avg = (/) <$> toDouble FL.sum <*> toDouble FL.length
toDouble = fmap (fromIntegral :: Int -> Double)
llhisto :: FilePath -> IO ()
llhisto src = print =<< (S.fold (FL.classify FL.length)
$ S.map bucket
$ S.splitOnSuffix (== ord' '\n') FL.length
$ File.toBytes src)
where
bucket n = let i = n `mod` 10 in if i > 9 then (9,n) else (i,n)
main :: IO ()
main = do
src <- fmap head getArgs
putStrLn "cat" >> cat src -- Unix cat program
putStr "wcl " >> wcl src -- Unix wc -l program
putStr "grepc " >> grepc "aaaa" src -- Unix grep -c program
putStr "avgll " >> avgll src -- get average line length
putStr "llhisto " >> llhisto src -- get line length histogram
putStr "cp " >> cp src "dst-xyz.txt" -- Unix cp program
putStr "append " >> append src "dst-xyz.txt" -- Appending to file