Introduction to Elm, v2 Record Iteration
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 list.map, and go over those elements 1, 2, and 3.
[00:00:16] 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.
[00:00:44] 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.
[00:01:02] 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.
[00:01:18] 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.
[00:01:29] 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.