Created
August 1, 2017 17:01
-
-
Save scott-fleischman/bd4e75138cb84884bcfbcc138abe8a99 to your computer and use it in GitHub Desktop.
Library Agnostic Streaming in Haskell from http://www.haskellforall.com/2014/11/how-to-build-library-agnostic-streaming.html
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 Main where | |
import Control.Monad.IO.Class (MonadIO, liftIO) | |
import Control.Monad (MonadPlus(mzero, mplus), msum) | |
import Data.Monoid (Monoid(..), (<>)) | |
import qualified Pipes | |
import qualified System.IO as IO | |
select :: MonadPlus m => [a] -> m a | |
-- select [] = mzero | |
-- select (x:xs) = return x `mplus` select xs | |
select = foldr (\x m -> return x `mplus` m) mzero | |
select' :: MonadPlus m => [a] -> m a | |
select' = msum . map return | |
replicateM'' :: MonadPlus m => Int -> m a -> m a | |
replicateM'' n = msum . replicate n | |
example :: (MonadIO m, MonadPlus m) => m () | |
example = do | |
x <- select [1, 2, 3] | |
liftIO (putStrLn ("x = " ++ show x)) | |
y <- select [4, 5, 6] | |
liftIO (putStrLn ("y = " ++ show y)) | |
replicateM' :: MonadPlus m => Int -> m a -> m a | |
replicateM' n m = do | |
m' <- select (replicate n m) | |
m' | |
-- or: replicateM' n = join . select . replicate n | |
stdinLn :: (MonadIO m, MonadPlus m) => m String | |
stdinLn = do | |
eof <- liftIO IO.isEOF | |
if eof | |
then mzero | |
else liftIO getLine `mplus` stdinLn | |
example1 :: (MonadIO m, MonadPlus m) => m () | |
example1 = do | |
liftIO getLine `mplus` liftIO getLine | |
liftIO (putStrLn "Foo") `mplus` liftIO (putStrLn "Foo") | |
example2 :: (MonadIO m, Monoid (m String), Monoid (m ())) => m () | |
example2 = do | |
liftIO getLine <> liftIO getLine | |
liftIO (putStrLn "Foo") <> liftIO (putStrLn "Foo") | |
echo :: (MonadIO m, MonadPlus m) => m () | |
echo = do | |
str <- stdinLn | |
liftIO (putStrLn str) | |
mapM' :: Monad m => (a -> m b) -> m a -> m b | |
mapM' = (=<<) | |
forM' :: Monad m => m a -> (a -> m b) -> m b | |
forM' = (>>=) | |
example' :: (MonadIO m, MonadPlus m) => m () | |
example' = mapM' (liftIO . print) (replicateM' 10 (liftIO getLine)) | |
main :: IO () | |
main = Pipes.runListT example' |
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
name: scratch | |
version: 0.1.0.0 | |
dependencies: | |
- base >= 4.7 && < 5 | |
- pipes | |
- text | |
- split | |
executables: | |
scratch: | |
main: Main.hs |
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
resolver: lts-8.23 | |
packages: | |
- . | |
extra-deps: [] | |
flags: {} | |
extra-package-dbs: [] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment