Skip to content

Instantly share code, notes, and snippets.

@mkohlhaas
Created January 16, 2022 05:05
Show Gist options
  • Save mkohlhaas/089e3a9d99fec939ca5e59c4f8680f0b to your computer and use it in GitHub Desktop.
Save mkohlhaas/089e3a9d99fec939ca5e59c4f8680f0b to your computer and use it in GitHub Desktop.
What is the Reader Monad Good For ?
  • view
    • page
      • topnav
      • content (needs email)
        • left
        • right
          • article
            • widget (needs email)
main :: IO ()
main = do
putStrLn "what is your email address?"
email <- getLine
putStrLn . show $ runReader view email
view :: Reader Email Html
view = do
page' <- page
return $ div
[ page'
]
page :: Reader Email Html
page = do
content' <- content
return $ div
[ topNav
, content'
]
topNav :: Html
topNav =
div
[ h1 [ "OurSite.com" ]
]
content :: Reader Email Html
content = do
email <- ask
right' <- right
return $ div
[ h1 [ "Custom Content for " ++ email ]
, left
, right'
]
left :: Html
left =
div
[ p [ "this is the left side" ]
]
right :: Reader Email Html
right = do
article' <- article
return $ div
[ article'
]
article :: Reader Email Html
article = do
widget' <- widget
return $ div
[ p [ "this is an article" ]
, widget'
]
widget :: Reader Email Html
widget = do
email <- ask
return $ div
[ p [ "Hey " ++ email ++ ", we've got a great offer for you!" ]
]
main :: IO ()
main =
putStrLn "what is your email address?" >>
getLine >>= \email ->
putStrLn . show $ view email
view :: Email -> Html
view email =
div
[ page email
]
page :: Email -> Html
page email =
div
[ topNav
, content email
]
topNav :: Html
topNav =
div
[ h1 [ "OurSite.com" ]
]
content :: Email -> Html
content email =
div
[ h1 [ "Custom Content for " ++ email ]
, left
, right email
]
left :: Html
left =
div
[ p [ "this is the left side" ]
]
right :: Email -> Html
right email =
div
[ article email
]
article :: Email -> Html
article email =
div
[ p [ "this is an article" ]
, widget email
]
widget :: Email -> Html
widget email =
div
[ p [ "Hey " ++ email ++ ", we've got a great offer for you!" ]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment