Skip to content
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