Created
October 2, 2010 18:17
-
-
Save snoyberg/607856 to your computer and use it in GitHub Desktop.
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
import Prelude hiding (head) | |
import Data.Enumerator hiding (map) | |
import Control.Exception (SomeException) | |
import Control.Monad.IO.Class | |
sum6 :: Monad m => Iteratee Int m Int | |
sum6 = do | |
maybeNum <- head | |
case maybeNum of | |
Nothing -> return 0 | |
Just i -> do | |
rest <- sum6 | |
return $ i + rest | |
extract :: Monad m => Iteratee a m b -> m (Either SomeException b) | |
extract (Iteratee mstep) = do | |
step <- mstep | |
case step of | |
Continue k -> do | |
let Iteratee mstep' = k EOF | |
step' <- mstep' | |
case step' of | |
Continue _ -> error "Misbehaving iteratee" | |
Yield b _ -> return $ Right b | |
Error e -> return $ Left e | |
Yield b _ -> return $ Right b | |
Error e -> return $ Left e | |
sum7 :: Monad m => Iteratee Int m Int | |
sum7 = Iteratee $ do | |
Continue k <- runIteratee sum6 | |
runIteratee $ k $ Chunks [1..10] | |
sum8 :: Monad m => Iteratee Int m Int | |
sum8 = Iteratee $ do | |
step <- runIteratee sum6 | |
case step of | |
Continue k -> runIteratee $ k $ Chunks [1..10] | |
_ -> return step | |
sum9 :: Monad m => Iteratee Int m Int -> Iteratee Int m Int | |
sum9 orig = Iteratee $ do | |
step <- runIteratee orig | |
case step of | |
Continue k -> runIteratee $ k $ Chunks [1..10] | |
_ -> return step | |
--sum10 :: Monad m => Step Int m Int -> Iteratee Int m Int | |
sum10 :: Monad m => Enumerator Int m Int | |
sum10 (Continue k) = k $ Chunks [1..10] | |
sum10 step = returnI step | |
sum11 :: Monad m => Enumerator Int m Int | |
sum11 (Continue k) = k $ Chunks [11..20] | |
sum11 step = returnI step | |
sum12 :: Monad m => [Int] -> Enumerator Int m Int | |
sum12 nums (Continue k) = k $ Chunks nums | |
sum12 _ step = returnI step | |
genericSum12 :: Monad m => [a] -> Enumerator a m b | |
genericSum12 nums (Continue k) = k $ Chunks nums | |
genericSum12 _ step = returnI step | |
applyEnum :: Monad m => Enumerator a m b -> Iteratee a m b -> Iteratee a m b | |
applyEnum enum iter = Iteratee $ do | |
step <- runIteratee iter | |
runIteratee $ enum step | |
getNumberEnum :: MonadIO m => Enumerator Int m b | |
getNumberEnum (Continue k) = do | |
x <- liftIO getLine | |
if x == "q" | |
then continue k | |
else k (Chunks [read x]) >>== getNumberEnum | |
getNumberEnum step = returnI step | |
intsToStrings :: (Show a, Monad m) => Iteratee a m String | |
intsToStrings = (unlines . map show) `fmap` consume | |
printStrings :: Iteratee String IO () | |
printStrings = do | |
mstring <- head | |
case mstring of | |
Nothing -> return () | |
Just string -> do | |
liftIO $ putStrLn string | |
printStrings | |
main = do | |
run_ sum6 >>= print | |
run_ sum7 >>= print | |
run_ sum8 >>= print | |
run_ (sum9 sum6) >>= print | |
run_ (applyEnum sum10 sum6) >>= print | |
run_ (applyEnum sum11 $ applyEnum sum10 sum6) >>= print | |
run_ (sum11 $$ sum10 $$ sum6) >>= print | |
run_ (enumList 5 [1..30] $$ sum6) >>= print | |
run_ (concatEnums | |
[ enumList 1 [1..10] | |
, enumList 1 [11..20] | |
, enumList 1 [21..30] | |
] $$ sum6) >>= print | |
run_ (getNumberEnum $$ sum6) >>= print | |
run_ (getNumberEnum $$ intsToStrings) >>= print |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment