Skip to content

Instantly share code, notes, and snippets.

@maca
Created April 29, 2017 01:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save maca/2b51a2ead717356b2d0715cd77ea0cee to your computer and use it in GitHub Desktop.
Save maca/2b51a2ead717356b2d0715cd77ea0cee to your computer and use it in GitHub Desktop.
Csv parsing
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