Created
April 29, 2017 01:45
-
-
Save maca/2b51a2ead717356b2d0715cd77ea0cee to your computer and use it in GitHub Desktop.
Csv parsing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Regex exposing (split, regex) | |
import List exposing (..) | |
import Html exposing (table, tr, td, th, text) | |
import Dict | |
input : String | |
input = | |
"name,age,gender\nmacario,36,male\nfabian,32,male" | |
rowToDict header row = | |
map2 (,) header row | |
|> Dict.fromList | |
parse str = | |
let | |
rows = | |
split Regex.All (regex "\n") str | |
|> map (split Regex.All (regex ",")) | |
in | |
case rows of | |
header :: data -> | |
Just (header, map (rowToDict header) data) | |
_ -> | |
Nothing | |
renderCol col = | |
let | |
content = | |
case col of | |
Just something -> | |
something | |
Nothing -> | |
"" | |
in | |
td [ ] [ text content ] | |
renderRow row = | |
tr | |
[ ] | |
[ renderCol <| Dict.get "name" row | |
, renderCol <| Dict.get "age" row | |
, renderCol <| Dict.get "gender" row | |
] | |
main = | |
case parse input of | |
Just (header, rows) -> | |
let | |
formHeader = | |
tr | |
[ ] | |
(map (\member -> th [ ] [text member]) header) | |
formRows = | |
map renderRow rows | |
in | |
table | |
[] | |
(formHeader :: formRows) | |
Nothing -> | |
text "fix your data you dumb fuck!!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment