Intro to Elixir | Lesson 17: Using cond


English (Auto-generated)

um let's learn today about using Cond which is another way to control the flow of our application in elixir, last lesson we learned about using case. So if you recall case is a way to give something to um as the first argument of the case and then pattern match it in different ways until we get um we get like some match that will execute the code in there. Or if we don't get a match we could at the end execute what's in the underscore block Con is useful uh when we I don't want to pattern match, I would say it's a lot more flexible than case. Um So um but you know, I I would favor case if you if you're simply just gonna do pattern matching, but if you want to go beyond pattern matching then um I would say, or sometimes if you want to pattern match in a way that doesn't work exactly in in in a case statement, then you could reserve to content. So how does can't work? Well, the Wakanda works is basically you start a block but can't do and then the idea is you put some statements, so um if you put something like I know one plus one equals two, I mean that's a statement and then Uh I don't know for example, block one. Um if you want to put, let me put something wrong here, maybe one less one vehicles do Block two and then finally with the con the catch all or what you might call the else would be just um true or not, but just walk through here. Let's give this a try and see what this results in. Um In this case as you might expect we get to the truth which is the final one which is block three. If we correct this one and we make this 13. Let's see here, then it will execute block to the idea is you can put whatever you want here. You're not necessarily pattern matching. Like you remember it was the case with pattern matching. Some kind of a variable or some kind of content while in cond you just have an expression that could include whatever you want. So another example for instance, let me give you a slightly more elaborate example probably. So let's say we have a list. My list equals hello one two maybe now you can do things like well. Mhm. Um Let's say um list that I believe we've seen on the account but let's double check. No, I'm dutch discount a list. Sometimes different programming languages use different things actually spelled it here. So you know, I'm not refusing myself here. My list is um three. Um Sorry my list equals equals accessory, yep. Then you say the lens of the list is three. Well let's say here we want to use list dot um we could use something like let's say pop at At 123 and then zero. Remember this will give us a two pole. The first one would be um um element and the element that we popped let's say. And then the second one will be um the second let's say the rest of the list. Um You know I'll use a simpler method. Let's say we use last 123. I mean we could use a pop but I want to make it simple for you. So let's say last. I could use something like here. If the last element of the list, my list is The number, let's say 4, 4. We can say something. The last element of the list is four. And then let's say here we have true and we say, you know, I don't know something like this. It's not a very useful example as you can tell um in this particular case as you might guess you will get the I don't know because the last element is not four and the number of element is not three. So what I'm trying to drive the point here is you can put whatever code you want right here. Okay you could put whatever code you want and based on that you could if if if this code evaluates to true then it will execute the block in here. Which is why I would say that khan is more flexible because you don't necessarily just pattern match you actually just whatever code you want and then at the very end you literally just put through. You know this will always match, you should put it at the very end because if you put it before it will just match all the time. So you should put it basically at the very end and it will match if not none else matches awesome. So again here as a recap for that, can, you know, for for controlling the flow of the application so far, we've learned how to control the flow of the application with pattern matching, just different function heads. That's the approach I would recommend most of the time. Um I'd say, you know, second common one is probably the case because in many cases you wanna better match on a simple variable and then, you know, if that doesn't work or doesn't do the job you want to do something more elaborate, like in this case uh then you could use cond and then the first one that evaluates to True will execute the code in there, We will learn about using if and unless in the next lesson. Um And yeah, you see basically here, we have different ways to control the flow of the application in elixir and they all come in useful in different times. So it's good to be aware of all of them. Thank you for listening in and I'll see you in the next lesson
97 Views 0 Likes 0 Comments

We learn controlling the flow of code using `cond`

Leave a comment (supports markdown format)