Last active
November 10, 2016 14:10
-
-
Save jamesmacaulay/c2badacb93b091489dd4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
-- inspired by: https://groups.google.com/d/msg/elm-discuss/2LxEUVe0UBo/ZgJ_ldUH6ygJ | |
-- thanks for the help: http://learnyouahaskell.com/functors-applicative-functors-and-monoids | |
module UserDecoder where | |
import Date exposing (Date) | |
import User exposing (User) | |
import Json.Decode as Js exposing ((:=)) | |
-- Applicative's `pure`: | |
constructing : a -> Js.Decoder a | |
constructing = Js.succeed | |
-- Applicative's `<*>`: | |
apply : Js.Decoder (a -> b) -> Js.Decoder a -> Js.Decoder b | |
apply = Js.object2 (<|) | |
userDecoder : Js.Decoder User | |
userDecoder = | |
constructing User | |
`apply` ("id" := Js.int) | |
`apply` ("name" := Js.string) | |
`apply` ("email" := Js.string) | |
`apply` ("created_at" := Js.customDecoder Js.string Date.fromString) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment