Skip to content

Instantly share code, notes, and snippets.

@dcmorse
Created October 14, 2018 23:52
Show Gist options
  • Save dcmorse/9a83e58c9608b181b6b1a4130d19ece1 to your computer and use it in GitHub Desktop.
Save dcmorse/9a83e58c9608b181b6b1a4130d19ece1 to your computer and use it in GitHub Desktop.
parsing recursive, lisp-like JSON in elm
-- I feel very uncertain this is the Right Thing.
-- tested against Elm 0.19
import Json.Decode exposing (..)
type TreeOfInt
= Branch (List TreeOfInt)
| Leaf Int
-- decodeString treeOfInt "[7, [10]]" => Branch [Leaf 7, Branch [Leaf 10]]
treeOfInt : Decoder TreeOfInt
treeOfInt =
let
f : Maybe Int -> Decoder TreeOfInt
f maybeInt =
case maybeInt of
Just i ->
succeed (Leaf i)
Nothing ->
andThen g (list treeOfInt)
g : List TreeOfInt -> Decoder TreeOfInt
g = succeed << Branch
in
andThen f (maybe int)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment