Created
January 25, 2019 07:05
-
-
Save harendra-kumar/0f4ddb4997f5c9f7db4d98ae9dffa69b to your computer and use it in GitHub Desktop.
streamly-type
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 RankNTypes #-} | |
{-# LANGUAGE FlexibleInstances #-} | |
{-# LANGUAGE MultiParamTypeClasses #-} | |
import Control.Monad.Trans.Class | |
import Control.Monad | |
newtype Stream m a = Stream | |
{ runStream :: (forall r. (a -> Stream m a -> m r) -> m r -> m r) } | |
instance Functor (Stream m) where | |
fmap f (Stream m) = Stream $ \yld stp -> m (\a r -> yld (f a) (fmap f r)) stp | |
instance Semigroup (Stream m a) where | |
Stream m1 <> Stream m2 = | |
Stream $ \yld stp -> m1 (\a r -> yld a (r <> Stream m2)) (m2 yld stp) | |
instance Monad (Stream m) where | |
return a = Stream $ \yld _ -> yld a (Stream $ \_ stp -> stp) | |
Stream m >>= f = Stream $ \yld stp -> | |
m (\a r -> runStream (f a <> (r >>= f)) yld stp) stp | |
instance Applicative (Stream m) where | |
pure = return | |
(<*>) = ap | |
instance MonadTrans Stream where | |
lift m = Stream $ \yld _ -> m >>= \a -> yld a (Stream $ \_ stp -> stp) | |
main :: IO () | |
main = do | |
let s = return 0 :: Stream m Int | |
let run x = runStream x (\a _ -> print a) (return ()) | |
run s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment