Last active
December 17, 2015 22:39
-
-
Save k0001/5683256 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 Control.Proxy ((>->), (\>\)) | |
import qualified Control.Proxy as P | |
import qualified Control.Proxy.Trans.State as P | |
import qualified Control.Proxy.Trans.Either as P | |
import qualified Control.Proxy.Parse as Pa | |
import qualified Control.Proxy.Binary as Pb | |
import qualified Data.Binary as Bin | |
import qualified Data.ByteString.Lazy as BL | |
import qualified Data.ByteString as BS | |
import Data.Monoid | |
intByTen :: Int -> Int | |
intByTen = (* 10) | |
{- Pa.decodeD' type: | |
decodeD' | |
:: (P.Proxy p, Monad m, Bin.Binary r) | |
=> () | |
-> Pe.EitherP ParsingError (Ps.StateP [BS.ByteString] p) | |
() (Maybe BS.ByteString) b' b m r | |
-} | |
main = do | |
let srcS = (Pa.wrap.) . P.fromListS $ BL.toChunks . Bin.encode =<< [3..5::Int] | |
run = P.runProxy . P.evalStateK mempty . P.runEitherK | |
putStrLn "-- Approach 1" | |
run $ srcS >-> (Pb.decodeD' \>\ P.mapD intByTen \>\ P.pull) >-> P.printD | |
putStrLn "-- Approach 2" | |
run $ srcS >-> byTenD >-> P.printD | |
putStrLn "-- Approach 3: Adds a '1' to output, after EOF." | |
run $ srcS >-> byTenThenOneD >-> P.printD | |
byTenD | |
:: (P.Proxy p, Monad m) | |
=> () | |
-> P.EitherP Pb.ParsingError (P.StateP [BS.ByteString] p) | |
() (Maybe BS.ByteString) () Int m r | |
byTenD = \() -> loop where | |
loop = do | |
num <- Pb.decodeD' () | |
P.respond (num * 10) | |
loop | |
byTenThenOneD | |
:: (P.Proxy p, Monad m) | |
=> () | |
-> P.EitherP Pb.ParsingError (P.StateP [BS.ByteString] p) | |
() (Maybe BS.ByteString) () Int m () | |
byTenThenOneD = \() -> loop where | |
loop = do | |
num <- Pb.decodeD' () | |
P.respond (num * 10) | |
eof <- P.liftP Pa.isEndOfInput | |
if eof | |
then P.respond 1 | |
else loop |
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
% ./hello | |
-- Approach 1 | |
30 | |
40 | |
50 | |
-- Approach 2 | |
30 | |
40 | |
50 | |
-- Approach 3: Adds a '1' to output, after EOF. | |
30 | |
40 | |
50 | |
1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment