Skip to content

Instantly share code, notes, and snippets.

@michaelt
Created October 5, 2016 12:46
Show Gist options
  • Save michaelt/18662a37113424c0d2ca5fd6816de2fa to your computer and use it in GitHub Desktop.
Save michaelt/18662a37113424c0d2ca5fd6816de2fa to your computer and use it in GitHub Desktop.
oneShot + Sink
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