Skip to content

clarify that Decode.map2 takes a constructor function as first argument #243

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion book/effects/json.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ That is all we needed for our HTTP example, but decoders can do more! For exampl
map2 : (a -> b -> value) -> Decoder a -> Decoder b -> Decoder value
```

This function takes in two decoders. It tries them both and combines their results. So now we can put together two different decoders:
This function takes in a type constructor function and two decoders. It tries both decoders and combines their results using the type constructor. So now we can put together two different decoders:

```elm
import Json.Decode exposing (Decoder, map2, field, string, int)
Expand All @@ -290,6 +290,8 @@ personDecoder =
(field "age" int)
```

Here, since a type alias generates a constructor function for its type, the `Person` constructor is the first argument to map2, followed by the two decoders.

So if we used `personDecoder` on `{ "name": "Tom", "age": 42 }` we would get out an Elm value like `Person "Tom" 42`.

If we really wanted to get into the spirit of decoders, we would define `personDecoder` as `map2 Person nameDecoder ageDecoder` using our previous definitions. You always want to be building your decoders up from smaller building blocks!
Expand Down