Last active
May 3, 2019 04:27
Star
You must be signed in to star a gist
is-stream sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE OverloadedStrings #-} | |
module SampleStream (main) where | |
import Data.Foldable (forM_) | |
import qualified Data.Text as T | |
import qualified Data.Text.IO as T | |
import System.IO (IOMode (..), withFile) | |
import System.IO.Streams as S | |
main :: IO () | |
main = withFile "./test/data/warning01.txt" ReadMode $ \h -> do | |
is <- handleToInputStream h >>= | |
S.lines >>= | |
decodeUtf8 >>= | |
addBrace >>= \s -> | |
makeCount >>= \cnt -> | |
S.zip cnt s >>= | |
addLineNumber | |
os <- writeTextConsole | |
connect is os | |
writeTextConsole :: IO (OutputStream T.Text) | |
writeTextConsole = makeOutputStream $ \m -> forM_ m T.putStrLn | |
addLineNumber :: InputStream (Int, T.Text) -> IO (InputStream T.Text) | |
addLineNumber = S.map (\(idx, tx) -> T.pack (show idx) `T.append` ":" `T.append` tx) | |
addBrace :: InputStream T.Text -> IO (InputStream T.Text) | |
addBrace = S.map (\x -> "{" `T.append` T.strip x `T.append` "}") | |
makeCount :: IO (InputStream Int) | |
makeCount = S.fromList [1..] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment