New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example for Json.Decode.value wrong in documentation #364

Closed
jvoigtlaender opened this Issue Aug 22, 2015 · 2 comments

Comments

Projects
None yet
1 participant
@jvoigtlaender
Contributor

jvoigtlaender commented Aug 22, 2015

The example at http://package.elm-lang.org/packages/elm-lang/core/2.1.0/Json-Decode#value is missing some brackets. Adding them gives:

variadic2 : (a -> b -> List c -> value) -> Decoder a -> Decoder b -> Decoder (List c) -> Decoder value
variadic2 f a b cs =
    customDecoder (list value) (\jsonList ->
        case jsonList of
          one :: two :: rest ->
              Result.map3 f
                (decodeValue a one)
                (decodeValue b two)
                (decodeValue cs rest)

          _ -> Result.Err "expecting at least two elements in the array")

But that's not type-correct according to http://elm-lang.org/try. (It complains about mismatched types Value vs. List Value in the 2nd argument of (decodeValue cs rest).)

@jvoigtlaender

This comment has been minimized.

Show comment
Hide comment
@jvoigtlaender

jvoigtlaender Aug 22, 2015

Contributor

A possible fix is this:

variadic2 : (a -> b -> List c -> value) -> Decoder a -> Decoder b -> Decoder c -> Decoder value
variadic2 f a b c =
    customDecoder (list value) (\jsonList ->
        case jsonList of
          one :: two :: rest ->
              Result.map3 f
                (decodeValue a one)
                (decodeValue b two)
                (combine (List.map (decodeValue c) rest))

          _ -> Result.Err "expecting at least two elements in the array")

where

combine : List (Result x a) -> Result x (List a)
combine = List.foldr (Result.map2 (::)) (Ok [])

is a function analogous to http://package.elm-lang.org/packages/Apanatshka/elm-signal-extra/5.4.1/Signal-Extra#combine (or to http://hackage.haskell.org/package/base-4.8.1.0/docs/Data-Traversable.html#v:sequenceA).

Contributor

jvoigtlaender commented Aug 22, 2015

A possible fix is this:

variadic2 : (a -> b -> List c -> value) -> Decoder a -> Decoder b -> Decoder c -> Decoder value
variadic2 f a b c =
    customDecoder (list value) (\jsonList ->
        case jsonList of
          one :: two :: rest ->
              Result.map3 f
                (decodeValue a one)
                (decodeValue b two)
                (combine (List.map (decodeValue c) rest))

          _ -> Result.Err "expecting at least two elements in the array")

where

combine : List (Result x a) -> Result x (List a)
combine = List.foldr (Result.map2 (::)) (Ok [])

is a function analogous to http://package.elm-lang.org/packages/Apanatshka/elm-signal-extra/5.4.1/Signal-Extra#combine (or to http://hackage.haskell.org/package/base-4.8.1.0/docs/Data-Traversable.html#v:sequenceA).

@jvoigtlaender

This comment has been minimized.

Show comment
Hide comment
Contributor

jvoigtlaender commented Aug 22, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment