elm-community / elm-community/json-extra

andThen2 : (a -> b -> Decoder c) -> Decoder a -> Decoder b -> Decoder c

Open
#25 3 comments 0 reactions 1 assignee Claimed by @zwilias View on GitHub
Dominant language
Elm
Stars
40
Forks
16
PR merge metrics
No merged PRs in 30d

Description

For combining multiple decode results in a way that might fail.

E.g if you have json that looks like
```json
{
"ids": [1, 2, 3, 4],
"names": ["Not", "Enough", "Names"]
}
```
and you want to zip the two lists together, but fail if they're different lengths, you could do
```elm
Decode.Extra.andThen2
(\ids names ->
if List.length ids == List.length names
Decode.succeed (List.zip ids names)
else
Decode.fail "expected the same number of ids and names"
)
(field "ids" (list int))
(field "names" (list string))
```

Can be implemented as something like
```elm
andThen2 : (a -> b -> Decoder c) -> Decoder a -> Decoder b -> Decoder c
andThen2 f decoderA decoderB =
map2 Tuple.pair decoderA decoderB
|> andThen (\(a, b) -> f a b)
```

It's simple enough to use the tuple everywhere, but it reads a little better to be able to say `andThen2` instead.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.