Created
October 5, 2016 12:46
-
-
Save michaelt/18662a37113424c0d2ca5fd6816de2fa to your computer and use it in GitHub Desktop.
oneShot + Sink
This file contains hidden or 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
module Main (main) where | |
import System.IO.Error | |
import GHC.Magic | |
data Sink = Await (Maybe Char -> Sink) | Done Int | |
countFrom :: Int -> Sink | |
countFrom n = let k = countFrom $! n + 1 | |
-- in Await $ \mi -> case mi of | |
in Await $ oneShot $ \mi -> case mi of | |
Nothing -> Done n | |
Just _ -> k | |
feedFrom :: Int -> Sink -> IO () | |
feedFrom _ (Done n) = print n | |
feedFrom 0 (Await f) = feedFrom 0 (case f $ Nothing of a -> a) | |
feedFrom n (Await f) = feedFrom (n - 1) (case f $ Just 'A' of a -> a) | |
main :: IO () | |
main = let a = feedFrom 10000000 (countFrom 0) in a >> a |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment