Create an HTML table: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) (→{{header|Raku}}: Fix up some internal links) |
|||
Line 2,208: | Line 2,208: | ||
g <- getStdGen |
g <- getStdGen |
||
putStrLn $ renderHtml $ makeTable ["", "X", "Y", "Z"] 3 g</lang> |
putStrLn $ renderHtml $ makeTable ["", "X", "Y", "Z"] 3 g</lang> |
||
Or using the Shakespeare templates from Yesod |
|||
{{libheader|shakespeare}} |
|||
<lang haskell>{-# LANGUAGE QuasiQuotes #-} |
|||
{-# LANGUAGE OverloadedStrings #-} |
|||
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| |
|||
<style> |
|||
#{renderCss $ styles undefined} |
|||
<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</lang> |
|||
=={{header|Icon}} and {{header|Unicon}}== |
=={{header|Icon}} and {{header|Unicon}}== |