Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
27
Task/Create-an-HTML-table/Haskell/create-an-html-table-1.hs
Normal file
27
Task/Create-an-HTML-table/Haskell/create-an-html-table-1.hs
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
import Data.List (unfoldr)
|
||||
import Control.Monad (forM_)
|
||||
|
||||
import qualified Text.Blaze.Html5 as B
|
||||
import Text.Blaze.Html.Renderer.Pretty (renderHtml)
|
||||
|
||||
import System.Random (RandomGen, getStdGen, randomRs, split)
|
||||
|
||||
makeTable
|
||||
:: RandomGen g
|
||||
=> [String] -> Int -> g -> B.Html
|
||||
makeTable headings nRows gen =
|
||||
B.table $
|
||||
do B.thead $ B.tr $ forM_ (B.toHtml <$> headings) B.th
|
||||
B.tbody $
|
||||
forM_
|
||||
(zip [1 .. nRows] $ unfoldr (Just . split) gen)
|
||||
(\(x, g) ->
|
||||
B.tr $
|
||||
forM_
|
||||
(take (length headings) (x : randomRs (1000, 9999) g))
|
||||
(B.td . B.toHtml))
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
g <- getStdGen
|
||||
putStrLn $ renderHtml $ makeTable ["", "X", "Y", "Z"] 3 g
|
||||
52
Task/Create-an-HTML-table/Haskell/create-an-html-table-2.hs
Normal file
52
Task/Create-an-HTML-table/Haskell/create-an-html-table-2.hs
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
{-# LANGUAGE QuasiQuotes #-}
|
||||
|
||||
import Data.List (elemIndex)
|
||||
import Text.Hamlet (shamlet, Html)
|
||||
import Text.Cassius (Css, renderCss, cassius)
|
||||
import Text.Blaze.Html.Renderer.String (renderHtml)
|
||||
import System.Random (getStdGen, randomRs)
|
||||
|
||||
styles :: p -> Css
|
||||
styles = [cassius|
|
||||
table, th, td
|
||||
border: 1px solid black
|
||||
border-collapse: collapse
|
||||
th, td
|
||||
padding: 15px
|
||||
th, .rowLabel
|
||||
background-color: #895
|
||||
td
|
||||
text-align: right
|
||||
|]
|
||||
|
||||
renderTable :: [[Int]] -> Html
|
||||
renderTable xs = [shamlet|
|
||||
$doctype 5
|
||||
<html>
|
||||
<head>
|
||||
<style>
|
||||
#{renderCss $ styles undefined}
|
||||
<body>
|
||||
<table>
|
||||
<tr>
|
||||
<th>
|
||||
$forall header <- headers
|
||||
<th>#{header}
|
||||
$forall row <- xs
|
||||
<tr>
|
||||
$maybe index <- elemIndex row xs
|
||||
<td .rowLabel>#{index + 1}
|
||||
$nothing
|
||||
<td>?
|
||||
$forall cell <- row
|
||||
<td>#{cell}
|
||||
|]
|
||||
where
|
||||
headers = ['X', 'Y', 'Z']
|
||||
|
||||
main :: IO ()
|
||||
main = renderHtml . renderTable . rowsOf 3 . take 9 <$> randomValues >>= putStrLn
|
||||
where
|
||||
rowsOf _ [] = []
|
||||
rowsOf n xs = take n xs : rowsOf n (drop n xs)
|
||||
randomValues = randomRs (1, 9999) <$> getStdGen
|
||||
Loading…
Add table
Add a link
Reference in a new issue