Skip to content

Instantly share code, notes, and snippets.

@solatis
Last active August 29, 2015 14:05
Show Gist options
  • Save solatis/22942319dbfae491e80e to your computer and use it in GitHub Desktop.
Save solatis/22942319dbfae491e80e to your computer and use it in GitHub Desktop.
My inability to comprehend Monads
module Main where
import System.ZMQ4.Monadic (ZMQ, Socket, socket, bind, send, runZMQ, liftIO, Push(..))
import System.ZMQ4 (Sender)
import Data.ByteString.Char8 (pack)
run :: System.ZMQ4.Sender t => Socket z t -> ZMQ z ()
run publisher =
mapM_ ((send publisher []) . pack) urls
where
urls = [generateUrl x | x <- [0,100..]]
generateUrl :: Integer -> String
generateUrl i = "http://www.imdb.com/search/title?count=100&start=" ++ show i
main :: IO ()
main =
runZMQ $ do
publisher <- socket Push
bind publisher "tcp://*:10150"
run publisher
liftIO $ putStrLn "Sent urls"
module Main where
import System.ZMQ4.Monadic (ZMQ, Socket, socket, bind, send, runZMQ, liftIO, Push(..))
import System.ZMQ4 (Sender)
import Data.ByteString.Char8 (pack)
run :: System.ZMQ4.Sender t => Socket z t -> ZMQ z ()
run publisher =
mapM_ ((send publisher []) . pack) urls
where
urls = [generateUrl x | x <- [0,100..]]
generateUrl :: Integer -> String
generateUrl i = "http://www.imdb.com/search/title?count=100&start=" ++ show i
openSender :: System.ZMQ4.Sender t => Int -> Socket z t -> ZMQ z ()
openSender = undefined
main :: IO ()
main =
runZMQ $ do
run $ openSender 10150
liftIO $ putStrLn "Sent urls"
{-
Compiler error:
src/Main.hs:23:19:
Couldn't match expected type ‘Socket z t0’
with actual type ‘Socket z0 t1 -> ZMQ z0 ()’
Probable cause: ‘openSender’ is applied to too few arguments
In the second argument of ‘($)’, namely ‘openSender 10150’
In a stmt of a 'do' block: run $ openSender 10150
-}
module Main where
import System.ZMQ4.Monadic
import Data.ByteString.Char8 (pack)
run :: Sender t => Socket z t -> ZMQ z ()
run publisher =
mapM_ ((send publisher []) . pack) urls
where
urls = [generateUrl x | x <- [0,100..]]
generateUrl :: Integer -> String
generateUrl i = "http://www.imdb.com/search/title?count=100&start=" ++ show i
openSender :: Int -> ZMQ z (Socket z t)
openSender port = undefined
main :: IO ()
main =
runZMQ $ do
run =<< openSender 10150
liftIO $ putStrLn "Sent urls"
{-
But this generates a compiler error:
src/Main.hs:21:13:
No instance for (Sender t0) arising from a use of ‘run’
The type variable ‘t0’ is ambiguous
Note: there are several potential instances:
instance Sender Dealer -- Defined in ‘System.ZMQ4’
instance Sender Pair -- Defined in ‘System.ZMQ4’
instance Sender Pub -- Defined in ‘System.ZMQ4’
...plus 7 others
Maybe this is because we left the openSender function undefined?
-}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment