Introduction to Elm, v2

Record Iteration

Richard Feldman

Richard Feldman

Vendr, Inc.
Introduction to Elm, v2

Check out a free preview of the full Introduction to Elm, v2 course

The "Record Iteration" Lesson is part of the full, Introduction to Elm, v2 course featured in this preview video. Here's what you'd learn in this lesson:

The differences while iterating over records in comparison to other collections.


Transcript from the "Record Iteration" Lesson

>> Richard Feldman: Okay, so let's talk about some differences in iteration between records and other collections, specifically lists, which we've talked about so far. So if we have a list that's 1, 2, 3. Map works fine over this, we can call, and go over those elements 1, 2, and 3.

We talked about how you cannot have a list with mixed contents, because then map would potentially blow up. So that's why map is allowed on lists, is that they rule out having mixed contents like this. Now, as we've seen with this record, records do allow mixed contexts. You can have, x = 1, y = "foo", you're allowed to mix and match whatever different types of contents you want inside of a record for the different fields.

And so for that reason, map would break if map were allowed on records. And so map's not allowed on records. This is sort of a tradeoff. So if we look at lists, they do support iteration. You can iterate over list. You can map over it. You can do a variety of other operations that work on each elements in the list.

But lists do not support mixed entries. Those go hand in hand. If it did support mixed entries, then it would not be allowed to support iteration, which is exactly what you have with records. So records do not support iteration. They don't support map, they don't support filter or any of the others, but they do support mixed entries.

And there's a causal relationship here. You sort have to pick one or the other if you want both of them to work reliably at run time. Without any possibility of crashing. There's no way to say, give me all the keys in this record, I wanna work with those.

Give me all of the values in this record, I wanna work with those. Basically, records are just a mapping between keys and values. But there's no way to iterate over either of them like there's with lists.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now