Skip to content

Instantly share code, notes, and snippets.

@danhodge
Last active October 1, 2019 03:34
Show Gist options
  • Save danhodge/29149aa5a0f505af0336eb7b2bb916b1 to your computer and use it in GitHub Desktop.
Save danhodge/29149aa5a0f505af0336eb7b2bb916b1 to your computer and use it in GitHub Desktop.
Elm Notes
-- JSON decoding
import Json.Decode
type alias Document = { message : String }
-- a decoder of type String that decodes the "message" field
message = Json.Decode.field "message" Json.Decode.string
-- a decode of type Document
doc = Json.Decode.map Document message
-- parses the provided String into a Document
Json.Decode.decodeString doc "{\"message\":\"stuff\"}"
-- decoding JSON into a record with a Maybe (when the field is mandatory in the JSON)
type alias Document = { message : Maybe String }
decoder = map (\msg -> Document Just msg) (field "message" string)
-- decoding a List of JSON
documentDecoder = map Document (field "message" string)
documentsDecoder = list documentDecoder
-- Anonymous function with multiple arguments
f = \a b -> a + b
-- Boolean not is a function: Bool -> Bool
-- to negate the results of another Bool-returning function, compose it with not
lt10 x = x < 10
gte10 = List.filter (lessThan10 >> not) [2,4,6,8,10,12]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment