Last active
October 28, 2016 14:29
-
-
Save mofas/170d54c0bc234af1b7bfc28b71601005 to your computer and use it in GitHub Desktop.
decode json to html
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 Html exposing (..) | |
import Debug exposing (..) | |
import List exposing (..) | |
import Json.Decode exposing (..) | |
jsonStr : String | |
jsonStr = """ | |
[ | |
{ | |
"title" : "讓廣告盯上消費者", | |
"summary" : " 夢想兩個字", | |
"date" : 1451836800 | |
}, | |
{ | |
"title" : "日本に上陸「人工知能のスター」", | |
"summary" : "人工知能は人間の行動を予測し", | |
"date" : 1455897600 | |
} | |
] | |
""" | |
main = | |
let | |
articleDecoder = list(object3 (,,) ("summary" := string) ("title" := string) ("date" := int)) | |
pressList: Result String (List ( String, String, Int )) | |
pressList = decodeString articleDecoder jsonStr | |
in | |
case pressList of | |
Result.Ok v -> | |
let | |
htmlList = v | |
|> List.map (\(summary, _, _) -> summary) | |
|> List.map Html.text | |
in | |
Html.div [] | |
htmlList | |
Result.Err err -> | |
Html.text "Error" |
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 Html exposing (..) | |
import Debug exposing (..) | |
import String exposing (..) | |
type alias Bar = | |
{ | |
a: String | |
, b: String | |
} | |
type alias Foo = | |
{ | |
bar : Bar | |
} | |
bar: Bar | |
bar = | |
{ | |
a = "A" | |
, b = "B" | |
} | |
foo: Foo | |
foo = | |
{ | |
bar = bar | |
} | |
doubleChar: String -> String | |
doubleChar a = String.repeat 2 a | |
tripleChar: String -> String | |
tripleChar a = String.repeat 3 a | |
updateBarA: ( String -> String ) -> Bar -> Bar | |
updateBarA f bar = | |
{ bar | a = ( f bar.a ) } | |
updateBarB: ( String -> String ) -> Bar -> Bar | |
updateBarB f bar = | |
{ bar | b = ( f bar.b ) } | |
updateFooBar: (Bar -> Bar ) -> Foo -> Foo | |
updateFooBar f foo = | |
{ foo | bar = ( f foo.bar ) } | |
updateBarATriple = updateBarA tripleChar | |
updateBarBDouble = updateBarB doubleChar | |
main = | |
let | |
newfoo = | |
foo | |
|> updateFooBar updateBarATriple | |
|> updateFooBar updateBarBDouble | |
in | |
div [] | |
[ | |
text newfoo.bar.a | |
, text newfoo.bar.b | |
] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment