Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson

Already subscribed? Sign In


    Update Elm Records with the Pipe Operator

    Enrico BuonannoEnrico Buonanno

    In this lesson, we will look at updating a Record's value. We'll see how we can keep simply update a single field of a record without changing the rest by using a function and a pipe -> | operator.

    We'll also see how we can update multiple values with a comma separated list as well as look how to update deeply nested values.



    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson


    00:01 Elm has dedicated syntax for updating records. If you've created a person with certain values for some properties, how do you represent a change in those values?

    00:11 For example, let's represent a change in a person's occupation. I can define a function promote. This will take a person, and it returns that same person. A record that is based on P, and they used a pipe character to signal what changes. He would say that his new occupation is going to be same manager.

    00:40 The type of this promote function, naturally it takes a person, and returns a person. To see selection, let me change to main function. Let me promote Joe, and this is a person, so I also need to use the two string function to help some kind of string representation.

    01:02 If I compile, you can see that the origin of value Joe has occupation of analyst. After it's been passed to the promote function, his occupation is now manager, but all other fields have their previous values, so his name is still Joe, and his age is still 21.

    01:21 Notice that the value Joe has not changed, but rather the value promote Joe returns a new person that is based on Joe with the specified changes. Just the matter of readability, I would not change this there a bit too many parentheses.

    01:37 You could write Joe, say that you are passing Joe to promote, and that returns a new person. You're going to pass that to the two string function, and that intern is best to text. This is the same as before, but perhaps a bit more readable.

    01:59 Updating nested records is similar, but slightly different. To demonstrate that, I would need another type. Let me create another type areas for dogs. I will say that dogs must also have a name as a string, and have a breath that's also string. In addition, I have a Boolean to indicate whether the animal has been sterilized or not.

    02:30 Now, I will assume that each person has a dog of type dog. In real life, a person may have a list of dogs, but for simplicity, let me just assume that every person has exactly one dog. Now, let me remove Bill, and in order for Joe to still be a person, he must have a dog, so his dog will be Fido. I don't need this function anymore. I will say that Fido is a dog, and let me reformat this slightly. I'll say that he is not sterilized.

    03:10 Now in order to demonstrate updated nested records, I'll define a function sterilize pets. This takes a person, and returns a person whose pet has been sterilized.

    03:29 We have P the person, or you would think you could write is, we have the original person P. There is one field that needs a change, and that's the dog. This would be again a record, which is the original person's dog with the sterilized field set to true.

    03:53 Unfortunately, when we compile, this doesn't work. This is because you cannot reference from inside this nested record. There is a simple fix, so you can extract this into a let close. Let's dog equals, so we have the original person's dog here, and will say in equals dog.

    04:23 This now compiles. Notice it on the right, we get still the representation of promoting Joe, what we need in addition with now also sterilize his pet. If you compile this, you can see that his pet is now been sterilized.

    04:43 You can also update multiple fields at a time. Let's say for example that promote should change the person's occupation, but also his salary.

    04:51 Let me just change person to also have a field salary, will be a float. I will give Joe a current salary of $10,000. I'll say that whenever somebody is promoted, his salary becomes the original salary, so P.salary times 1.2. 20 percent hike.

    05:18 Just know this the syntax after the pipe, you put a comma separated list over fields you want to be updated. If I run this now, you can see that his new salary is $12,000.