Skip to content

Instantly share code, notes, and snippets.

@jamesmacaulay
Last active November 10, 2016 14:10
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesmacaulay/c2badacb93b091489dd4 to your computer and use it in GitHub Desktop.
Save jamesmacaulay/c2badacb93b091489dd4 to your computer and use it in GitHub Desktop.
-- 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