Created
October 14, 2018 23:52
-
-
Save dcmorse/9a83e58c9608b181b6b1a4130d19ece1 to your computer and use it in GitHub Desktop.
parsing recursive, lisp-like JSON in elm
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
-- 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