Skip to content

@snoyberg /fib.hs
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Demonstrate conversion from standard Yesod to yesod-pure
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeFamilies #-}
import Control.Applicative ((<$>))
import Text.Blaze.Html (toValue, (!))
import qualified Text.Blaze.Html5 as H
import qualified Text.Blaze.Html5.Attributes as HA
import Yesod.Pure
data App = App
instance RenderRoute App where
data Route App = HomeR
| FibR Int
deriving Eq
renderRoute HomeR = ([], [])
renderRoute (FibR i) = (["fib", toPathPiece i], [])
parseRoute :: RouteParse App
parseRoute [] = Just HomeR
parseRoute ["fib", i] = FibR <$> fromPathPiece i
parseRoute _ = Nothing
dispatchRoute :: RouteDispatch App
dispatchRoute "GET" HomeR = handler getHomeR
dispatchRoute "GET" (FibR i) = handler $ getFibR i
dispatchRoute _ _ = Nothing
instance YesodDispatch App App where
yesodDispatch = dispatch parseRoute dispatchRoute
instance Yesod App
type Handler = GHandler App App
getHomeR :: Handler RepHtml
getHomeR = defaultLayout $ do
setTitle "Hello World!"
toWidget $ \render -> do
H.p "Hello World"
H.a ! HA.href (toValue $ render (FibR 5) []) $ "Fifth fib"
addCSS "p { color: red }"
getFibR :: Int -> Handler RepHtml
getFibR i = defaultLayout $ do
setTitle "Fibs"
toWidget $ \render -> do
H.p $ do
"Fib for "
toHtml i
": "
toHtml $ fibs !! i
H.a ! HA.href (toValue $ render (FibR $ i + 1) []) $ "Next fib"
fib :: Int -> Int
fib i = fibs !! i
fibs :: [Int]
fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
main :: IO ()
main = warpDebug 3000 App
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.