Intro to Elixir | Lesson 19: Comprehensions


English (Auto-generated)

Hello Friends. Um Today we're gonna learn about Innumerable comprehension and innumerable comprehension. It's basically using 4 to do through innumerable. Um if you recall. Mhm. We have a list sometimes the most common way to put an ordered list of items. Let's say I'll just use some random numbers. You could simply print every element in the list. We could do it in multiple ways but one way to do it is to use the n. Um library which is built in with elixir. So we could do in um dot each and then we pass it the list and then we take a function that have X. And then we could do something like I owe that puts X. Make sure we close the function and then let's go and try this in I. X. We noticed that what our court does. It prints every element of dollars. Um So again here to recap in um dot each is a method is a is a function from the random module. The imam library that's built in with elixir. So we don't need to import it or bring it extra and then we give it the list as the first argument and then the second argument takes in a function. And this function we can write it on in line by making it take an argument and use this kind of single or thin arrow with an end. If you recall also for your memory we could shorten this by using this 1% syntax by doing something like puts and then put the first argument and we reference that by doing 1% 1. So that can also be written this way which in both cases we get the same outcome of having um of having every element of that list printed now. In some cases it may make sense to use for uh so which is again using so I'll just stay here using genome and then here 54 comprehension. So we could do something like this before X. And then we use this arrow which you use in the function but we use it in a different direction and then you give it to the list. Thank you. And we just can print it so we can do I owed upwards. Next let's try it. We get the same outcome pretty much. I mean the difference noticed in the um in the In in the case of the four is in their comprehension we're getting a list in this case as a return value and this one includes the outcome of each run of the comprehension. While in the case of the enemy we're getting the last one which is just okay and by the way this okay is the output of it's the return of io that put remember I owe that puts will display something to the to the in this case um I X. And then it will return the atom. Okay so the case of the four, the return is a little bit different so it's not exactly the same as in um under the hood I would say they're implemented in a very similar fashion. Um I would say generally speaking whenever possible favors the enemy approach, it's generally more flexible. It's more idiomatic for using elixir. Now. four has some really good spots where it's it's better to be used in some cases it makes things easier to read if you're building web applications um and you're working with views um like you maybe like Um you may have used something like EJ. S or er be before where you put a code like this where you're mixing HTML code and in this case would be electric code. So you could you could have like for example H1 in that particular case using something like a four comprehension may make things easier to read. Let's say you want to list all the users, you could do something like this again if you haven't encountered this before, that is completely okay. Um and then you can put something like happy here and then you put something like X X stop full name. In this case you probably define a function that's called the name. And then for the ex uh so something along these lines that you may have in writing View files. So basically generating html from data that is fetched with elixir. So I find four loops for comprehension Czar are very useful in situations like this that said if you have normal functions if you're just writing a function that is all elixir whenever possible. I would say more often than not using in um approach is more dramatic. It's more um it's more common I would say in in elixir code now. Um four works with other innumerable. So for instance, four is quite handy in other cases by the way, you could still using them in those cases. But for example, if you wanna like explain numbers 1 200 one way to do it is to use ranges which if you recall, it's, it's a type of data structure that we could define a range of numbers. It starts with one and then ends with the other. So in this case we could print numbers 1 to 100 by using a comprehension and passing a range to it. So let me show you here. This basically prints Um numbers 1 200 and of course we're getting a list with 100 atoms each one of them is okay. Mm hmm. Um, it can also work with sports lists. Remember keyword lists is a key value storage but it uses the list under the hood. Uh, and then we define it as such. Uh, my keyword list is a one B two C three let's say. Um The difference is um, I'll avoid using values X and Y. Maybe the differences. Remember with the keyword lists we're dealing with bears because it's really a list of troubles. So uh in this case my cure list you can do something X. Is and then inter plate X. I should say first item is and then the first the second item is why. And then maybe just to visually separate it, we can put some minds in here. So what we're doing in this case we're iterating through a cute word list. However in each iteration we get a tuple That have two elements in it. And this is where you can think of it, your pattern match this. For example A. 12 X. And Y. So X. In this case will be A. And then Y will be one. This is why you get a one B. Two and then C. Three. Um And you can basically use this comprehension for thought awesome. So that's about a list comprehension. It's it's again syntax sugar for iterating through innumerable als. Um It comes in handy in many situations and that's it for today's lesson. Uh We'll see you next time
98 Views 0 Likes 0 Comments

We learn how to use `for` to loop through enumerables

Leave a comment (supports markdown format)