This Lesson is for Members

Subscribe today and get access to all lessons! Plus direct HD download for offline use, enhances transcripts, member comment forums, and iTunes "podcast" RSS feed. Level up your skills now!

Unlock This Lesson

Already subscribed? Sign In

Autoplay

    Records in Elm

    Ronn RossRonn Ross

    Records are important data structures in Elm. In this lesson we learn to create a record as well as describe the record by creating a type. We will also see how these new types can be used to clean up our annotations.

    Code

    Code

    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
    Transcript

    Transcript

    00:00 Records are data structures in Elm. Let's create a record named Resources. This will contain resources that we want to store for learning Elm. The record structure is wrapped in curly braces. We will create a key value pair named Name, and set it equal to a string.

    00:21 A record can also hold different types. We can add isComplete as a bool and an ID as an int.

    00:30 Let's do a little set up to make this app run. We already importing HTML, but let's bring in everything with the double dots. In Main, let's reference a view function we are going to write, and pass in resources. Finally, create a view, and it expects resources and writes out name.

    00:51 We can access values from a record by using it's name and a dot and then then value. We will use forward function to pass it into text.

    01:04 Using square brackets, we can make a list of our records. While we're at it, let's add a link key value.

    01:20 Now that we have a list of items, we will need to map over them to transform our list into HTML.

    01:37 If we wanted, we could break up the view function by creating a function that handles the mapping of the list of resources.

    01:52 Right now, if we wanted to add a type annotation to resource list, it would look like this.

    02:12 Yikes. This is the shape of our data, but it's not very readable. We can break that out and create a resource type.

    02:21 This looks very similar to our actual record, but now we use semicolons. Use the keyword type, then alias, then the name of our new type.

    02:43 Alias says anytime someone refers to the capital R Resource, this is the structure of the data. Now we can use the name Resource in our type annotation.

    02:53 This says we are expecting a list of Resource. We could also create a type for that.

    03:07 Let's update our annotation again and add one for view. Let's verify that works.

    03:20 Being able to give a name to the structure of our data really cleans up our type annotations, and we now have the basics of records.

    Discuss

    Discuss