simple-pipe

simple pipeline library like conduit https://github.com/YoshikuniJujo/simple-pipe/wiki

Latest on Hackage:0.0.0.29

This package is not currently in any snapshots. If you're interested in using it, we recommend adding it to Stackage Nightly. Doing so will make builds more reliable, and allow stackage.org to host generated Haddocks.

BSD3 licensed and maintained by Yoshikuni Jujo

examples/upperFile.hs

  • read file (sample.txt)

  • take 3 lines

  • to upper all lines

  • write to stdout

extensions

  • PackageImports

 import Data.Pipe
 import Data.Char
 import System.IO
 import "monads-tf" Control.Monad.Trans

 main :: IO ()
 main = do
 	_ <- runPipe $ readFileP "sample.txt"
		=$= takeP 3
		=$= convert (map toUpper)
		=$= writeString
 	return ()

 readFileP :: FilePath -> Pipe () String IO ()
 readFileP fp = bracket (openFile fp ReadMode) hClose hRead

 hRead :: Handle -> Pipe () String IO ()
 hRead h = do
 	eof <- lift $ hIsEOF h
 	if eof then return () else do
 		l <- lift $ hGetLine h
 		yield l
 		hRead h

 writeString :: Pipe String () IO ()
 writeString = do
 	ms <- await
 	case ms of
 		Just s -> lift (putStrLn s) >> writeString
 		_ -> return ()

 takeP :: Monad m => Int -> Pipe a a m ()
 takeP 0 = return ()
 takeP n = do
	mx <- await
	case mx of
		Just x -> yield x >> takeP (n - 1)
		_ -> return ()
comments powered byDisqus