Create a gist now

Instantly share code, notes, and snippets.

@alicebob /Main.elm Secret
Last active Jan 2, 2018

Embed
What would you like to do?
  1. alicebob revised this gist Jan 2, 2018. 1 changed file with 4 additions and 11 deletions.
    View
    @@ -12,24 +12,17 @@ elemListDecoder =
    elemDecoder : Decoder Elem
    elemDecoder =
    Json.Decode.oneOf [ elemDecoderChunk, elemDecoderType ]
    elemDecoderChunk : Decoder Elem
    elemDecoderChunk =
    Json.Decode.map Chunk string
    elemDecoderType : Decoder Elem
    elemDecoderType =
    Json.Decode.field "type" string
    Json.Decode.oneOf
    [ Json.Decode.map Chunk string
    , Json.Decode.field "type" string
    |> Json.Decode.andThen elemDecoderTypeHelp
    ]
    elemDecoderTypeHelp : String -> Decoder Elem
    elemDecoderTypeHelp t =
    case t of
    "p" ->
    -- meta: using elemDecoder directly doesn't work somehow
    Json.Decode.map Para (Json.Decode.field "content" (Json.Decode.list elemDecoder))
    -- Json.Decode.map Para (Json.Decode.field "content" (Json.Decode.list (Json.Decode.oneOf [ elemDecoderChunk, elemDecoderType])))
    _ -> Json.Decode.fail <| "Can't do a " ++ t ++ " elem"
  2. alicebob revised this gist Jan 2, 2018. 1 changed file with 71 additions and 1 deletion.
    View
    @@ -1 +1,71 @@
    123
    import Html exposing (..)
    import Json.Decode exposing (Decoder, list, string)
    import Json.Decode
    type Elem
    = Para (List Elem)
    | Chunk String
    elemListDecoder : Decoder (List Elem)
    elemListDecoder =
    Json.Decode.list elemDecoder
    elemDecoder : Decoder Elem
    elemDecoder =
    Json.Decode.oneOf [ elemDecoderChunk, elemDecoderType ]
    elemDecoderChunk : Decoder Elem
    elemDecoderChunk =
    Json.Decode.map Chunk string
    elemDecoderType : Decoder Elem
    elemDecoderType =
    Json.Decode.field "type" string
    |> Json.Decode.andThen elemDecoderTypeHelp
    elemDecoderTypeHelp : String -> Decoder Elem
    elemDecoderTypeHelp t =
    case t of
    "p" ->
    -- meta: using elemDecoder directly doesn't work somehow
    Json.Decode.map Para (Json.Decode.field "content" (Json.Decode.list elemDecoder))
    -- Json.Decode.map Para (Json.Decode.field "content" (Json.Decode.list (Json.Decode.oneOf [ elemDecoderChunk, elemDecoderType])))
    _ -> Json.Decode.fail <| "Can't do a " ++ t ++ " elem"
    --- skeleton from here on
    main =
    Html.beginnerProgram { model = model, view = view, update = update }
    model : Model
    model =
    let js = "[\"foo\", {\"type\": \"p\", \"content\": []}]"
    in
    case Json.Decode.decodeString elemListDecoder js of
    Ok d -> { doc = d }
    Err err -> Debug.crash err
    -- MODEL
    type alias Model = { doc : List Elem }
    -- UPDATE
    type Msg = Reset
    update : Msg -> Model -> Model
    update msg model =
    case msg of
    Reset -> model
    -- VIEW
    view : Model -> Html Msg
    view model =
    Html.text "all fine"
  3. alicebob created this gist Jan 2, 2018.
    View
    @@ -0,0 +1 @@
    123