Skip to content

Instantly share code, notes, and snippets.

@erantapaa
Last active August 29, 2015 14:27
Show Gist options
  • Select an option

  • Save erantapaa/f1075d0d8b1fd0b6be55 to your computer and use it in GitHub Desktop.

Select an option

Save erantapaa/f1075d0d8b1fd0b6be55 to your computer and use it in GitHub Desktop.
pipes server-client example
-- Example of using the Rakhana libraries.
-# LANGUAGE NoMonomorphismRestriction #-}
module Foo
where
import Data.Rakhana
import Control.Monad.Trans (liftIO)
import qualified Data.ByteString.Char8 as B
showBytes b = do
putStr $ "bytes length: " ++ show (B.length b) ++ " |"
B.putStr b
putStrLn "|"
myDrive :: Drive IO ()
myDrive = do liftIO $ putStrLn "Hello!"
b <- driveGet 5
liftIO $ showBytes b
driveDiscard 3
b <- driveGet 4
liftIO $ showBytes b
example :: IO ()
example = runDrive (fileTape "input") myDrive
-- This type checks:
import Pipes
import Pipes.Core
server :: Server Int String IO ()
server = undefined
client :: Client Int String IO ()
client = undefined
example = runEffect $ server >>~ const client
{-# LANGUAGE RankNTypes, NoMonomorphismRestriction #-}
module Bar where
import Pipes
import Pipes.Core
import Control.Monad.Trans (liftIO)
-- types of Requests:
data Req = Odd Int | Positive Int | Succ Int
-- types of Responses:
data Resp = U | B Bool | I Int
type Answerer m a = Server' Req Resp m a
type Asker m a = Client' Req Resp m a
serverLoop :: Monad m => Req -> Answerer m a
serverLoop rq = do
let r = case rq of
Odd n -> B (odd n)
Positive n -> B (n > 0)
Succ n -> I (n+1)
rq' <- respond r
serverLoop rq'
asker :: (MonadIO m, Monad m) => Asker m ()
asker = do
B b <- request (Odd 3)
liftIO $ putStrLn $ "is 3 odd? -> " ++ show b
B b <- request (Positive 0)
liftIO $ putStrLn $ "is 0 positive? -> " ++ show b
I n <- request (Succ 41)
liftIO $ putStrLn $ "what comes after 41? -> " ++ show n
startServer = do
r <- respond undefined
serverLoop r
example = runEffect $ startServer >>~ (const asker)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment