Instructor: 00:00 One of the important functions in functional programming is map. You're probably familiar with the List.map. This is a function where you have a list of A's, and a function from A to B, and you end up with the list of B's.
00:15 The function is applied to each A with individual list. There is also Maybe.map, and that is a similar signature you start with the Maybe.A in a function from A to B, and you end up with the Maybe of B. There is a map defined for decoders as well, and the pattern is the same, you start with the decoder of A, and you have a function from A to value, and you end up with the decoder of value.
00:42 The idea is that, if you can decode some JSON into an A, and you have a way to go from A to this value, then you can decode the JSON into the value. Let's go back to our user decoder to see how this could be useful.
00:56 Let's see that we want a user's email to ours be lower case. The JSON data that we get is not quite consistent, so we get the occasional applicant's email. Now, as part of decoding JSON, we would like to normalize all the emails to be lower case. What we can do, instead of simply using the string decoder, we can use string and map with the String.toLower function.
01:21 Now, you can save. You can see that even though my JSON has capital J in the email, but the decoder JSON has lower case still. Let's discuss slightly more interesting example. We have this Boolean to indicate whether the user is a premium user or not.
01:39 In elm, we can use a more elegant representation of this. We could have a type membership, and we can say that the membership can be standard or premium. Now, in our type definition, rather than having this Boolean, we have a membership of type membership.
02:02 Now, what we decode a JSON representation of a user, we need to go from Boolean to membership. Here, rather than bool, let me use membership. This would be a new decoder that I'll define just below. The idea is that, I'll start with the normal bool decoder, and then I'll map a function, let me call it two membership and this should be a function from bool to membership.
02:32 Let me put a definition in let clause. We take a Boolean and in our pattern matching the Boolean. Now, this compiles, and you can see that, in our JSON we have isPremium true and in the decoded elm object, we have membership of premium.